2010-09-22 57 views
1

我有一个WCF REST服务,它可以从Windows服务(.NET 3.5)中工作。为了更容易构建和调试,我想从控制台运行它。当我这样做时,我正在设置控制台应用程序中的端点。当我创建端点时,它会失败,并显示以下错误: “在服务'System.RuntimeType'实现的合同列表中找不到合同名'IRestService'。”在控制台应用程序中托管WCF REST服务时,发现合同名称错误

我的接口并[的ServiceContract]连接到它有:

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

这里是控制台应用程序:

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(); 

     } 
    } 
} 

为什么会出现这个错误?我该如何解决它?

+0

请原谅我的问题,但是您有没有在控制台应用程序中引用服务库(.dll)? – CodingGorilla 2010-09-22 20:13:28

+0

没什么太明显的。 :-)我有一个参考。 – pc1oad1etter 2010-09-22 20:19:35

+0

“RestService”的定义是什么? – 2010-09-22 20:19:42

回答

4

而是这条线的,

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

应该

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

有两个构造来WebServiceHost2,你拨打的期待服务实例之一。这就是为什么它正在寻找System.RuntimeType中的合同。

+0

虚拟布尔值给了我。谢谢。 – pc1oad1etter 2010-09-23 00:39:39

0

尝试修改此行:

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

要这样:

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

有时它需要一个完全合格的名称。

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

+0

除非控制台应用程序中存在冲突的IRestService接口,否则这应该没有任何区别。这与使用配置文件有点不同,在配置文件中你反映了程序集,有时你需要更具体。使用typeof()时,编译器会负责确保引用的合格性。 – CodingGorilla 2010-09-22 20:35:12

+0

设拉子 - 我也试过,但那不是。谢谢。 – pc1oad1etter 2010-09-23 00:39:11

相关问题