代码背后的Silverlight自定义绑定

时间:2014-01-03 12:36:55

标签: c# wcf silverlight wshttpbinding custom-binding

我见过很多关于WShttpbindingSilverlight的例子。此外,我成功地设法运行应用程序,但我必须从后面的代码创建自定义绑定。

我已尝试过,但收到如下错误:

In Binding 'MyCustom', TransportBindingElement 'HttpsTransportBindingElement' does not
appear last in the BindingElementCollection.  Please change the order of elements such
that the TransportBindingElement is last.

代码

public static MyTempService.ServiceClient WCFServiceClient()
{
    {       
        EndpointAddressBuilder AddressBuilder = new EndpointAddressBuilder(new EndpointAddress("https://node94.MyWab.local:8089/WcfService/Service.svc"));      

        CustomBinding Cusbinding = new CustomBinding
        ("MyCustom",
         "MyTempService",
         SecurityBindingElement.CreateUserNameOverTransportBindingElement(),
         new BinaryMessageEncodingBindingElement(),
         new HttpsTransportBindingElement()
        );

        Cusbinding.Elements.Add(new TextMessageEncodingBindingElement()
        {
            MessageVersion = System.ServiceModel.Channels.MessageVersion.Default,
            WriteEncoding = System.Text.Encoding.UTF8
        });

        Cusbinding.OpenTimeout = new TimeSpan(0, 5, 0);
        Cusbinding.CloseTimeout = new TimeSpan(0, 5, 0);
        Cusbinding.ReceiveTimeout = new TimeSpan(0, 5, 0);
        Cusbinding.SendTimeout = new TimeSpan(0, 5, 0);

        return new MyTempService.ServiceClient(Cusbinding, AddressBuilder.ToEndpointAddress());
    }
}

服务配置文件

<system.serviceModel>
    <diagnostics>
      <messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
      <endToEndTracing propagateActivity="true" activityTracing="true"
        messageFlowTracing="true" />
    </diagnostics>

    <behaviors>
      <serviceBehaviors>
        <behavior name="WsBehaviour">
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>

          <serviceCredentials>
            <userNameAuthentication userNamePasswordValidationMode="Custom"/>
          </serviceCredentials>

        </behavior>
      </serviceBehaviors>
    </behaviors>

    <bindings>
      <wsHttpBinding>
        <binding name="WsBinding">
          <security mode="Transport">
            <transport clientCredentialType="None" proxyCredentialType="None"
              realm="" />
            <message clientCredentialType="UserName" negotiateServiceCredential="false"
              algorithmSuite="Default" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

    <services>
      <service behaviorConfiguration="WsBehaviour" name="Service">
        <endpoint address="https://node94.MyWeb.local:8089/WcfService/Service.svc"
          binding="wsHttpBinding" bindingConfiguration="WsBinding" contract="IService">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

1 个答案:

答案 0 :(得分:0)

当我查看你的代码时,我看到BindingElementCollection中的最后一个是

TextMessageEncodingBindingElement()

以下列方式尝试:

1)从construtor

中删除新的HttpsTransportBindingElement()

2)将此元素添加为

之后的最后一个元素
    Cusbinding.Elements.Add(new TextMessageEncodingBindingElement()
    {
        MessageVersion = System.ServiceModel.Channels.MessageVersion.Default,
        WriteEncoding = System.Text.Encoding.UTF8
    });

    Cusbinding.Elements.Add(new HttpsTransportBindingElement());