如何将WCF服务从SOAP转换为REST?

Rak*_*esh 2 c# rest wcf soap

我对WCF很新,我对它有疑问.

在浏览了一些文章后,我发现在web.config文件中,如果我将从bindHttpBinding和httpGetEnabled的端点绑定从true更改为false,则使用REST.

我的问题是,这些只是我需要改变以制作服务SOAP或REST的两件事吗?或者我是否需要更改/添加任何其他内容?

VVN*_*VVN 6

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

<endpointBehaviors>
  <behavior name="jsonBehavior">
    <enableWebScript/>
  </behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

您的方案中的端点配置示例如下

<services>
  <service name="TestService">
    <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
    <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
  </service>
</services>
Run Code Online (Sandbox Code Playgroud)

应用[WebGet]操作合同使其成为RESTful.例如

public interface ITestService
{
   [OperationContract]
   [WebGet]
   string HelloWorld(string text)
}
Run Code Online (Sandbox Code Playgroud)

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

<client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
      contract="ITestService" name="BasicHttpBinding_ITestService" />
  </client>
Run Code Online (Sandbox Code Playgroud)

在C#中

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");
Run Code Online (Sandbox Code Playgroud)