如何更改默认WCF服务绑定? [英] How to change default WCF service binding?

查看:186
本文介绍了如何更改默认WCF服务绑定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WCF中,我有一些服务.其中一个必须对消息大小有更大的限制,因此我必须创建另一个绑定并更改配置.

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.

但是...我在Web.config中看不到我的服务的任何配置-什么都没有.某些东西是默认的吗? 那么在哪里可以更改服务绑定?

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

推荐答案

在WCF 4.0+中,引入了默认绑定和终结点的概念.例如,如果您创建一个新的WCF服务应用程序,而没有任何更改,那么您将使用basicHttpBinding(http的默认绑定)获得一个默认端点,监听该服务的URI.

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:

进行默认的绑定配置部分.这是通过从绑定中省略name属性来完成的,就像这样:

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>

请注意,没有name属性(出于说明目的,其他属性已被省略).您指定的配置将用作通过HTTP传入并使用basicHttpBinding的任何请求的默认设置.

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.

像在步骤1中那样创建配置,但是使用name属性,然后使用bindingConfig属性将该绑定配置分配给显式端点,如下所示:

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> 

第二个示例将"MyBinding"配置分配给已定义的端点.

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

如果要对http请求使用除basicHttpBinding之外的其他内容,则还可以更改协议映射,如Neel的答案所示.

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.

您还可以查看 Windows Communication Foundation 4开发人员简介有关WCF 4.0中引入的默认绑定/端点/等的更多信息

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

这篇关于如何更改默认WCF服务绑定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆