wcf - WCF 服务的 REST/SOAP 端点

标签 wcf rest soap

我有一个 WCF 服务,我想将其公开为 RESTfull 服务和 SOAP 服务。 有人以前做过类似的事情吗?

最佳答案

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

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

您的场景中端点配置的示例是

<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格式,则操作的参数不能包含复杂类型。

回复 SOAP 和 RESTful POX(XML) 帖子

对于作为返回格式的普通旧 XML,这是一个同时适用于 SOAP 和 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 服务的客户端端点配置,

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

在 C# 中

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

另一种方法是公开两个不同的服务合约,并且每个合约都具有特定的配置。这可能会在代码级别生成一些重复项,但是最终,您希望使其正常工作。

关于wcf - WCF 服务的 REST/SOAP 端点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/186631/

相关文章:

带参数的 jQuery WCF 服务 MVC2 VS2010 .NET 4.0 调用失败

.net - 客户端证书 : Could not establish secure channel for SSL/TLS with authority (Again! )

java - Apache Camel 和 CXF : How do i send HTTP status code from bean

node.js - 在 Node 中保存来自 REST POST 调用的数据

xml - 如何从来自Rest调用的xml响应中获取属性

java - SOAP 故障堆栈跟踪

web-services - 如何创建 HelloWorld JAX-WS 示例(客户端 - 服务)?

c# - WCF/ASP.NET - 防止滥用,例如 DOS

c# - IIS 7.5 中带有 REST/SOAP 端点的 WCF 4 服务

c# - 如何检索虚拟目录的物理路径