WCF命令式绑定

时间:2011-02-09 13:48:09

标签: wcf wcf-binding wcf-client

如何将以下声明性(通过配置文件)绑定转换为命令式绑定(应用程序内部硬编码)?

<system.serviceModel>
        <bindings>
            <customBinding>
                <binding name="CustomBinding_IAEService">
                    <security defaultAlgorithmSuite="Basic256Sha256Rsa15" authenticationMode="MutualCertificateDuplex"
                        requireDerivedKeys="false" securityHeaderLayout="Lax" includeTimestamp="true" allowSerializedSigningTokenOnReply="true"
                        keyEntropyMode="CombinedEntropy" messageProtectionOrder="SignBeforeEncrypt"
                        messageSecurityVersion="Default" requireSignatureConfirmation="false">
                        <localClientSettings cacheCookies="true" detectReplays="true"
                            replayCacheSize="900000" maxClockSkew="00:05:00"
                            replayWindow="00:05:00" sessionKeyRenewalInterval="10:00:00"
                            sessionKeyRolloverInterval="00:05:00" reconnectTransportOnFailure="true"
                            timestampValidityDuration="00:05:00" cookieRenewalThresholdPercentage="60" />
                        <localServiceSettings detectReplays="true" issuedCookieLifetime="10:00:00"
                            maxStatefulNegotiations="128" replayCacheSize="900000" maxClockSkew="00:05:00"
                            negotiationTimeout="00:01:00" replayWindow="00:05:00" inactivityTimeout="00:02:00"
                            sessionKeyRenewalInterval="15:00:00" sessionKeyRolloverInterval="00:05:00"
                            reconnectTransportOnFailure="true" maxPendingSessions="128"
                            maxCachedCookies="1000" timestampValidityDuration="00:05:00" />
                        <secureConversationBootstrap />
                    </security>
                    <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
                        messageVersion="Soap12" writeEncoding="utf-8">
                        <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                            maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    </textMessageEncoding>
                    <httpsTransport manualAddressing="false" maxBufferPoolSize="524288"
                        maxReceivedMessageSize="65536" allowCookies="false" authenticationScheme="Anonymous"
                        bypassProxyOnLocal="false" decompressionEnabled="true" hostNameComparisonMode="StrongWildcard"
                        keepAliveEnabled="true" maxBufferSize="65536" proxyAuthenticationScheme="Anonymous"
                        realm="" transferMode="Buffered" unsafeConnectionNtlmAuthentication="false"
                        useDefaultWebProxy="true" requireClientCertificate="false" />
                </binding>
            </customBinding>
        </bindings>

由于

编辑:

在Adislav重播之后,我尝试了:

    // configure security properties
    AsymmetricSecurityBindingElement security = SecurityBindingElement.CreateMutualCertificateDuplexBindingElement(MessageSecurityVersion.Default);
    security.DefaultAlgorithmSuite = SecurityAlgorithmSuite.Basic256Sha256Rsa15;
    security.SecurityHeaderLayout = SecurityHeaderLayout.Lax;
    security.AllowSerializedSigningTokenOnReply = true;
    security.KeyEntropyMode = SecurityKeyEntropyMode.CombinedEntropy;
    security.MessageProtectionOrder = MessageProtectionOrder.SignBeforeEncrypt;
    security.RequireSignatureConfirmation = false;

    // configure encoding properties
    TextMessageEncodingBindingElement encoding = new TextMessageEncodingBindingElement();

    // configure transport properties
    HttpsTransportBindingElement transport = new HttpsTransportBindingElement();

    CustomBinding customBinding = new CustomBinding(security, encoding, transport);

但是这段代码不起作用。我错过了什么?

由于

1 个答案:

答案 0 :(得分:2)

您必须使用System.ServiceModel.Channels.CustomBinding类和相关的绑定元素(来自相同的命名空间):

  var security = SecurityBindingElement.CreateMutualCertificateDuplexBindingElement(MessageSecurityVersion.Default);
  // configure security properties
  var encoding = new TextMessageEncodingBindingElement();
  // configure encoding properties
  var transport = new HttpsTransportBindingElement();
  // configure transport properties
  var customBinding = new CustomBinding(security, encoding, transport);