如何将绑定添加到WCF的Config文件中?

时间:2013-10-22 10:26:49

标签: .net wcf wcf-binding

我正在编写一个WCF应用程序,用于使用POST从android接收文件,并抛出System.ServiceModel.ServiceActivation异常,我理解从链接中要做什么: -

System.ServiceModel.ServiceActivationException in wcf service

以下是Web.config文件

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

    <system.web>
        <compilation debug="true" targetFramework="4.0" />
    </system.web>
    <system.serviceModel>
        <behaviors>
            <serviceBehaviors>
                <behavior name="Service1Behavior">
                    <serviceMetadata httpGetEnabled="true"/>
                    <serviceDebug includeExceptionDetailInFaults="false"/>

                </behavior>
            </serviceBehaviors>
            <endpointBehaviors>
                <behavior name="web">
                    <webHttp/>
                </behavior>
            </endpointBehaviors>
        </behaviors>

        <services>
            <service name="WcfImageUpload.Service1"
                     behaviorConfiguration="ServiceBehaviour" >
                <host>
                    <baseAddresses>
                        <add baseAddress="http://somesite.com:5555/Service1/" />
                    </baseAddresses>
                </host>
                <endpoint name="Service1"
                          address=""
                          binding="webHttpBinding"
                          contract="WcfImageUpload.IService1" 
                          behaviorConfiguration="web"/>

                <endpoint name="LoginServiceMex"
                          address="mex"
                          binding="mexHttpBinding"
                          contract="IMetadataExchange" />
            </service>
        </services>
    </system.serviceModel>
</configuration>

我需要添加绑定

<bindings>
  <webHttpBinding>
    <binding name="WebHttpEndpointBinding">
      <security mode="TransportCredentialOnly">
        <transport clientCredentialType="Windows"/>
      </security>
    </binding>
  </webHttpBinding>
</bindings>

我应该在哪里添加,我是.NET和WCF的新手。

2 个答案:

答案 0 :(得分:0)

你可以像下面这样放置......

<system.serviceModel>
  <services>
   <service  name="MyService">
  <endpoint address="http://localhost/IISHostedService/MyService.svc" 
  binding="wsHttpBinding" bindingName="wshttpbind" contract="IMyService">
  <identity>
  <dns value="localhost"/>
  </identity>
  </endpoint>
  <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
    </service>
  </services>
  <bindings>
      <webHttpBinding>
         <binding name="WebHttpEndpointBinding">
           <security mode="TransportCredentialOnly">
             <transport clientCredentialType="Windows"/>
           </security>
         </binding>
      </webHttpBinding>
   </bindings>

</system.serviceModel>

谢谢, Vishal Patel

答案 1 :(得分:0)

您需要在<system.ServiceModel>标记内的WCF服务的web.config中包含绑定。

你有多个WCF支持的绑定,比如基本的http绑定,Web HttpBinding,wsHttpBinding等。

如果要像asmx服务那样访问服务,请使用使用SOAP的BasicHttpBinding。

如果您希望WCF是RESTful,请使用WebHttpBinding并相应地配置行为。还有其他绑定,比如wsHttpBindlings更安全。