在Windows服务主机中使用nettcpbinding无法发现wcf服务 [英] wcf service not discoverable with nettcpbinding in windows service host
本文介绍了在Windows服务主机中使用nettcpbinding无法发现wcf服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
hi;
我在Windows服务中托管了wcf并尝试使用nettcpbinding配置但是当我尝试添加服务时,该服务无法被发现,我的代码如下
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
推荐答案
您好Aly14,
根据代码详细信息,我发现发布元数据的问题在代码中。如果我们通过NET.TCP协议发布元数据,以及我们用于发布元数据的协议。
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";
}
}
结果。
如果有任何我可以帮忙的话,请随时告诉我。
Feel free to let me know if there is anything I can help with.
最诚挚的问候
亚伯拉罕
这篇关于在Windows服务主机中使用nettcpbinding无法发现wcf服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文