Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I programmatically create this custom binding?

Tags:

We've got to access a web service that uses soap11... no problem I'll just set the binding as:

BasicHttpBinding wsBinding = new BasicHttpBinding(BasicHttpSecurityMode.TransportWithMessageCredential); 

Nope. No dice. So I asked the host of the service why we're having authentication issues and he said that our config needed to have this custom binding:

<bindings>     <customBinding>         <binding name="lbinding">             <security  authenticationMode="UserNameOverTransport"                  messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11"                  securityHeaderLayout="Strict"                  includeTimestamp="false"                 requireDerivedKeys="true"                  keyEntropyMode="ServerEntropy">             </security>             <textMessageEncoding messageVersion="Soap11" />             <httpsTransport authenticationScheme ="Negotiate" requireClientCertificate ="false" realm =""/>         </binding>     </customBinding> </bindings> 

Only problem is we're creating our binding programmatically not via the config. So if someone could point me in the right direction in regards to changing my BasicHttpBinding into a custombinding that conforms to the .config value provided I'll give them a big shiny gold star for the day.

like image 439
D.Forrest Avatar asked Mar 31 '10 12:03

D.Forrest


People also ask

How to create custom binding in WCF?

Custom bindings can be built from a set of system-provided binding elements or can include user-defined custom binding elements. You can use custom binding elements, for example, to enable the use of new transports or encoders at a service endpoint. For working examples, see Custom Binding Samples.

How can custom binding be added to an object that is sealed?

To do this, you add the individual binding elements to a collection represented by an instance of the BindingElementCollection class, and then set the Elements property of the CustomBinding equal to that object.

What is binding configuration in WCF?

WCF achieves this by configuring binding attributes of an endpoint. WCF lets you choose HTTP or TCP transport protocol, encoding, etc. just by tweaking the value of binding attribute for an endpoint. To cater to different transport protocols, WCF lets you select HTTP, TCP and MSMQ binding types.


2 Answers

Solved it!

Here's the winning code for those who are in a similar predicament.

Uri epUri = new Uri(_serviceUri); CustomBinding binding = new CustomBinding(); SecurityBindingElement sbe = SecurityBindingElement.CreateUserNameOverTransportBindingElement(); sbe.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11;         sbe.SecurityHeaderLayout = SecurityHeaderLayout.Strict; sbe.IncludeTimestamp = false; sbe.SetKeyDerivation(true); sbe.KeyEntropyMode = System.ServiceModel.Security.SecurityKeyEntropyMode.ServerEntropy; binding.Elements.Add(sbe); binding.Elements.Add(new TextMessageEncodingBindingElement(MessageVersion.Soap11, System.Text.Encoding.UTF8)); binding.Elements.Add(new HttpsTransportBindingElement()); EndpointAddress endPoint = new EndpointAddress(epUri); 
like image 58
D.Forrest Avatar answered Sep 19 '22 15:09

D.Forrest


@D. Forrest already found the solution, but a simple way to see what objects are involved for a given WCF configuration is to call .Endpoint.Binding.CreateBindingElements() on the client proxy you are using. You can the dump the object tree of each item in the list that is returned and see how the binding is configured.

like image 44
Jeremy Wiebe Avatar answered Sep 17 '22 15:09

Jeremy Wiebe