将WCF配置文件转换为代码

时间:2015-10-21 23:17:55

标签: c# wcf

我目前正在为Web服务使用自动生成的代理。它将成为库的一部分,我不能使用* .config文件。在过去,我刚刚将.config代码转换为C#代码,但在这种情况下,.config文件比我过去使用的更复杂,我很难找到足够的样本来获取这个转换。

    <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>

任何有关转换上述内容的帮助都表示赞赏。

1 个答案:

答案 0 :(得分:1)

我最后看了一下我的其他项目,然后把我知道怎么做的事情拉了过来。我能够使这个工作,虽然我无法弄清楚如何配置其中一个属性(AllowSerializedSigningTokenOnReply)。但是,它似乎仍然可以正常工作。

这就是我的所作所为,希望这可以帮助别人解决这个问题的头痛:

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");