How to change default WCF service binding?

徘徊边缘 提交于 2019-12-08 08:01:23

问题


In my WCF I have a few services. One of them must have a bigger limit for message size so i must create another binding and change configuration.

But... i can't see any configuration of my services in Web.config - nothing. Something is default? So where I can change service binding?


回答1:


In WCF 4.0+ the concept of default bindings and endpoints was introduced. If you create a new WCF Service Application, for example, out of the box with no changes you will get a default endpoint listening at the URI of the service using basicHttpBinding (the default binding for http).

If you need larger values than the default values for binding configuration, you have two choices:

Make a default binding configuration section. This is done by omitting the name attribute from the binding, like this:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding maxReceivedMessageSize="528880" />
    </basicHttpBinding>
  </bindings>
<system.serviceModel>

Note there is no name attribute (the other attributes have been omitted for purposes of the illustration). The configuration you specified is will be used as the default for any request that comes in over http and uses basicHttpBinding.

Create the configuration as in step 1, but use the name attribute and then assign that binding configuration to an explicit endpoint using the bindingConfig attribute, like this:

<system.serviceModel>
  <bindings>
    <basicHttpBinding>
      <binding name="MyBinding" maxReceivedMessageSize="528880" />
    </basicHttpBinding>
  </bindings>
  <services>
    <service name="MyService">
      <endpoint address="" bindingConfiguration="MyBinding" binding="basicHttpBinding" contract="MyService.IMyContract" />
    </service>
  </services>
<system.serviceModel> 

The second example will assign the "MyBinding" configuration to the defined endpoint.

If you want to use something other than basicHttpBinding for http requests, then you can also change the protocol mapping as shown in Neel's answer.

You can also check out A Developer's Introduction to Windows Communication Foundation 4 for more information on the default bindings/endpoints/etc introduced in WCF 4.0




回答2:


If you want to change default binding to wsHttpBinding you must use:

<protocolMapping>
    <add scheme="http" binding="wsHttpBinding"/>
</protocolMapping>


来源:https://stackoverflow.com/questions/27500078/how-to-change-default-wcf-service-binding

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!