如何以编程方式编写端点(netPeerTcpBinding)

本文关键字:端点 netPeerTcpBinding 方式编 编程 | 更新日期: 2023-09-27 18:34:32

我需要用c#以编程方式编写这个结束,这是用于聊天应用程序的。

应用程序配置部分

 <system.serviceModel>
        <client>
          <endpoint name="ChatEndPoint" address="net.p2p://chatMesh/ChatServer" binding="netPeerTcpBinding" bindingConfiguration="PeerTcpConfig" contract="Test.IChatService"></endpoint>
        </client>
        <bindings>
          <netPeerTcpBinding>
            <binding name="PeerTcpConfig" port="0">
              <security mode="None"></security>
              <resolver mode="Custom">
                <custom address="net.tcp://192.168.0.147:22222/ChatServer" binding="netTcpBinding" bindingConfiguration="TcpConfig"></custom>
              </resolver>
            </binding>
            <!--<binding name="BindingDefault" port="0">
              <security mode="None"></security>
              <resolver mode="Auto"></resolver>
            </binding>-->
          </netPeerTcpBinding>
          <netTcpBinding>
            <binding name="TcpConfig">
              <security mode="None"></security>
            </binding>
          </netTcpBinding>
        </bindings>
      </system.serviceModel>

C#部分

 InstanceContext context = new InstanceContext(
                        new Home(txtUserName.Text.Trim()));
                    factory =
                        new DuplexChannelFactory<IChatChannel>(context, "ChatEndPoint");
                    channel = factory.CreateChannel();

这对我来说很好用。但是,我需要动态更改端点地址。寻求宝贵的支持。

如何以编程方式编写端点(netPeerTcpBinding)

使用端点选择逻辑构建您自己的香奈儿工厂

https://msdn.microsoft.com/en-us/library/ms734681.aspx。

或者只是使用DuplexChanelFactory的另一个构造函数,它采用EndPointAddress对象,您可以使用所有必要的参数在代码中创建该对象。

https://msdn.microsoft.com/ru-ru/library/ms576164(v=vs.110(.aspx