我正在尝试联系托管在控制台应用程序中的基本 REST 服务(由于我们产品的特定原因,我们无法在 IIS 中托管)。我可以访问 localhost:8002/Session_REST/,并且 wsdl 显示正常。但是,如果我尝试 localhost:8002/Session_REST/HelloWorld_GET 或 localhost:8002/Session_REST/HelloWorld_GET/ - 我得到的只是 405 错误:找不到方法。
应用程序配置:
<?xml version="1.0"?>
<configuration>
<system.serviceModel>
<services>
<service name="Proprietary.WebServices.Session_REST" behaviorConfiguration="servBehavior">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8002/Session_REST/"/>
</baseAddresses>
</host>
<endpoint address="xmlService" binding="webHttpBinding" behaviorConfiguration="restBehavior" contract="Proprietary.WebServices.ISession_REST">
<identity>
<userPrincipalName value="warren_thompson@proprietary.com" />
</identity>
</endpoint>
<endpoint address="mex" contract="IMetadataExchange" binding="mexHttpBinding" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp helpEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
</startup>
</configuration>
控制台应用程序:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceProcess;
using System.ServiceModel;
using Proprietary.WebServices;
using Proprietary.Framework;
namespace MyTestConsole
{
class Program
{
static void Main(string[] args)
{
ServiceHost testServiceHost2 = null;
try
{
testServiceHost2 = new ServiceHost(typeof(Session_REST));
testServiceHost2.Open();
Console.WriteLine("Server Started");
Console.ReadLine();
}
finally
{
if (testServiceHost2 != null)
{
((IDisposable)testServiceHost2).Dispose();
}
}
}
}
}
WCF 合约:使用系统;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using Proprietary.Framework;
using System.IO;
namespace Proprietary.WebServices
{
[ServiceContract]
public interface ISession_REST
{
[OperationContract]
[WebInvoke(BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json)]
string HelloWorld_POST();
[OperationContract]
[WebInvoke(Method="GET")]
string HelloWorld_GET();
}
}
WCF 实现:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using Proprietary.Framework;
using Proprietary.WebSupport;
using System.Web.Script.Serialization;
namespace Proprietary.WebServices
{
/// <summary>
/// Represents a doclink web service session - Rest version.
/// </summary>
public class Session_REST : ISession_REST
{
string ISession_REST.HelloWorld_POST()
{
return "Hellow World via POST";
}
string ISession_REST.HelloWorld_GET()
{
return "Hello World via GET";
}
}
}