WCF自定义绑定配置

3

我编写了一个继承自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);

我的问题是我不知道如何在App.config文件中进行配置。 它是customBinding元素还是bindingExtension元素?还是其他什么东西?

2个回答

6
当您在代码中创建自定义绑定时,是否也实现了“YourBindingElement”(派生自StandardBindingElement)和“YourBindingCollectionElement”(派生自StandardBindingCollectionElement)?如果是这样-请使用它来配置您的自定义绑定,就像任何其他绑定一样。第一步是在app.config或web.config文件的扩展部分中的<system.serviceModel>中注册您的绑定。
<extensions>
  <bindingExtensions>
    <add name="yourBindingName" 
       type="YourBinding.YourBindingCollectionElement, YourBindingAssembly" />
  </bindingExtensions>
</extensions>

现在,您的新绑定已在WCF中注册为“正常”可用绑定。像其他绑定一样,在绑定部分指定您的特定内容:
<bindings>
  <yourBinding>
    <binding name="yourBindingConfig" 
             proxyAddress="...." useDefaultWebProxy="false" />
  </yourBinding>
</bindings>

在此处指定其他参数,如您的“...BindingElement”类中定义的那样。
最后,在system.serviceModel中的服务和/或客户端部分中像正常绑定一样使用您的绑定。
<client>
  <endpoint
    address="....your url here......"
    binding="yourBinding" 
    bindingConfiguration="yourBindingConfig"
    contract="IYourContract" 
    name="YourClientEndpointName" />
</client>

我在网上找不到很多关于如何在代码中编写自己的绑定的资源 - 微软有一个WCF/WPF/WF示例套件,其中包括一些示例,我基本上从中学到了足够的知识来解决它 :-)
Michele Leroux Bustamante有一篇非常好的文章介绍如何创建自定义绑定 - 这是系列的第二部分,但第一部分不公开 :-(
这是一个在代码中的示例自定义绑定,您可以下载完整的源代码:ClearUserNameBinding
Marc

0
如果您想通过配置使用此自定义绑定,您必须扩展BindingCollectionElement抽象基类并在web.config中定义bindingExtensions元素。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接