WCF服务的REST / SOAP端点

我有一个WCF服务,我想把它作为RESTfull服务和SOAP服务公开。 任何人都曾经做过这样的事情?

您可以在两个不同的端点公开服务。 SOAP可以使用支持SOAP的绑定,例如basicHttpBinding,RESTful的可以使用webHttpBinding。 我假设您的REST服务将使用JSON,在这种情况下,您需要使用以下行为configuration来configuration两个端点

<endpointBehaviors> <behavior name="jsonBehavior"> <enableWebScript/> </behavior> </endpointBehaviors> 

您的scheme中的端点configuration示例是

 <services> <service name="TestService"> <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/> <endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="ITestService"/> </service> </services> 

所以,该服务将可在

将[WebGet]应用于操作合同,使其成为RESTful。 例如

 public interface ITestService { [OperationContract] [WebGet] string HelloWorld(string text) } 

请注意,如果REST服务不在JSON中,则操作的参数不能包含复杂types。

回复SOAP和RESTful POX(XML)的post

对于普通的旧的XML作为返回格式,这是一个既适用于SOAP又适用于XML的示例。

 [ServiceContract(Namespace = "http://test")] public interface ITestService { [OperationContract] [WebGet(UriTemplate = "accounts/{id}")] Account[] GetAccount(string id); } 

REST Plain Old XML的POX行为

 <behavior name="poxBehavior"> <webHttp/> </behavior> 

端点

 <services> <service name="TestService"> <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/> <endpoint address="xml" binding="webHttpBinding" behaviorConfiguration="poxBehavior" contract="ITestService"/> </service> </services> 

服务将在

REST请求在浏览器中尝试,

http://www.example.com/xml/accounts/A123

添加服务引用后,SOAP服务的SOAP请求客户端端点configuration,

  <client> <endpoint address="http://www.example.com/soap" binding="basicHttpBinding" contract="ITestService" name="BasicHttpBinding_ITestService" /> </client> 

在C#

 TestServiceClient client = new TestServiceClient(); client.GetAccount("A123"); 

另一种方式是公开两个不同的服务合同,每一个都有特定的configuration。 这可能会在代码级别生成一些重复,但是在一天结束时,您希望使其工作。

这个post已经被“社区wiki”很好的回答了,我也推荐看看Rick Strahl的networking博客,关于WCF Rest有很多好的post,像这样 。

我用这两种方法来获得这种MyService服务…然后我可以使用jQuery中的REST接口或Java中的SOAP。

这是从我的Web.Config:

 <system.serviceModel> <services> <service name="MyService" behaviorConfiguration="MyServiceBehavior"> <endpoint name="rest" address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="restBehavior"/> <endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/> <endpoint name="soap" address="soap" binding="basicHttpBinding" contract="MyService"/> </service> </services> <behaviors> <serviceBehaviors> <behavior name="MyServiceBehavior"> <serviceMetadata httpGetEnabled="true"/> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> <endpointBehaviors> <behavior name="restBehavior"> <webHttp/> </behavior> </endpointBehaviors> </behaviors> </system.serviceModel> 

这是我的服务类(.svc代码隐藏,不需要接口):

  /// <summary> MyService documentation here ;) </summary> [ServiceContract(Name = "MyService", Namespace = "http://myservice/", SessionMode = SessionMode.NotAllowed)] //[ServiceKnownType(typeof (IList<MyDataContractTypes>))] [ServiceBehavior(Name = "MyService", Namespace = "http://myservice/")] public class MyService { [OperationContract(Name = "MyResource1")] [WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "MyXmlResource/{key}")] public string MyResource1(string key) { return "Test: " + key; } [OperationContract(Name = "MyResource2")] [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource/{key}")] public string MyResource2(string key) { return "Test: " + key; } } 

其实我只使用Json或Xml,但是这两者都是为了演示的目的。 那些是获取数据的GET请求。 要插入数据,我会使用具有属性的方法:

 [OperationContract(Name = "MyResourceSave")] [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource")] public string MyResourceSave(string thing){ //... 

如果您只想开发一个Web服务并将其托pipe在许多不同的端点(即SOAP + REST,带有XML,JSON,CSV和HTML输出)上。 您还应该考虑使用我为此构build的ServiceStack ,其中您开发的每个服务都可以在SOAP和REST端点上自动使用,而无需进行任何configuration。

Hello World示例显示了如何使用服务创build一个简单的(无需configuration):

 public class Hello { public string Name { get; set; } } public class HelloResponse { public string Result { get; set; } } public class HelloService : IService { public object Any(Hello request) { return new HelloResponse { Result = "Hello, " + request.Name }; } } 

没有其他configuration是必需的,这个服务立即可用在REST中:

  • 肥皂
  • XML
  • JSON
  • HTML

它还内置了一个友好的HTML输出 (当使用具有Accept:text / html(例如浏览器)的HTTP客户端进行调用时),以便更好地显示服务的输出。

处理不同的REST动词也是微不足道的,下面是一个完整的REST服务CRUD应用程序,在1页的C#中(less于configurationWCF所需的):

  • 前端TODO应用程序
  • 后端REST服务实现

MSDN现在似乎有一个这样的文章:

https://msdn.microsoft.com/en-us/library/bb412196(v=vs.110).aspx

介绍:

默认情况下,Windows Communication Foundation(WCF)使端点只能用于SOAP客户端。 在如何:创build一个基本的WCF Web HTTP服务,一个端点可用于非SOAP客户端。 有时您可能希望以双方的方式创build同一个合同,如Web端点和SOAP端点。 本主题显示如何执行此操作的示例。