• WCF服务的Web HTTP方式


    NET 3.5以后,WCF中提供了WebGet的方式,允许通过url的形式进行Web 服务的访问。现将WCF服务设置步骤记录如下:

    1. endpoint通讯协议设置成  webHttpBinding

    2. endpoint的行为设置成 <webHttp />

    3. 在接口上加入 WebGet 的Attributes

    示例代码如下: web.config文件的配置 

     <system.serviceModel>
        <services>
          <service name="Services.ShowerService">
             /*提供Web HTTP服务用*/
            <endpoint binding="webHttpBinding" behaviorConfiguration="WebBehavior" contract="Services.IShowerService" />
          </service>
        </services>
        <behaviors>
          <!--WCF中提供了Web HTTP的方式-->
          <endpointBehaviors>
            <behavior name="WebBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
          <!--WCF中提供了Web HTTP的方式-->
    
          <serviceBehaviors>
            <behavior name="">
              <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"  />
              <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
      </system.serviceModel>

     WCF接口的设置,这里加入了对URI模板(UriTemplate)和JSON(WebMessageFormat.Json)的支持:

    namespace Services
    {
        [ServiceContract]
        public interface ShowerService
        {
            [OperationContract]
            [WebGet(UriTemplate="/Hello/{name}", RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
            string Hello(string name);
        }
    }

     测试:打开IE浏览器,在地址栏输入:http://localhost:3000/Services/ShowerService.svc/hello/abc,将会看到访问后的结果。  

     调试:将Web.config中的 <webHttp /> 修改为 <webHttp helpEnabled="true" />将可以在浏览器页面中列举出可用接口,并提供提交的数  据样例。

     打开IE浏览器,在地址栏输入:http://localhost:3000/Services/ShowerService.svc/help 即可。

     Siverlight 访问:使用SL的WebClient访问WebInvoke方法时,不要忘记将 HttpRequestHeader.ContentType 设置成 application/json,代码如下所示:

     WebClient client = new WebClient();
     client.Headers[HttpRequestHeader.ContentType] = "application/json";
  • 相关阅读:
    PHP字符串
    PHP第四章数组2
    oop soa cbd
    spring 时间组件
    IReport 常见问题整理
    maven 与 jenkins 集成的时候,打包出现错误处理
    Mybatis 传递参数中的_paramter 的理解
    JS 之表单特殊控制
    JQuery 中设置AJAX 的全局函数
    spring + ehcache 整合
  • 原文地址:https://www.cnblogs.com/li150dan/p/9529323.html
Copyright © 2020-2023  润新知