c# – WCF – AddressFilter不匹配
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – WCF – AddressFilter不匹配,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2584字,纯文字阅读大概需要4分钟。
内容图文
![c# – WCF – AddressFilter不匹配](/upload/InfoBanner/zyjiaocheng/779/b3aa51573aa7463bbd15508883169fa6.jpg)
我有一个自托管的WCF服务,并在调用它时收到以下异常:
The message with To ‘net.tcp://localhost:53724/Test1’ cannot be
processed at the receiver, due to an AddressFilter mismatch at the
EndpointDispatcher. Check that the sender and receiver’s
EndpointAddresses agree.
有效的解决方案是在服务接口的实现类之前添加[ServiceBehavior(AddressFilterMode = AddressFilterMode.Prefix)].但事实并非如此!所以,我试图找到错误的来源,以便删除它.
我发现了
当我添加ServiceBehavior属性并且调用成功时 – 以下内容:OperationContext.Current.EndpointDispatcher.EndpointAddress
返回:net.tcp:// localhost / Test1 – 注意没有端口.这实际上是我为ServiceHost.Open方法提供的.但添加端口是因为我指定了ListenUriMode.Unique.
那么:如何使用AddressFilterMode.Exact修复错误?
代码重现:
[ServiceContract]
public interface IWCF1
{
[OperationContract]
bool SendMessage(string message);
}
[ServiceBehavior(AddressFilterMode = AddressFilterMode.Prefix)]
public class WCF1 : IWCF1
{
public bool SendMessage(string message)
{
Debug.WriteLine("Message: " + message);
Debug.WriteLine(OperationContext.Current.EndpointDispatcher.EndpointAddress, "EndpointAddress");//Does not include the port!
return true;
}
}
public void test()
{
Uri uri2 = Service(typeof(WCF1), typeof(IWCF1), "Test1");
IWCF1 iwcf1 = CreateChannel(uri2.ToString());
new Task(() => iwcf1.SendMessage("abc")).Start();
}
public Uri Service(Type class1, Type interface1, string uri)
{
string serviceUri = "net.tcp://localhost/" + uri;
ServiceHost host = new ServiceHost(class1, new Uri(serviceUri));
ServiceEndpoint ep = host.AddServiceEndpoint(interface1, new NetTcpBinding(SecurityMode.None), serviceUri);
ep.ListenUriMode = ListenUriMode.Unique;
host.Open();
return host.ChannelDispatchers[0].Listener.Uri;
}
public static IWCF1 CreateChannel(string address)
{
EndpointAddress ep = new EndpointAddress(address);
ChannelFactory<IWCF1> channelFactory = new ChannelFactory<IWCF1>(new NetTcpBinding(SecurityMode.None), ep);
return channelFactory.CreateChannel();
}
解决方法:
我怀疑AddressFilterMode.Exact错误的来源涉及逻辑端点和物理服务地址之间的差异.
EndpointAddress是服务的逻辑地址,它是SOAP消息发送到的地址. ListenUri是服务的物理地址.它具有端口和地址信息,服务端点实际上侦听当前计算机上的消息.调度程序使用逻辑端点地址而不是物理服务位置进行匹配和过滤,因此精确匹配找不到服务的原因.
物理地址与逻辑地址不同:
net.tcp://localhost:53724/Test1 != net.tcp://localhost/Test1
一些选项需要考虑:
继续使用AddressFilterMode.Prefix
尝试指定HostNameComparisonMode
提前指定端口
参考文献:
https://msdn.microsoft.com/en-us/library/aa395210%28v=vs.110%29.aspx
https://msdn.microsoft.com/en-us/magazine/cc163412.aspx#S4
内容总结
以上是互联网集市为您收集整理的c# – WCF – AddressFilter不匹配全部内容,希望文章能够帮你解决c# – WCF – AddressFilter不匹配所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。