WCF 自定义绑定配置 [英] WCF CustomBinding Configuration

查看:31
本文介绍了WCF 自定义绑定配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've written a custom binding class that inherits from CustomBinding. My custom class overrides the BuildChannelFactory method and uses a custom ChannelFactory to create a custom channel.

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

I'm having difficulties using the custom binding class in the WCF client. I am able to use my custom binding class if I configure it in code:

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 元素?还有别的吗?

My problem is I don't know how to configure it in the App.config file. Is it a customBinding element, or a bindingExtension element? Is it something else?

推荐答案

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

When you created your custom binding in code, did you also implement a "YourBindingElement" (deriving from StandardBindingElement) and a "YourBindingCollectionElement" (deriving from StandardBindingCollectionElement) along with it?

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

If so - use that to configure your custom binding, as if it were any other binding.

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

The first step is to register your binding in the app.config or web.config file in the extensions section of <system.serviceModel>

<extensions>
  <bindingExtensions>
    <add name="yourBindingName" 
       type="YourBinding.YourBindingCollectionElement, YourBindingAssembly" />
  </bindingExtensions>
</extensions>

现在,您的新绑定在 WCF 中注册为正常"可用绑定.与其他绑定一样,请在绑定部分指定您的详细信息:

Now, your new binding is registered as a "normal" available binding in WCF. Specify your specifics in the bindings section as for other bindings, too:

<bindings>
  <yourBinding>
    <binding name="yourBindingConfig" 
             proxyAddress="...." useDefaultWebProxy="false" />
  </yourBinding>
</bindings>

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

Specify other parameters here, as defined in your "...BindingElement" class.

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

Lastly, use your binding like a normal binding in your services and/or client sections in system.serviceModel:

<client>
  <endpoint
    address="....your url here......"
    binding="yourBinding" 
    bindingConfiguration="yourBindingConfig"
    contract="IYourContract" 
    name="YourClientEndpointName" />
</client>

我真的找不到很多关于如何在网络上用代码编写自己的绑定的资源 - Microsoft 有一个 WCF/WPF/WF 示例套件,其中包括一些我基本上学到的足以解决问题的示例:-)

I couldn't really find a lot of resources on how to write your own binding in code on the web - Microsoft has a WCF/WPF/WF sample kit which includes a few samples from which I basically learned enough to figure it out :-)

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

There's one really good article by Michele Leroux Bustamante on creating your custom bindings - part 2 of a series, but part 1 is not available publicly :-(

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

Here's a sample custom binding in code for which you can download the complete source code: ClearUserNameBinding.

马克

这篇关于WCF 自定义绑定配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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