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

时间:2016-02-15 06:16:36

标签: c# wpf wcf app-config endpoint

我需要在c#中以编程方式编写此结尾proint,这适用于聊天应用程序。

app.config部分

 <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();

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

1 个答案:

答案 0 :(得分:0)

使用端点选择逻辑Wright自己的ChanelFactory

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

或者只使用DuplexChanelFactory的另一个构造函数来获取EndPointAddress对象,您可以在代码中创建所有必需参数

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