WCF CustomBinding配置

uri*_*ini 3 wcf web-services

我编写了一个继承自CustomBinding的自定义绑定类.我的自定义类会覆盖BuildChannelFactory方法,并使用自定义ChannelFactory来创建自定义通道.

我在WCF客户端中使用自定义绑定类时遇到了困难.如果我在代码中配置它,我可以使用我的自定义绑定类:

Binding myCustomBinding = new MyCustomBinding();

ChannelFactory<ICustomerService> factory = 
   new ChannelFactory<ICustomerService>(myCustomBinding, 
        new EndpointAddress("http://localhost:8001/MyWcfServices/CustomerService/"));

ICustomerService serviceProxy = factory.CreateChannel();
serviceProxy.GetData(5);
Run Code Online (Sandbox Code Playgroud)

我的问题是我不知道如何在App.config文件中配置它.它是customBinding元素还是bindingExtension元素?还有别的吗?

mar*_*c_s 5

在代码中创建自定义绑定时,是否还实现了"YourBindingElement"(从StandardBindingElement派生)和"YourBindingCollectionElement"(从StandardBindingCollectionElement派生)?

如果是这样 - 使用它来配置自定义绑定,就好像它是任何其他绑定一样.

第一步是在<system.serviceModel>的扩展部分的app.config或web.config文件中注册绑定

<extensions>
  <bindingExtensions>
    <add name="yourBindingName" 
       type="YourBinding.YourBindingCollectionElement, YourBindingAssembly" />
  </bindingExtensions>
</extensions>
Run Code Online (Sandbox Code Playgroud)

现在,您的新绑定在WCF中注册为"正常"可用绑定.在绑定部分中指定您的细节以及其他绑定:

<bindings>
  <yourBinding>
    <binding name="yourBindingConfig" 
             proxyAddress="...." useDefaultWebProxy="false" />
  </yourBinding>
</bindings>
Run Code Online (Sandbox Code Playgroud)

在此处指定其他参数,如"... BindingElement"类中所定义.

最后,在system.serviceModel中的服务和/或客户端部分中使用绑定,就像正常绑定一样:

<client>
  <endpoint
    address="....your url here......"
    binding="yourBinding" 
    bindingConfiguration="yourBindingConfig"
    contract="IYourContract" 
    name="YourClientEndpointName" />
</client>
Run Code Online (Sandbox Code Playgroud)

我真的找不到很多关于如何在网络上的代码中编写自己的绑定的资源 - 微软有一个WCF/WPF/WF示例工具包,其中包含一些样本,我基本上从中学到了很多东西: - )

Michele Leroux Bustamante一篇关于创建自定义绑定的非常好的文章 - 系列的第2部分,但第1部分不公开:-(

下面是代码中的自定义绑定示例,您可以从中下载完整的源代码:ClearUserNameBinding.