改变EndpointAddress后发生notsupportedexception.处理步骤

本文关键字:处理 notsupportedexception EndpointAddress 改变 | 更新日期: 2023-09-27 17:51:16

我正在使用WCF和Silverlight。我想改变EndpointAddress与代码背后动态:

EndpointAddress endpointAdress = new EndpointAddress(serviceUrl);
var proxy = new ServerConnectionClient(context);
proxy.Endpoint.Address = endpointAdress;

连接打开成功,但是从服务调用方法后发生ActionNotSupportedException.

web . config :

<configuration>
  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="NetTcpBinding">
          <binaryMessageEncoding />
          <tcpTransport maxReceivedMessageSize="2147483647" maxBufferSize="2147483647" />
        </binding>
      </customBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://MyIpAddress:4502/engine/net" binding="customBinding"
        bindingConfiguration="NetTcpBinding" contract="CTMSConnection.IServerConnection"
        name="NetTcpBinding" />
    </client>
  </system.serviceModel>
</configuration>

添加业务引用后生成的以上配置。

问题在哪里?

改变EndpointAddress后发生notsupportedexception.处理步骤

这很简单。您必须生成与Web.Config 完全相同的代码。

必须使用下面的代码:

        System.ServiceModel.EndpointAddress endpointAddress = new System.ServiceModel.EndpointAddress("net.tcp://YourIpAddress:4502/CTMSEngine/net");
        System.ServiceModel.Channels.CustomBinding customBinding = new System.ServiceModel.Channels.CustomBinding();
        System.ServiceModel.Channels.BinaryMessageEncodingBindingElement BMEelement = new System.ServiceModel.Channels.BinaryMessageEncodingBindingElement();
        System.ServiceModel.Channels.TcpTransportBindingElement TcpTelement = new System.ServiceModel.Channels.TcpTransportBindingElement();
        customBinding.Elements.Add(BMEelement);
        customBinding.Elements.Add(TcpTelement);
        TcpTelement.MaxReceivedMessageSize = 2147483647;
        TcpTelement.MaxBufferSize = 2147483647;
        proxy = new ServerConnectionClient(customBinding, endpointAddress);