WCF: Net.TCP multiple bindings, same port, different IP Addresses

后端 未结 3 391
暖寄归人
暖寄归人 2020-12-29 12:40

I\'ve run into a problem. I\'m a little new at WCF so any help would be greatly appreaciated.

Here\'s my code:

public static void StartHosts()
    {
         


        
相关标签:
3条回答
  • 2020-12-29 13:15

    Thanks for the info Corazza!

    I've figured out how to accomplish this. I was going about this all the wrong way.

    My ultimate goal was to have the service listening on every IP Address available on the machine. Trying to bind to each address individually is the wrong way of doing this.

    Instead, I only needed to bind the service to the machine's Host Name (not 'localhost') and WCF automatically listens on all adapters.

    Here's the corrected code:

    public static void StartHosts()
        {
            try
            {
                // Formulate the uri for this host
                string uri = string.Format(
                    "net.tcp://{0}:{1}/ServerTasks",
                    Dns.GetHostName(),
                    ServerSettings.Instance.TCPListeningPort
                );
    
                // Create a new host
                ServiceHost host = new ServiceHost(typeof(ServerTasks), new Uri(uri));
    
                // Add the endpoint binding
                host.AddServiceEndpoint(
                    typeof(ServerTasks),
                    new NetTcpBinding(SecurityMode.Transport) 
                            { 
                                TransferMode = TransferMode.Streamed
                            },
                    uri
                );
    
                // Add the meta data publishing
                var smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                    smb = new ServiceMetadataBehavior();
    
                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                host.Description.Behaviors.Add(smb);
    
                host.AddServiceEndpoint(
                    ServiceMetadataBehavior.MexContractName,
                    MetadataExchangeBindings.CreateMexTcpBinding(),
                    "net.tcp://localhost/ServerTasks/mex"
                );
    
                // Run the host
                host.Open();
            }
            catch (Exception exc)
            {
                DebugLogger.WriteException(exc);
            }
        }
    
    0 讨论(0)
  • 2020-12-29 13:20

    Mel,

    While I've never tried this before myself, here's some samples to look at that I've heard before. You may want to create your binding object first and then add the same instance to the AddServiceEndpoint method, just a thought so you're not creating new bindings every time as I remember reading somewhere that netTCPBindings should be a 1:1 relationship with the address (even though you're using different addresses).

    I don't think you have to worry about port sharing as your opening up multiple ports.

    Here's a sample of what you may want to accomplish with multiple ports.

    http://www.aspfree.com/c/a/Windows-Scripting/WCF-and-Bindings/2/

    Here's a good sample for using portsharing on the NetTcpBinding.

    http://blogs.msdn.com/drnick/archive/2006/08/08/690333.aspx

    -Bryan

    0 讨论(0)
  • 2020-12-29 13:31

    Looks like i'm bit late :). But anyway - there is quite simple way to make Wcf listen all available network interfaces "net.tcp://0.0.0.0:8523/WCFTestService".

    0 讨论(0)
提交回复
热议问题