WCF User Interface Configuration

I wrote a custom binding class that inherits from CustomBinding. My custom class overrides the BuildChannelFactory method and uses its own ChannelFactory to create a custom channel.

I'm having difficulty using a custom bind class in the WCF client. I can use my own 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); 

My problem: I do not know how to configure it in the App.config file. Is this a customBinding element or a bindingExtension element? Is this something else?

+3
source share
2 answers

When you created your own binding in the code, did you also implement “YourBindingElement” (obtained from StandardBindingElement) and “YourBindingCollectionElement” (obtained from StandardBindingCollectionElement) with it?

If so, use this to customize your custom binding, as if it were any other binding.

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

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

Your new binding is now registered as a “normal” available binding in WCF. Define your specifics in the bindings section, as with other bindings:

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

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

Finally, use the binding as 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> 

I could not find many resources on how to write my own binding in code on the Internet. Microsoft has a WCF / WPF / WF sample kit that includes several samples, from which I basically learned enough to understand this :-)

There's a really nice article in Michele Leroux Bustamante 's article on creating your custom bindings - part 2 of the series, but part 1 is not publicly available:

Here is an example of custom binding in code for which you can download the full source code: ClearUserNameBinding .

Mark

+5
source

If you want to use this custom binding through configuration, you must extend the abstract BindingCollectionElement base and define the bindingExtensions element in web.config.

0
source

Source: https://habr.com/ru/post/1478930/


All Articles