Paul Mrozowski
Paul Mrozowski

Reputation: 6734

Converting a WCF config file into code

I am currently using an auto-generated proxy for a Web Service. It's going to be part of a library and I can't use the *.config file. In the past I've just converted the .config code to C# code, but in this case the .config file is a bit more complex than what I've used in the past and I'm struggling to find enough samples to get this converted.

    <system.serviceModel>
    <extensions>
        <bindingElementExtensions>
            <add name="CustomMessageEncoder" type="SampleAPI.CustomMessageEncoderBindingElementExtensionElement, SampleAPI" />
        </bindingElementExtensions>
    </extensions>
    <bindings>
        <customBinding>
            <binding name="DeviceInfoServiceBinding">
                <CustomMessageEncoder></CustomMessageEncoder>
                <security authenticationMode="CertificateOverTransport"
                          allowSerializedSigningTokenOnReply="true" 
                          enableUnsecuredResponse="true"
                          messageSecurityVersion="WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10" >                        
                </security>
                <httpsTransport maxReceivedMessageSize="2000000"></httpsTransport>                  
            </binding>                
        </customBinding>
    </bindings>
    <client>
        <endpoint address="https://ws.sample.com/DeviceQuery/v1"
                  binding="customBinding"
                  bindingConfiguration="DeviceInfoServiceBinding"
                  contract="TestWS.DeviceInfoServiceType"
                  behaviorConfiguration="CertBehavior"
                  name="DeviceInfoServiceType">
        </endpoint>
    </client>
    <behaviors>
        <endpointBehaviors>
            <behavior name="CertBehavior">                  
                <clientCredentials>
                    <clientCertificate storeLocation="CurrentUser" 
                                       storeName="My" 
                                       findValue="Sample" 
                                       x509FindType="FindByIssuerName" />
                </clientCredentials>                    
            </behavior>
        </endpointBehaviors>
    </behaviors>
</system.serviceModel>

Any help with converting the above is appreciated.

Upvotes: 2

Views: 669

Answers (1)

Paul Mrozowski
Paul Mrozowski

Reputation: 6734

I ended up looking at one of my other projects and just pulled over the things I knew how to do. I was able to get this working, although I couldn't quite figure out how to configure one of the properties (AllowSerializedSigningTokenOnReply). However, it still seemed to work OK.

Here's what I did, hopefully this will save someone the headache of figuring this out:

var binding = new CustomBinding();
binding.Elements.Add(new CustomMessageEncoderBindingElement());
var sec = SecurityBindingElement.CreateCertificateOverTransportBindingElement();
// AllowSerializedSigningTokenOnReply = true
sec.EnableUnsecuredResponse = true;
sec.MessageSecurityVersion = MessageSecurityVersion.WSSecurity11WSTrustFebruary2005WSSecureConversationFebruary2005WSSecurityPolicy11BasicSecurityProfile10;
binding.Elements.Add(sec);
binding.Elements.Add(new HttpsTransportBindingElement() { MaxReceivedMessageSize = 2000000 });

var endpoint = new EndpointAddress("https://ws.sample.com/DeviceQuery/v1");

var client = new TestWS.DeviceInfoServiceTypeClient(binding, endpoint);
client.ClientCredentials.ClientCertificate.SetCertificate(StoreLocation.CurrentUser,
                                                      StoreName.My,
                                                      X509FindType.FindByIssuerName,
                                                      "Sample");

Upvotes: 1

Related Questions