Search code examples
c#wcf

WCF - how programmatically set bindingconfiguration?


now in 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>

in client code:

InstanceContext context = new InstanceContext(
                        new ChatClient(numberclient);
DuplexChannelFactory<IFileChannel> factory =
                        new DuplexChannelFactory<IFileChannel>(context, "FileEndPoint");
IFileChannel channel = factory.CreateChannel();
                                        
channel.Open();

I try make so:

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

but "PeerTcpConfig" have custom address="net.tcp://191.14.3.11/FileServer" and have bindingConfiguration="TcpConfig"

how I can set for binding custom address in code and set one more binding TcpConfig for NetPeerTcpBinding binding


Solution

  • done work code so:

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