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方式访问了.

posted on 2017-10-27 15:13  fendoudexiaoniao  阅读(1017)  评论(0编辑  收藏  举报

导航