tags:

views:

44

answers:

2

I have a WCF REST service that works from a Windows service (.NET 3.5). To make it easier to build and debug, I would like to run it from a console. When I do this, I am setting up the endpoints in the console app. When I create an endpoint, it fails with this error: "The contract name 'IRestService' could not be found in the list of contracts implemented by the service 'System.RuntimeType'."

My interface DOES have [ServiceContract] attached to it:

namespace RestServiceLibrary
{
    [ServiceContract]
    public interface IRestService
    ...

Here is the console app:

namespace RestServiceConsole
{
    class Program
    {
        static void Main(string[] args)
        {

            WebServiceHost2 webHost = new WebServiceHost2(typeof(RestService), new Uri("http://localhost:8082"));
            ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(IRestService), new WebHttpBinding(), "");
            ServiceDebugBehavior stp = webHost.Description.Behaviors.Find<ServiceDebugBehavior>();
            stp.HttpHelpPageEnabled = false;
            webHost.Open();
            Console.WriteLine("Service is up and running");
            Console.WriteLine("Press enter to quit ");
            Console.ReadLine();
            webHost.Close();

        }
    }
}

Why am I getting this error? How can I fix it?

A: 

Try changing this line:

ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(IRestService), 
    new WebHttpBinding(), ""); 

To this:

ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(RestServiceLibrary.IRestService), 
    new WebHttpBinding(), ""); 

Sometimes it requires a fully qualified name.

http://aspdotnethacker.blogspot.com/2010/06/contract-name-could-not-be-found-in.html

Shiraz Bhaiji
Unless there is a conflicting IRestService interface in the console application, this shouldn't make any difference. This is a little different than using config files where you're reflecting over assemblies and you need to be more specific sometimes. With typeof() the compiler is going to take care of making sure the references are properly qualified.
Coding Gorilla
Shiraz - I had tried that as well, but that wasn't it. Thanks.
pc1oad1etter
+3  A: 

Instead of this line,

WebServiceHost2 webHost = new WebServiceHost2(typeof(RestService), new Uri("http://localhost:8082"));

it should be

WebServiceHost2 webHost = new WebServiceHost2(typeof(RestService), true, new Uri("http://localhost:8082"));

There are two constructors to WebServiceHost2, you are calling the one that is expecting a service instance. That's why it is looking for a contract in System.RuntimeType.

Darrel Miller
The dummy boolean got me. Thanks.
pc1oad1etter