【问题标题】:How to convert a WCF service from SOAP to REST?如何将 WCF 服务从 SOAP 转换为 REST?
【发布时间】:2016-08-19 17:39:41
【问题描述】:

我对 WCF 很陌生,对此我有疑问。

在浏览了一些文章后,我发现在 web.config 文件中,如果我将端点绑定从 basicHttpBinding 更改为 webHttpBinding,并将 httpGetEnabled 从 true 更改为 false,它会使用 REST。

我的问题是我需要更改的仅有的两件事来制作服务 SOAP 或 REST?还是我需要更改/添加任何其他内容?

【问题讨论】:

    标签: c# wcf rest soap


    【解决方案1】:

    您可以在两个不同的端点中公开服务。 SOAP 可以使用支持SOAP 的绑定,例如basicHttpBindingRESTful 可以使用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");
    

    【讨论】:

    • 那么 httpgetenabled 将其设置为 true 还是 false 呢?我也需要更改它,对吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2019-08-15
    • 2013-11-07
    • 2013-01-09
    • 2010-09-16
    • 2013-01-24
    相关资源
    最近更新 更多