WCF - Как вынести bindingConfiguration из app.config в код?

есть сервер с CustomPeerResolverService

и есть клиенты в сети P2P

с установками в app.config все работает, но необходимо не постоянное подключение к динамически создаваемым адресам

сейчас у клиента в 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();                    
                    

необходимо вынести из кофига всё в код, чтобы можно было указывать при создании, вместо ChangingName123 - любое другое имя в любой момент времени, чтобы не было привязки к одной сети из-за статичного конфига.

пробую выносить в код клиента:

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

не могу понять как для данного binding в коде установить bindingConfiguration="PeerTcpConfig"

у PeerTcpConfig есть custom address="net.tcp://191.14.3.11/FileServer - как его прописать в коде, не понимаю, так же у него есть в конфиге binding - TcpConfig - и как его в коде добавить


Ответы (1 шт):

Автор решения: Архипов Владимир

не нашёл нигде примеров, получилось более внимательно всмотреться в код, сделал решение, которое работает:

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();
→ Ссылка