更改 EndpointAddress 后发生 ActionNotSupportedException

ActionNotSupportedException occurred after change EndpointAddress

我正在使用 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>

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

问题出在哪里?

这很容易。 您必须生成与 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);