【问题标题】:When hosting a WCF REST service in a console app, error finding contract name在控制台应用程序中托管 WCF REST 服务时,查找合同名称时出错
【发布时间】:2010-09-22 20:10:36
【问题描述】:

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

我的界面确实附有 [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();

        }
    }
}

为什么会出现此错误?我该如何解决?

【问题讨论】:

  • 请原谅我问的很明显,但是您的控制台应用程序中是否有对服务库 (.dll) 的引用?
  • 没什么太明显的。 :-) 我确实有参考。
  • RestService的定义是什么?

标签: .net wcf rest


【解决方案1】:

而不是这一行,

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 中寻找合约的原因。

【讨论】:

  • 虚拟布尔值得到了我。谢谢。
【解决方案2】:

尝试更改此行:

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

【讨论】:

  • 除非控制台应用程序中存在冲突的 IRestService 接口,否则这不会产生任何影响。这与使用配置文件来反映程序集有些不同,有时您需要更具体。使用 typeof() 编译器将负责确保引用正确限定。
  • Shiraz - 我也试过了,但不是这样。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-07-02
  • 1970-01-01
  • 2010-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多