开发者

When hosting a WCF REST service in a console app, error finding contract name

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 ca开发者_开发知识库n I fix it?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜