简体   繁体   中英

How to change default WCF service binding?

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM