简体   繁体   English

如何检查网络TCP wcf服务是否已连接

[英]How to check net TCP wcf service is connected

Command to check whether net TCP wcf Windows service is running? 用于检查net TCP wcf Windows服务是否正在运行的命令?

I was trying with proxy to the wcf and getting timeout exception while calling a contract. 我正在尝试使用代理到wcf并在调用合同时获得超时异常。 So unable to confirm whether service is running or not 因此无法确认服务是否正在运行

The following ways could achieve you want. 以下方式可以实现您的需求。
1. Use the Netstat windows command to check whether the port is listening over TCP. 1.使用Netstat windows命令检查端口是否正在通过TCP侦听。
For example, Check whether port 1900 is listening over TCP. 例如,检查端口1900是否正在通过TCP侦听。 netstat的 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
2. OnlineAnnouncement/offlineAnnouncement notification by using UDP discovery endpoint. 2. OnlineAnnouncement / offline使用UDP发现端点发布通知。 When server is online, it could send online announcement by using UDP discovery endpoint. 当服务器在线时,它可以使用UDP发现端点发送在线通知。 I have made an example, wish it is useful to you. 我举了一个例子,希望它对你有用。
Server. 服务器。

class Program
    {
        static void Main(string[] args)
        {
            Uri uri = new Uri("net.tcp://localhost:1900");
            NetTcpBinding binding = new NetTcpBinding();
            using (ServiceHost sh=new ServiceHost(typeof(MyService),uri))
            {
                sh.AddServiceEndpoint(typeof(IService), binding, "");
                ServiceMetadataBehavior smb;
                smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
                if (smb == null)
                {
                    smb = new ServiceMetadataBehavior()
                    {
                    };
                    sh.Description.Behaviors.Add(smb);
                }
                ServiceDiscoveryBehavior sdb = new ServiceDiscoveryBehavior();
                sdb.AnnouncementEndpoints.Add(new UdpAnnouncementEndpoint());
                sh.Description.Behaviors.Add(sdb);
                sh.AddServiceEndpoint(new UdpDiscoveryEndpoint());

                Binding binding1 = MetadataExchangeBindings.CreateMexTcpBinding();
                sh.AddServiceEndpoint(typeof(IMetadataExchange), binding1, "mex");
                sh.Open();
                Console.WriteLine("Service is ready...");

                Console.ReadLine();
                sh.Close();
            }

        }
    }
    [ServiceContract(Namespace ="mydomain")]
    public interface IService
    {
        [OperationContract(Name ="AddInt")]
        int Add(int x, int y);

    }
    public class MyService : IService
    {
        public int Add(int x, int y)
        {
            return x + y;
        }
}

Client. 客户。

  static void Main(string[] args)
    {

        Console.Title = "Start client first";

        AnnouncementService annsvc = new AnnouncementService();
        annsvc.OnlineAnnouncementReceived += OnlineRec;
        annsvc.OfflineAnnouncementReceived += OfflineRec;
        using (ServiceHost host = new ServiceHost(annsvc))
        {
            host.AddServiceEndpoint(new UdpAnnouncementEndpoint());
            host.Open();
            Console.Read();
        }
    }

    private static void OfflineRec(object sender, AnnouncementEventArgs e)
    {
        Console.WriteLine($"\nService is offline, service address:{e.EndpointDiscoveryMetadata.Address.Uri}");
    }

    private static void OnlineRec(object sender, AnnouncementEventArgs e)
    {
        Console.WriteLine($"\nService is online, service address:{e.EndpointDiscoveryMetadata.Address.Uri}");
    }

Result. 结果。
UdpDiscoveryEndpoint
Official document. 官方文件。
https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dd483353(v=vs.100) https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dd483353(v=vs.100)
Actually, we could get the service status by the service metadata address. 实际上,我们可以通过服务元数据地址获取服务状态。 for the above example, we could detect the service status with inputting net.tcp://localhost:1900/mex in Adding service reference dialog. 对于上面的示例,我们可以通过在添加服务引用对话框中输入net.tcp:// localhost:1900 / mex来检测服务状态。
在此输入图像描述
3. Use notepad to log the status. 3.使用记事本记录状态。 Please refer to my example. 请参考我的例子。

Uri uri = new Uri("http://localhost:1000");
BasicHttpBinding binding = new BasicHttpBinding();
ServiceHost sh = null;

protected override void OnStart(string[] args)
{
    sh = new ServiceHost(typeof(MyService), uri);
    ServiceMetadataBehavior smb;
    smb = sh.Description.Behaviors.Find<ServiceMetadataBehavior>();
    if (smb==null)
    {
        smb = new ServiceMetadataBehavior
        {
            HttpGetEnabled = true
        };
        sh.Description.Behaviors.Add(smb);
    }
    sh.Open();
    WriteLog($"Service is ready at {DateTime.Now.ToString("hh-mm-ss")}");
}

protected override void OnStop()
{
    if (sh!=null&&sh.State==CommunicationState.Opened)
    {
        sh.Close();
        WriteLog($"Service is closed at {DateTime.Now.ToString("hh-mm-ss")}");
    }
}
public static void WriteLog(string text)
{
    using (StreamWriter sw=File.AppendText(@"D:\log.txt"))
    {
        sw.WriteLine(text);
        sw.Flush();
    }
}

Feel free to let me know if there is anything I can help with. 如果有什么我可以帮忙,请随时告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM