C# WCF-如何以编程方式设置bindingconfiguration?

C# WCF-如何以编程方式设置bindingconfiguration?,c#,wcf,C#,Wcf,现在在client app.config中: <system.serviceModel> <client> <endpoint name="FileEndPoint" address="net.p2p://ChangingName123/FileServer" binding="netPeerTcpBinding" bindingConfiguration

现在在client app.config中:

<system.serviceModel>
    <client>
      <endpoint name="FileEndPoint" address="net.p2p://ChangingName123/FileServer"
                binding="netPeerTcpBinding" bindingConfiguration="PeerTcpConfig"
                contract="FileClient.IFileService"></endpoint>
     
      
   </client>

    <bindings>
      <netPeerTcpBinding>
        <binding name="PeerTcpConfig" port="0">
          <security mode="None"></security>
          <resolver mode="Custom">
            <custom address="net.tcp://191.14.3.11/FileServer" binding="netTcpBinding"
                    bindingConfiguration="TcpConfig"></custom>
          </resolver>
        </binding>
        
      </netPeerTcpBinding>
      <netTcpBinding>
        <binding name="TcpConfig">
          <security mode="None"></security>
        </binding>
      </netTcpBinding>
    </bindings>
  </system.serviceModel>


在客户端代码中:

InstanceContext context = new InstanceContext(
                        new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
                        new DuplexChannelFactory<IFileChannel>(context, "FileEndPoint");
IFileChannel channel = factory.CreateChannel();
                                        
channel.Open();
InstanceContext上下文=新建InstanceContext(
新聊天客户端(号码客户端);
双工厂=
新的DuplexChannelFactory(上下文,“FileEndPoint”);
IFileChannel=factory.CreateChannel();
通道打开();
我试着这样做:

NetPeerTcpBinding binding = new NetPeerTcpBinding();
            
EndpointAddress endpoint = new EndpointAddress("net.p2p://ChangingName123/FileServer");

InstanceContext context = new InstanceContext(
                        new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
                        new DuplexChannelFactory<IFileChannel>(context, binding, endpoint);
NetPeerTcpBinding binding=newnetpeertcpbinding();
EndpointAddress endpoint=新的EndpointAddress(“net.p2p://ChangingName123/FileServer”);
InstanceContext上下文=新InstanceContext(
新聊天客户端(号码客户端);
双工厂=
新的DuplexChannelFactory(上下文、绑定、端点);
但是“PeerTcpConfig”有
customaddress=“net”。tcp://191.14.3.11/FileServer“
和have
bindingConfiguration=“TcpConfig”

如何在代码中设置绑定自定义地址并为NetPeerTcpBinding绑定设置一个绑定TcpConfig;
  private NetTcpBinding binding = new NetTcpBinding();
  private EndpointAddress endPoint;

  private void initial_binding()
    {
        binding.Security.Mode = SecurityMode.None;
        
        binding.HostNameComparisonMode = HostNameComparisonMode.WeakWildcard;
       
        binding.ReliableSession.Enabled = false;
    }

initial_binding();
endPoint= new EndpointAddress(@"net.tcp://" + 191.14.3.11+ @":4000/FileServer");


 var IAChannelFactory = new ChannelFactory<FileClient.IFileService>(binding, 
 endpoint);
            IFileService Client = new FileService();
            try
             {
                client.anyFunction();
             }
            catch (Exception ex)
             {
                Logger.Log.Error(ex.Message);
             }
私有端点地址端点; 私有无效初始绑定() { binding.Security.Mode=SecurityMode.None; binding.HostNameComparisonMode=HostNameComparisonMode.WeakWildcard; binding.ReliableSession.Enabled=false; } 初始绑定(); 端点=新的端点地址(@“net.tcp://“+191.14.3.11+@”:4000/文件服务器); var IAChannelFactory=新的ChannelFactory(绑定, 终点); IFileService Client=新文件服务(); 尝试 { client.anyFunction(); } 捕获(例外情况除外) { Logger.Log.Error(例如消息); } 第一步,您应该定义netTcpBinding和端点,然后分配自定义绑定和端点(您应该使用端口连接到端点)完成这些操作后,只需创建ChannelFactory对象并连接到您的服务。此代码在我的项目中使用了一年,我只需将其自定义为您的代码。如果您有任何问题,请提问。

完成工作代码,因此:

InstanceContext context = new InstanceContext(new ChatClient(numberclient));
NetPeerTcpBinding binding = new NetPeerTcpBinding();

EndpointAddress endpoint = new EndpointAddress("net.p2p://ChangingName123/FileServer");
binding.Resolver.Custom.Address = new EndpointAddress("net.tcp://191.14.3.11/FileServer");
binding.Security.Mode = SecurityMode.None;
NetTcpBinding ntcp = new NetTcpBinding();
ntcp.Security.Mode = SecurityMode.None;
binding.Resolver.Custom.Binding = ntcp;
factory = new DuplexChannelFactory<IChatChannel>(context, binding, endpoint);
channel = factory.CreateChannel();
InstanceContext context=newinstanceContext(newchatclient(numberclient));
NetPeerTcpBinding binding=新的NetPeerTcpBinding();
EndpointAddress endpoint=新的EndpointAddress(“net.p2p://ChangingName123/FileServer”);
binding.Resolver.Custom.Address=新端点地址(“网络地址”)。tcp://191.14.3.11/FileServer");
binding.Security.Mode=SecurityMode.None;
NetTcpBinding ntcp=新的NetTcpBinding();
ntcp.Security.Mode=SecurityMode.None;
binding.Resolver.Custom.binding=ntcp;
工厂=新的DuplexChannelFactory(上下文、绑定、端点);
channel=factory.CreateChannel();

您可以参考:在本文中,app.config中有多个端点,但需要示例如何在ask中生成代码和代码,您可以看到一个端点的多个绑定在ru讨论中首先发布此问题,找到解决方案我做了另一个,thaks寻求答案