zoukankan      html  css  js  c++  java
  • WCF 添加 RESTful 支持,适用于 IIS、Winform、cmd 宿主

    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>
    

      

    so, the service will be available at

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

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

      

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

    Reply to the post for SOAP and RESTful POX(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);
    }
    

      POX behavior for REST Plain Old XML

    <behavior name="poxBehavior">
      <webHttp/>
    </behavior>
    

      Endpoints

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

      

    Service will be available at

    REST request try it in browser,

    http://www.example.com/xml/accounts/A123

    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>
    

      in 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.

  • 相关阅读:
    POJ 1915 简单 广搜题
    poj 2479 最大子数段
    poj 1321 深搜题
    hdu 1024 最大子段和
    .net 学习总结
    [转]SharpDevelop源码分析 (二、主程序+隐藏的初始化)
    工作六年经验分享:软件工程师如何修炼(转)
    Ajax命名空间一个获取指定的页面元素的快捷方式——$get()
    [转]SharpDevelop代码分析 (一、序+基本概念)
    [转]SharpDevelop源码分析 (三、插件系统)
  • 原文地址:https://www.cnblogs.com/pangguoming/p/7503984.html
Copyright © 2011-2022 走看看