Kerwen
Kerwen

Reputation: 552

How to get the WCF service address from DynamicEndpoint

I Crreated a DynamicEndpoint to find the WCF service automatically.

namespace Client
{
    class Program
    {
        static void Main(string[] args)
        {
            DynamicEndpoint dynamicEndpoint = new DynamicEndpoint(ContractDescription.GetContract(typeof(ICalculator)), new NetTcpBinding());            
            using (ChannelFactory<ICalculator> channelFactory = new ChannelFactory<ICalculator>(dynamicEndpoint))
            {
                ICalculator caculate = channelFactory.CreateChannel();
                Console.WriteLine("x + y = {2} when x = {0} and y = {1}", 4, 9, caculate.Add(4, 9));
                Console.WriteLine("Find service, the service address is: " + dynamicEndpoint.Address.Uri);
            }
            Console.Read();
        }
    }
}

The problem is when I try to print the service address, the return value is

http://schemas.microsoft.com/discovery/dynamic

That's not the real service address I published.
1. How to get the real service address?
2. If there are multiple services available, which one will DynamicEndpoint choose? Can I get the address array or list?

Upvotes: 0

Views: 57

Answers (1)

Abraham Qian
Abraham Qian

Reputation: 7522

As far as I know, we could not get the actual use endpoint in client. except that we use the OperationContext object,which provides access to the execution context of a service method.
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.operationcontext?redirectedfrom=MSDN&view=netframework-4.7.2
For example, we could refer to the following code to get the actual endpoint.
Server.

public string GetAddress()
        {
            OperationContext oc = OperationContext.Current;
            string result=oc.Channel.LocalAddress.Uri.ToString();
            return result;

        }

Client.

ChannelFactory<IService> factory = new ChannelFactory<IService>(dynamicEndpoint);
            IService sv = factory.CreateChannel();

            Console.WriteLine(sv.GetAddress());

Besides,I don't think dynamic endpoint could list the endpoints that have been found. Dynamic Endpoint merge service discovery with service invokation. when a service is invoked using a dynamic endpoint, it will depend on the FindCriteria property to find the service endpoint and then invokes it.
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.discovery.dynamicendpoint?view=netframework-4.7.2
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.discovery.findcriteria?view=netframework-4.7.2

Upvotes: 1

Related Questions