C# 将WCF配置文件转换为代码

C# 将WCF配置文件转换为代码,c#,wcf,C#,Wcf,我目前正在为Web服务使用自动生成的代理。它将成为库的一部分,我不能使用*.config文件。在过去,我只是将.config代码转换为C#代码,但在本例中,.config文件比我在过去使用的要复杂一些,我正在努力找到足够的示例来进行转换 <system.serviceModel> <extensions> <bindingElementExtensions> <add name="CustomMes

我目前正在为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>


非常感谢您对以上内容的任何帮助。

我最后看了一个其他项目,完成了我知道如何做的事情。虽然我不太清楚如何配置其中一个属性(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");

这到底是什么?
wssecurity11wstrustfebruary2005wssecurityconversationfebruary2005wssecuritypolicy11basicsecurityprofile10
?是的,他们有一些疯狂的MessageSecurityVersion枚举值。它指定使用的消息安全类型(以及信任、安全对话和安全策略)。多好的“标准”啊。我一点也不懂bt发布日期这个名字