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

Upvotes: 0

Views: 736

Answers (2)

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

Upvotes: 0

sina ajilyan
sina ajilyan

Reputation: 41

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

First Step you Should Define netTcpBinding and a endPoint Then assign Your Custom Binding and Your EndPoint (You Should Use a Port For Connecting to Your Endpoint) after these simply Create ChannelFactory Object and Connect to Your Service. this code is working for a year in my project i just customize it to your code. if you have any question just ask.

Upvotes: 1

Related Questions