C# 来自CodeBehind的Silverlight自定义绑定
我看到了很多关于C# 来自CodeBehind的Silverlight自定义绑定,c#,wcf,silverlight,wshttpbinding,custom-binding,C#,Wcf,Silverlight,Wshttpbinding,Custom Binding,我看到了很多关于WShttpbinding和Silverlight的例子。此外,我成功地运行了应用程序,但我必须从代码隐藏中创建自定义绑定 我已经试过了,但出现如下错误: In Binding 'MyCustom', TransportBindingElement 'HttpsTransportBindingElement' does not appear last in the BindingElementCollection. Please change the order of elem
WShttpbinding
和Silverlight
的例子。此外,我成功地运行了应用程序,但我必须从代码隐藏中创建自定义绑定
我已经试过了,但出现如下错误:
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>
当我查看您的代码时,我看到BindingElement集合中的最后一个是 TextMessageEncodingBindingElement() 请按以下方式尝试: 1) 从构造函数中删除新的HttpsTransportBindingElement() 2) 添加此元素作为后面的最后一个元素
Cusbinding.Elements.Add(new TextMessageEncodingBindingElement()
{
MessageVersion = System.ServiceModel.Channels.MessageVersion.Default,
WriteEncoding = System.Text.Encoding.UTF8
});
Cusbinding.Elements.Add(new HttpsTransportBindingElement());