WCF 服务的 REST/SOAP 端点 [英] REST / SOAP endpoints for a WCF service

查看:44
本文介绍了WCF 服务的 REST/SOAP 端点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 WCF 服务,我想将它公开为 RESTfull 服务和 SOAP 服务.以前有人做过这样的事情吗?

I have a WCF service and I want to expose it as both a RESTfull service and as a SOAP service. Anyone has done something like this before?

推荐答案

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

You can expose the service in two different endpoints. the SOAP one can use the binding that support SOAP e.g. basicHttpBinding, the RESTful one can use the webHttpBinding. I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration

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

您方案中的端点配置示例是

An example of endpoint configuration in your scenario is

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

因此,该服务将在

将 [WebGet] 应用到操作合约上,使其成为 RESTful.例如

Apply [WebGet] to the operation contract to make it RESTful. e.g.

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

注意,如果 REST 服务不是 JSON,则操作的参数不能包含复杂类型.

Note, if the REST service is not in JSON, parameters of the operations can not contain complex type.

对于作为返回格式的纯旧 XML,这是一个适用于 SOAP 和 XML 的示例.

For plain old XML as return format, this is an example that would work both for SOAP and XML.

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

REST 的 POX 行为 普通的旧 XML

<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 服务的客户端端点配置,

SOAP request client endpoint configuration for SOAP service after adding the service reference,

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

在 C# 中

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

另一种方法是公开两个不同的服务合约,每个合约都有特定的配置.这可能会在代码级别生成一些重复项,但归根结底,您想让它工作.

Another way of doing it is to expose two different service contract and each one with specific configuration. This may generate some duplicates at code level, however at the end of the day, you want to make it working.

这篇关于WCF 服务的 REST/SOAP 端点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆