• WCF配置后支持通过URL进行http方式调用


    第一、app.config的配置,全局代码如下:

     
    <?xml version="1.0"?>
    <configuration>

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <services>
          <service name="WcfService1.Service1">
            <endpoint address="" behaviorConfiguration="webBehavior" binding="webHttpBinding"
              contract="WcfService1.IService1" />
          </service>
        </services>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <!--<serviceMetadata httpGetEnabled="true"/>-->
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
          <endpointBehaviors>
            <behavior name="webBehavior">
              <webHttp />
            </behavior>
          </endpointBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
      </system.webServer>
     
    </configuration>
     

    1.endpointBehaviors节点是后来新增且必须的

    2.注意名称webBehavior的对应关系

    3.binding="webHttpBinding"

    第二、WCF接口必须增加标记

    [OperationContract]

    [WebGet(ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]

    //[WebInvoke(ResponseFormat = WebMessageFormat.Json,Method="Post",BodyStyle = WebMessageBodyStyle.Bare)]

    String SetDph(String dph);

    1.WebGet表示通过get方式访问,WebInvoke可能通过Method指定get或post

    2.BodyStyle设置为Bare就直接返回结果,如果设置为Wrapped将会自动增加些内容

    3.ResponseFormat有xml和json两种方式

    通过上面两个地方的配置之后就能够轻松的通过http方式访问了.

  • 相关阅读:
    postgresql允许远程访问的配置修改
    Oracle常用监控sql语句
    Python Twisted 学习系列22(转载stulife最棒的Twisted入门教程)
    Python Twisted 学习系列21(转载stulife最棒的Twisted入门教程)
    有趣的题目
    入学测试题详解
    完成这个例子,说出java中针对异常的处理机制。
    遍历Map key-value的两种方法
    java中的 FileWriter类 和 FileReader类
    Java中Split函数的用法技巧
  • 原文地址:https://www.cnblogs.com/cppfans140812/p/7743451.html
Copyright © 2020-2023  润新知