【发布时间】:2016-08-19 17:39:41
【问题描述】:
我对 WCF 很陌生,对此我有疑问。
在浏览了一些文章后,我发现在 web.config 文件中,如果我将端点绑定从 basicHttpBinding 更改为 webHttpBinding,并将 httpGetEnabled 从 true 更改为 false,它会使用 REST。
我的问题是我需要更改的仅有的两件事来制作服务 SOAP 或 REST?还是我需要更改/添加任何其他内容?
【问题讨论】:
我对 WCF 很陌生,对此我有疑问。
在浏览了一些文章后,我发现在 web.config 文件中,如果我将端点绑定从 basicHttpBinding 更改为 webHttpBinding,并将 httpGetEnabled 从 true 更改为 false,它会使用 REST。
我的问题是我需要更改的仅有的两件事来制作服务 SOAP 或 REST?还是我需要更改/添加任何其他内容?
【问题讨论】:
您可以在两个不同的端点中公开服务。 SOAP 可以使用支持SOAP 的绑定,例如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)
}
添加服务引用后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");
【讨论】: