WCF - AddressFilter不匹配不匹配、WCF、AddressFilter

2023-09-04 01:04:47 作者:原来″只是伤不起

我有一种自承载WCF服务,我打电话时,得到了以下异常:

  

使用的消息的net.tcp://本地主机:53724 / Test1的不能   在接收器处进行处理时,由于一个错配AddressFilter在   EndpointDispatcher。检查发送者和接收者的   EndpointAddresses同意。

这是工作的解决方案是增加 [ServiceBehavior(AddressFilterMode = AddressFilterMode。preFIX)] 服务接口的实现类之前。但是,这不应该是这样的!所以,我试图让错误的来源,以便将其删除。

我发现, 当我加入 ServiceBehavior 属性和调用成功 - 以下内容: OperationContext.Current.EndpointDispatcher.EndpointAddress 返回:的net.tcp://本地主机/ Test1的 - 注意到没有港口。这实际上是我喂 ServiceHost.Open 方法。但是,端口将被加入,因为我指定 ListenUriMode.Unique

所以:我如何解决与 AddressFilterMode.Exact

错误

code复制:

  [的ServiceContract]
公共接口IWCF1
{
    [OperationContract的]
    布尔SendMessage函数(字符串消息);
}

[ServiceBehavior(AddressFilterMode = AddressFilterMode。preFIX)
公共类WCF1:IWCF1
{
    公共BOOL SendMessage函数(字符串消息)
    {
        的Debug.WriteLine(消息:+消息);
        的Debug.WriteLine(OperationContext.Current.EndpointDispatcher.EndpointAddress的EndpointAddress); //不包括港!
        返回true;
    }
}


公共无效测试()
{
    乌里uri2 =服务(typeof运算(WCF1)的typeof(IWCF1),测试1);
    IWCF1 iwcf1 = CreateChannel(uri2.ToString());
    新的任务(()=> iwcf1.SendMessage(ABC))。启动();
}

公众开放的服务(类型1级,类型接口1,串URI)
{
    字符串serviceUri分别=的net.tcp://本地主机/+ URI;
    ServiceHost的主机=新的ServiceHost(1级,新的URI(serviceUri分别));
    ServiceEndpoint EP = host.AddServiceEndpoint(接口1,新NetTcpBinding的(SecurityMode.None),serviceUri分别);
    ep.ListenUriMode = ListenUriMode.Unique;
    host.Open();
    返回host.ChannelDispatchers [0] .Listener.Uri;
}

公共静态IWCF1 CreateChannel(串地址)
{
    的EndpointAddress EP =新的EndpointAddress(地址);
    的ChannelFactory< IWCF1>的ChannelFactory =新的ChannelFactory< IWCF1>(新NetTcpBinding的(SecurityMode.None),EP);
    返回channelFactory.CreateChannel();
}
 
vlookup匹配不到数据的常见问题和解决方案

解决方案

我怀疑 AddressFilterMode.Exact 错误的根源涉及到逻​​辑的端点和物理之间的差异服务地址。

的EndpointAddress 是一种服务,它是一个SOAP消息给地址的逻辑地址。该 ListenUri 是服务的物理地址。它在服务端点实际上侦听当前计算机上消息的端口和地址信息。逻辑端点地址,而不是物理服务地址,由调度员匹配和滤波,从而为什么完全匹配没有找到该服务的原因使用。

的物理地址是不相同的逻辑地址:

 的net.tcp://本地主机:53724 / Test1的=的net.tcp://本地主机/ Test1的
 

有几个选项来考虑: 继续使用 AddressFilterMode。preFIX 尝试指定 HostNameComparisonMode 在预先指定的端口

参考: https://msdn.microsoft.com/en -us /库/ aa395210%28V = vs.110%29.aspx https://msdn.microsoft.com/en-us/magazine/cc163412的.aspx#S4

I have a self-hosted WCF service, and am getting the following exception when calling it:

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.

The solution that works is to add [ServiceBehavior(AddressFilterMode = AddressFilterMode.Prefix)] before the service interface's implementation class. But that shouldn't be the case! So, I'm trying to get to the source of the error in order to remove it.

I've found that When I do add the ServiceBehavior attribute and the call succeeds - the following: OperationContext.Current.EndpointDispatcher.EndpointAddress Returns: net.tcp://localhost/Test1 - notice the absence of the port. That is actually what I feed the ServiceHost.Open method. But the port is added because I specify ListenUriMode.Unique .

So: How do I fix the error with AddressFilterMode.Exact ?

Code to reproduce:

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

解决方案

I suspect that the source of the AddressFilterMode.Exact error involves the difference between the logical endpoint and physical service address.

The EndpointAddress is the logical address of a service, which is the address that SOAP messages are addressed to. The ListenUri is the physical address of the service. It has the port and address information where the service endpoint actually listens for messages on the current machine. The logical endpoint address, not the physical service location, is used for by the dispatcher for matching and filtering, thus the reason why an exact match does not find the service.

The Physical Address is not the same as the Logical Address:

  net.tcp://localhost:53724/Test1  !=  net.tcp://localhost/Test1  

A few options to consider: Continue to use AddressFilterMode.Prefix Try specifying HostNameComparisonMode Specify the port in advance

References: https://msdn.microsoft.com/en-us/library/aa395210%28v=vs.110%29.aspx https://msdn.microsoft.com/en-us/magazine/cc163412.aspx#S4