BlueIced
BlueIced

Reputation: 131

The contract name could not be found in the list of contracts implemented by the service

In the code below I get an error. I guess it is something simple or impossible to do, because I wasn't able to find anything with Google. Anyway the error I am getting is:

The contract name 'SchippersStop.Wcf.Balie.Interfaces.IBalie' could not be found in the list of contracts implemented by the service 'System.ServiceModel.Routing.RoutingService'.

The error occurs when I try to add a ServiceEndpoint to the host.

            ServiceHost host = new ServiceHost(typeof(RoutingService));
            string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

            foreach (string dll in Directory.GetFiles(path, "*.dll"))
            {
                Assembly assembly = Assembly.LoadFile(dll);
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.GetInterfaces().Contains(typeof(ISchippersStopSvc)))
                    {
                        DiscoveryClient discoveryClient = new DiscoveryClient(new UdpDiscoveryEndpoint());

                        FindCriteria findCriteria = new FindCriteria(type);
                        findCriteria.Duration = TimeSpan.FromSeconds(10);

                        FindResponse findResponse = discoveryClient.Find(findCriteria);

                        EndpointDiscoveryMetadata discoveryMetadata = findResponse.Endpoints.First();
                        Uri uri = discoveryMetadata.Address.Uri;
                        string routerAddress = uri.AbsoluteUri.Replace(uri.Host, "0.0.0.0");

                        DiscoveryClientBindingElement discoveryBindingElement = new DiscoveryClientBindingElement();
                        discoveryBindingElement.FindCriteria = findCriteria;
                        discoveryBindingElement.DiscoveryEndpointProvider = new UdpDiscoveryEndpointProvider();
                        CustomBinding binding = new CustomBinding();
                        binding.Elements.Insert(0, discoveryBindingElement);

                        ContractDescription contract = ContractDescription.GetContract(type);
                        RoutingConfiguration rc = new RoutingConfiguration();
                        rc.RouteOnHeadersOnly = true;

                        string groupName = Guid.NewGuid().ToString();
                        foreach (EndpointDiscoveryMetadata endpoint in findResponse.Endpoints)
                        {
                            List<ServiceEndpoint> endpointList = new List<ServiceEndpoint>();
                            ServiceEndpoint client = new ServiceEndpoint(contract, binding, new EndpointAddress(endpoint.Address.Uri.AbsoluteUri));
                            endpointList.Add(client);
                            endpointList.AddRange(findResponse.Endpoints.Except(new List<EndpointDiscoveryMetadata>() { endpoint }).Select(s => new ServiceEndpoint(contract, binding, new EndpointAddress(s.Address.Uri.AbsoluteUri))).ToList());
                            endpointList.ForEach(f => f.EndpointBehaviors.Add(new ClientTrackerEndpointBehavior() { }));
                            rc.FilterTable.Add(new CustomMessageFilter(groupName), endpointList);
                        }

                        RoutingBehavior routing = new RoutingBehavior(rc);
                        host.Description.Behaviors.Add(routing);

                        // Here the error occurs
                        host.AddServiceEndpoint(type, binding, routerAddress);
                    }
                }
            }

The goal is to add WCF services that are discovered on the network to the router. However I would like to set it up in such a way that I don't need to know anything about the services and the router can detect all the necessary configurations after which it can dynamically load them. The only thing I want to provide to the router is the ServiceContract. The router can detect the assemblies with a ServiceContract based on the common interface they inherit from.

Can someone help me to solve this error?

Upvotes: 0

Views: 1150

Answers (0)

Related Questions