没有端点监听
Posted
技术标签:
【中文标题】没有端点监听【英文标题】:There was no endpoint listening 【发布时间】:2014-12-05 13:21:57 【问题描述】:我正在尝试托管自己的 wcf 并远离 app.config 文件。我的 wcf 服务是我的 wpf 项目的一部分。主机代码是。
host = new ServiceHost(typeof(Service1), new Uri("http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/"));
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
smb = new ServiceMetadataBehavior();
BasicHttpBinding q = new BasicHttpBinding(BasicHttpSecurityMode.None);
q.CloseTimeout = new TimeSpan(1, 1, 0);
q.OpenTimeout = new TimeSpan(1, 1, 0);
q.ReceiveTimeout = new TimeSpan(1, 1, 0);
q.SendTimeout = new TimeSpan(1, 1, 0);
q.AllowCookies = false;
q.BypassProxyOnLocal = false;
q.MaxBufferSize = 2147483646;
q.MaxBufferPoolSize = 2147483646;
q.MaxReceivedMessageSize = 2147483646;
q.ReaderQuotas.MaxArrayLength = 2147483646;
q.ReaderQuotas.MaxBytesPerRead = 2147483646;
q.ReaderQuotas.MaxDepth = 2147483646;
q.ReaderQuotas.MaxNameTableCharCount = 2147483646;
q.ReaderQuotas.MaxStringContentLength = 2147483646;
host.AddServiceEndpoint(typeof(IService1), q, "");
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
host.Description.Behaviors.Add(smb);
host.AddServiceEndpoint(typeof(IService1), q, "http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/");
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
host.Open();
我得到错误的客户端代码是。
var ep = "http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/";
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.SendTimeout = new System.TimeSpan(0, 1, 30);
ChannelFactory<IService1> wcfFactory = new ChannelFactory<IService1>(binding, new EndpointAddress(ep));
IService1 wcf = wcfFactory.CreateChannel();
System.Net.ServicePointManager.DefaultConnectionLimit = 200;
wcf.generateId(System.Environment.MachineName);
我已经关闭了我的防火墙,所以这不是导致错误的原因。
关于我哪里出错的任何想法。
【问题讨论】:
您是否在同一台机器上运行客户端和服务器?如果不是,您需要将System.Net.Dns.GetHostName()
更改为服务器地址(在您的客户端代码中)。
是的,是同一台机器
首先我发现你的代码执行后有一些错误: 1. System.InvalidOperationException:需要一个ServiceDiscoveryBehavior来支持DiscoveryEndpoint。 2. System.InvalidOperationException:此服务有多个端点在“localhost:8733/DatabaseTransferWcfServiceLibaryMethod/Service1”监听,它们共享相同的启动操作“tempuri.org/IService1/GenerateId”
在您的 app.config 中,您已将定义的端点留在其中,这就是您收到此类错误的原因。
我不知道为什么,但自从我的电脑再次启动它就可以工作了。
【参考方案1】:
看看cmets。当我进行这两项更改时,一切正常
static void Main(string[] args)
var host = new ServiceHost(typeof(Service1), new Uri("http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/"));
ServiceMetadataBehavior smb = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
if (smb == null)
smb = new ServiceMetadataBehavior();
BasicHttpBinding q = new BasicHttpBinding(BasicHttpSecurityMode.None);
q.CloseTimeout = new TimeSpan(1, 1, 0);
q.OpenTimeout = new TimeSpan(1, 1, 0);
q.ReceiveTimeout = new TimeSpan(1, 1, 0);
q.SendTimeout = new TimeSpan(1, 1, 0);
q.AllowCookies = false;
q.BypassProxyOnLocal = false;
q.MaxBufferSize = 2147483646;
q.MaxBufferPoolSize = 2147483646;
q.MaxReceivedMessageSize = 2147483646;
q.ReaderQuotas.MaxArrayLength = 2147483646;
q.ReaderQuotas.MaxBytesPerRead = 2147483646;
q.ReaderQuotas.MaxDepth = 2147483646;
q.ReaderQuotas.MaxNameTableCharCount = 2147483646;
q.ReaderQuotas.MaxStringContentLength = 2147483646;
host.AddServiceEndpoint(typeof(IService1), q, "");
smb.HttpGetEnabled = true;
smb.MetadataExporter.PolicyVersion = PolicyVersion.Default;
host.Description.Behaviors.Add(smb);
// ADD ServiceDiscoveryBehavior
host.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
// COMMENT THIS LINE
host.AddServiceEndpoint(typeof(IService1), q, "http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/");
host.AddServiceEndpoint(new UdpDiscoveryEndpoint());
host.Open();
var ep = "http://" + System.Net.Dns.GetHostName() + ":8733/DatabaseTransferWcfServiceLibaryMethod/Service1/";
var binding = new BasicHttpBinding();
binding.Security.Mode = BasicHttpSecurityMode.None;
binding.SendTimeout = new System.TimeSpan(0, 1, 30);
ChannelFactory<IService1> wcfFactory = new ChannelFactory<IService1>(binding, new EndpointAddress(ep));
IService1 wcf = wcfFactory.CreateChannel();
System.Net.ServicePointManager.DefaultConnectionLimit = 200;
Console.WriteLine(wcf.GenerateId(System.Environment.MachineName));
【讨论】:
以上是关于没有端点监听的主要内容,如果未能解决你的问题,请参考以下文章
EndpointNotFoundException异常的可能原因是什么?