A.Learn
A.Learn

Reputation: 168

WCF: To and via uri must match error while using unique ListenUri and WCF discovery

I am trying to create a WCF service which uses httpBinding and I want unique Listen Uri.The client is using WCF discovery to detect service in discovery proxy.Protection level of Operation contract is set to None

I am getting an error in client saying to and via uri must be same:

Console Output:

Finding ICalculatorService endpoints...

Found 1 ICalculatorService endpoint(s).

Invoking CalculatorService at http://localhost/ac5271c3-14ea-45b0-8519-ab1c20f6bdac/
Using the viaUri http://localhost/ac5271c3-14ea-45b0-8519-ab1c20f6bdac/b5bb79cc-84a1-44f5-a913-b8928b51232a


Unhandled Exception: System.ArgumentException: The binding specified requires that the to and via URIs must match because the Addressing Ver
sion is set to None. The to URI specified was 'http://localhost/ac5271c3-14ea-45b0-8519-ab1c20f6bdac/'. The via URI specified was 'http://lo
calhost/ac5271c3-14ea-45b0-8519-ab1c20f6bdac/b5bb79cc-84a1-44f5-a913-b8928b51232a'.
   at System.ServiceModel.Channels.HttpChannelFactory`1.ValidateCreateChannelParameters(EndpointAddress remoteAddress, Uri via)
   at System.ServiceModel.Channels.HttpChannelFactory`1.OnCreateChannelCore(EndpointAddress remoteAddress, Uri via)
   at System.ServiceModel.Channels.ChannelFactoryBase`1.InternalCreateChannel(EndpointAddress address, Uri via)
   at System.ServiceModel.Channels.ServiceChannelFactory.ServiceChannelFactoryOverRequest.CreateInnerChannelBinder(EndpointAddress to, Uri v
ia)

Service:

Uri baseAddress = new Uri("http://localhost/" + Guid.NewGuid().ToString() + "/");
ServiceHost serviceHost = new ServiceHost(typeof(CalculatorService), baseAddress);

        try
        {
            ServiceEndpoint endpoint = serviceHost.AddServiceEndpoint(typeof(ICalculatorService), new BasicHttpBinding(), string.Empty);

            // Set the ListenUri mode to unique
            endpoint.ListenUriMode = ListenUriMode.Unique;

            // Make the service discoverable over UDP multicast
            serviceHost.Description.Behaviors.Add(new ServiceDiscoveryBehavior());
            serviceHost.AddServiceEndpoint(new UdpDiscoveryEndpoint());

            serviceHost.Open();

            Console.WriteLine("Calculator Service started at {0}", baseAddress);

            Console.ReadLine();
        }
        catch (CommunicationException e)
        {
            Console.WriteLine(e.Message);
        }

Client:

 DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

        Console.WriteLine("Finding ICalculatorService endpoints...");
        Console.WriteLine();

        FindCriteria findCriteria = new FindCriteria(typeof(ICalculatorService));
        findCriteria.Duration = TimeSpan.FromSeconds(5);

        // Find ICalculatorService endpoints            
        FindResponse findResponse = discoveryClient.Find(findCriteria);

        Console.WriteLine("Found {0} ICalculatorService endpoint(s).", findResponse.Endpoints.Count);
        Console.WriteLine();

        // Check to see if endpoints were found
        if (findResponse.Endpoints.Count > 0)
        {
            EndpointDiscoveryMetadata discoveredEndpoint = findResponse.Endpoints[0];

            // Check to see if the endpoint has a listenUri and if it differs from the Address URI
            if (discoveredEndpoint.ListenUris.Count > 0 && discoveredEndpoint.Address.Uri != discoveredEndpoint.ListenUris[0])
            {
                // Since the service is using a unique ListenUri, it needs to be invoked at the correct ListenUri 
                InvokeCalculatorService(discoveredEndpoint.Address, discoveredEndpoint.ListenUris[0]);
            }
            else
            {
                // Endpoint was found, however it doesn't have a unique ListenUri, hence invoke the service with only the Address URI
                InvokeCalculatorService(discoveredEndpoint.Address, null);
            }
        }

InvokeCalculatorService:

CalculatorServiceClient client = new CalculatorServiceClient(new BasicHttpBinding(), endpointAddress);
        Console.WriteLine("Invoking CalculatorService at {0}", endpointAddress.Uri);

    // if viaUri is not null then add the approprate ClientViaBehavior.
    if (viaUri != null)
    {
        client.Endpoint.Behaviors.Add(new ClientViaBehavior(viaUri));
        Console.WriteLine("Using the viaUri {0}", viaUri);
    }

    Console.WriteLine();

    double value1 = 100.00D;
    double value2 = 15.99D;

    // Call the Add service operation.
    double result = client.Add(value1, value2);
    Console.WriteLine("Add({0},{1}) = {2}", value1, value2, result);

Please help me solving above error

Upvotes: 0

Views: 501

Answers (1)

Tim
Tim

Reputation: 28530

This may be related to using basicHttpBinding, which does not use WS-addressing so there is no To address (see https://social.msdn.microsoft.com/Forums/en-US/8386191c-8bd5-49c7-8816-ddb04cb419c0/basichttpbinding-separating-logical-and-physical-address?forum=wcf).

If you don't have a pressing requirement to use basicHttpBinding, you can try using WsHttpBinding instead:

CalculatorServiceClient client = new CalculatorServiceClient(new WsHttpBinding(), endpointAddress);

Upvotes: 1

Related Questions