none
wcf service not discoverable with nettcpbinding in windows service host RRS feed

  • Question

  • hi ;

    I hosted the wcf in windows service and try to configure using nettcpbinding but when I try to add service , the service not discoverable ,my code like below

                var baseAddress = new Uri("net.tcp://localhost:1586/CollectService");
                var selfHost = new ServiceHost(typeof(TransactionService), baseAddress);         

                  var binding = new NetTcpBinding();
                    binding.Security.Mode = SecurityMode.Message;            

                ServiceHost serviceHost = new ServiceHost(typeof(TransactionService), baseAddress);
                selfHost.AddServiceEndpoint(typeof(ITransactionService), binding, "TransactionServiceInstaller");

                ServiceMetadataBehavior serviceMetadataBehavior = new ServiceMetadataBehavior();
                serviceMetadataBehavior.HttpGetEnabled = true;
                serviceHost.Description.Behaviors.Add(serviceMetadataBehavior);
                selfHost.Open();
    Tuesday, November 13, 2018 10:30 PM

All replies

  • Hi Aly14,
    According to code details, I find a problem that publishes the metadata in the code. If we publish the metadata by NET.TCP protocol, as well as the protocol we used to publish the metadata.

    I have made a demo,wish it is useful to you.

    class Program
        {
            static void Main(string[] args)
            {
                var baseAddress = new Uri("net.tcp://localhost:1586/myservice");
                var binding = new NetTcpBinding();
                binding.Security.Mode = SecurityMode.Message;
                using (ServiceHost sh=new ServiceHost(typeof(MyService),baseAddress))
                {
                    sh.AddServiceEndpoint(typeof(IService),binding,"");
                    ServiceMetadataBehavior smb;
                    smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                    if (smb==null)
                    {
                        smb = new ServiceMetadataBehavior();
                        sh.Description.Behaviors.Add(smb);
                    }
                    Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
                    sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
    
                    sh.Open();
                    Console.WriteLine("Service is ready....");
    
                    Console.ReadLine();
                    sh.Close();
                }
            }
        }
    
        [ServiceContract]
        public interface IService
        {
            [OperationContract]
            string SayHello();
        }
        public class MyService : IService
        {
            public string SayHello()
            {
                return "Helo Stragner";
            }
        }

    Result.

    Feel free to let me know if there is anything I can help with.

    Best Regards

    Abraham


    Thursday, November 15, 2018 9:27 AM
    Moderator
  • service  started but wcf still not discoverable.
    Thursday, November 15, 2018 4:29 PM
  • Hi Aly14,
    Do you have added the following code to your project?
    Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
                    sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");

    we need to create Net/TCP address to publish the metadata and enable the ServiceMetadataBehavior. 
    Feel free to let me know if the problem still exists.
    Best Regards
    Abraham
    Friday, November 16, 2018 3:25 AM
    Moderator
  • I applied all that you published below code ,But we hosted wcf in Windows service not console application.

    var baseAddress = new Uri("net.tcp://localhost:1586/myservice");
               
    var binding = new NetTcpBinding();
                binding
    .Security.Mode = SecurityMode.Message;
               
    using (ServiceHost sh=new ServiceHost(typeof(MyService),baseAddress))
               
    {
                    sh
    .AddServiceEndpoint(typeof(IService),binding,"");
                   
    ServiceMetadataBehavior smb;
                    smb
    = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                   
    if (smb==null)
                   
    {
                        smb
    = new ServiceMetadataBehavior();
                        sh
    .Description.Behaviors.Add(smb);
                   
    }
                   
    Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
                    sh
    .AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");

                    sh
    .Open();
                    

               
    }
    I added above code in hosted wcf in wiondows service and I try to add reference in test client using

    this address (net.tcp://localhost:1586/myservice) but wcf not discoverable.

    Friday, November 16, 2018 7:12 AM
  • Hi Aly14,
    there is no problem the code seems to me, please refer to my following code.
     
     public partial class Service1 : ServiceBase
        {
            Uri uri = new Uri("net.tcp://localhost:6200/myservice");
    
            internal static ServiceHost sh = null;
            public Service1()
            {
                InitializeComponent();
            }
    
            protected override void OnStart(string[] args)
            {
                if (sh != null)
                {
                    sh.Close();
                }
    
                NetTcpBinding binding = new NetTcpBinding();
                binding.Security.Mode = SecurityMode.Message;
    
                sh = new ServiceHost(typeof(MyService), uri);
                sh.AddServiceEndpoint(typeof(IService), new NetTcpBinding(), "");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior();
                    sh.Description.Behaviors.Add(smb);
                }
                Binding mexbinding = MetadataExchangeBindings.CreateMexTcpBinding();
                sh.AddServiceEndpoint(typeof(IMetadataExchange), mexbinding, "mex");
                sh.Open();
                Log("Service is ready....");
    
            }
    
            protected override void OnStop()
            {
                if (sh != null)
                {
                    sh.Close();
                    sh = null;
                    Log("Service closed succesfully");
                }
            }
    }

    Installation

    Result.

    The following official document shows a How-to example using XML configuration.
    https://docs.microsoft.com/en-us/previous-versions/msp-n-p/ff649818(v=pandp.10)
    Feel free to let me know if there is anything I can help with.
    Best Regards
    Abraham
    Monday, November 19, 2018 6:05 AM
    Moderator
  • thank you so much , its working  , So I want to ask username and password when client adding to reference adress without ssl.
    Monday, November 19, 2018 8:09 AM
  • Hi,

    We maintain to discuss the same topic in a thread so that we can help other people who encountered the same problem. Based on the fact that your new question is a complex topic and there is little relation to the current issue, I suggest you post a new thread about your new question.

    Thanks for your understanding.

    Best Regards

    Abraham


    Tuesday, November 20, 2018 3:00 AM
    Moderator
  • I am sorry , I have to open new ticket But it was hard to explain for new users due to we follow this scenario.

    I have some questions related this post

    * how is better nettcp or http binding

    * is it possible to ask username and password when adding service to reference

    * please may you convert nettcpbinding configuration for web.config if wcf on IIS

    Thank you in advance.

    Tuesday, November 20, 2018 9:29 AM
  • Hi Aly14,

    No, please pay attention to your new question relations to authentication. You could find relate solution in the following links.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-authenticate-with-a-user-name-and-password
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/authentication-in-wcf
    >> how is better nettcp or http binding
    Different Bindings corresponds to different application scenarios, NettTcp binding is often used in the intranet, While the wshttpbinding is often used in the internet and more secure.
    Best Regards
    Abraham



    Wednesday, November 21, 2018 2:33 AM
    Moderator
  • Hi Abraham;

    you are missundertand me >

    * I dont ask to authantication in wcf , I ask to , I want to prevent wcf service adding reference ,when someone try to add I want ask username and password

    Wednesday, November 21, 2018 7:14 AM
  • Hi Aly14,
    If you don’t want someone could invoke the web service by adding service reference, you could disable metadata publishing via comment out the ServiceMetadataBehavior.
    <!--<serviceMetadata httpGetEnabled="true"/>-->
    In my limited knowledge, the question relates to the authentication in WCF. You should refer to the following document for your requirement.
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/how-to-authenticate-with-a-user-name-and-password
    https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/authentication-in-wcf
    Best Regards
    Abraham
    Thursday, November 22, 2018 2:31 AM
    Moderator