简体   繁体   中英

WCF Discovery and DataService V3

I would like to expose discovery endpoints (both TCP and UDP) for my Data Services v3 and enable services to be discoverable from the client and discover them in another application. The main point in the discovery is to get the service endpoint address at the client.

I have tried to adapt the samples that Microsoft have provided for WCF Discovery , but so far I failed to achieve my goal. I have created a custom Data Service Host Factory on server side :

public class CustomDataServiceHostFactory : System.Data.Services.DataServiceHostFactory
{
    protected override System.ServiceModel.ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var serviceHost = base.CreateServiceHost(serviceType, baseAddresses);

        EndpointDiscoveryBehavior endpointDiscoveryBehavior = new EndpointDiscoveryBehavior();

        // Create XML metadata to add to the service endpoint
        XElement endpointMetadata = new XElement(
            "Root",
            new XElement("Information", "This endpoint is Data Service v3!"),
            new XElement("Time", System.DateTime.Now.ToString("MM/dd/yyyy HH:mm")));

        // Add the XML metadata to the endpoint discovery behavior.
        endpointDiscoveryBehavior.Extensions.Add(endpointMetadata);

        //may be this is not the safest way to set the behaviour
        foreach (var endpoint in serviceHost.Description.Endpoints)
        {
            endpoint.Behaviors.Add(endpointDiscoveryBehavior);
        }

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

        return serviceHost;
    }
}

On the client side I have tried the following code:

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

// Find service endpoints    
// ServiceReference.DataModel is the generated class for the Data Service client proxy       
FindCriteria findCriteria = new FindCriteria(typeof(ServiceReference.DataModel));
findCriteria.Duration = TimeSpan.FromSeconds(30);

FindResponse findResponse = discoveryClient.Find(findCriteria);

// Check to see if endpoints were found & print the XML metadata in them.
if (findResponse.Endpoints.Count > 0)
{
    foreach (XElement xElement in findResponse.Endpoints[0].Extensions)
    {
        Console.WriteLine("Printing Metadata from ServiceEndpoint:");
        Console.WriteLine("Endpoint Information: " + xElement.Element("Information").Value);
        Console.WriteLine("Endpoint Started at Time: " + xElement.Element("Time").Value);
        Console.WriteLine();
    }
}

Unfortunately this does not work. I get InvalidOperationException :

Attempted to get contract type for DataModel, but that type is not a ServiceContract, nor does it inherit a ServiceContract.

If I am heading in the right direction I need a way to express the type for the service contract for the discovery. Too bad I am not sure that it will even work like the normal WCF Discovery...

Please share your ideas or even better - working solutions.

I think exception message is clear enough. For service discovery You try to use type of your data model, while You must use type of your WCF service implementation - this is different things.

Basically DataServicesV3 service adapter uses your data model and exposes it as a WCF service with it's own service contract. Look at DataServiceV3 type declaration see that it is implementing some interface, i just don't remember name, in this interface declaration you will find [ServiceContract] and [ServiceOperation] attributes. This is Your SERVICE CONTRACT for all ancestors of DataServiceV3. They use THE SAME contract. Here stands another problem I haven't managed to solve yet - how to make WS-Discovery work with DataServices if they share same contract. You'd better dig in this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM