Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WCF - AddressFilter mismatch

Tags:

c#

.net

wcf

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();
}
like image 879
ispiro Avatar asked Oct 19 '22 16:10

ispiro


1 Answers

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

like image 123
Seymour Avatar answered Nov 14 '22 23:11

Seymour