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

时间:2019-03-30 06:16:59

标签: c# wcf

检查网络TCP wcf Windows服务是否正在运行的命令?

我正在尝试代理wcf,并在调用合同时遇到超时异常。因此无法确认服务是否正在运行

1 个答案:

答案 0 :(得分:0)

以下方法可以实现您想要的。
1.使用Netstat windows命令检查端口是否正在通过TCP侦听。
例如,检查端口1900是否正在侦听TCP。 netstat https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat
2.通过使用UDP发现端点进行OnlineAnnouncement / offlineAnnouncement通知。 服务器处于联机状态时,可以使用UDP发现端点发送联机通知。 我举了一个例子,希望对您有用。
服务器。

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

客户。

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

结果。
UdpDiscoveryEndpoint
正式文件。
https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dd483353(v=vs.100)
实际上,我们可以通过服务元数据地址来获取服务状态。对于上面的示例,我们可以在“添加服务引用”对话框中输入net.tcp:// localhost:1900 / mex来检测服务状态。
enter image description here
3.使用记事本记录状态。请参考我的示例。

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

请随时告诉我是否有什么可以帮忙的。