用于检查net TCP wcf Windows服务是否正在运行的命令?
我正在尝试使用代理到wcf并在调用合同时获得超时异常。因此无法确认服务是否正在运行
以下方式可以实现您的需求。 1.使用Netstat windows命令检查端口是否正在通过TCP侦听。 例如,检查端口1900是否正在通过TCP侦听。 https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/netstat 2. OnlineAnnouncement / offline使用UDP发现端点发布通知。当服务器在线时,它可以使用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}");
}
结果。 官方文件。 https://docs.microsoft.com/en-us/previous-versions/dotnet/netframework-4.0/dd483353(v=vs.100) 实际上,我们可以通过服务元数据地址获取服务状态。对于上面的示例,我们可以通过在添加服务引用对话框中输入net.tcp:// localhost:1900 / mex来检测服务状态。 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();
}
}
如果有什么我可以帮忙,请随时告诉我。