.net 在控制台应用程序中托管WCF REST服务时,查找合同名称时出错
我有一个从Windows服务(.NET 3.5)运行的WCF REST服务。为了更容易构建和调试,我想从控制台运行它。当我这样做时,我正在控制台应用程序中设置端点。当我创建端点时,它会失败,并出现以下错误: “在服务'System.RuntimeType'实现的协定列表中找不到协定名称'IRestService'。” 我的界面确实附带了[ServiceContract]:.net 在控制台应用程序中托管WCF REST服务时,查找合同名称时出错,.net,wcf,rest,.net,Wcf,Rest,我有一个从Windows服务(.NET 3.5)运行的WCF REST服务。为了更容易构建和调试,我想从控制台运行它。当我这样做时,我正在控制台应用程序中设置端点。当我创建端点时,它会失败,并出现以下错误: “在服务'System.RuntimeType'实现的协定列表中找不到协定名称'IRestService'。” 我的界面确实附带了[ServiceContract]: namespace RestServiceLibrary { [ServiceContract] publi
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();
}
}
}
命名空间RestServiceConsole
{
班级计划
{
静态void Main(字符串[]参数)
{
webservicehost2webhost=新的WebServiceHost2(typeof(RestService),新的Uri(“http://localhost:8082"));
ServiceEndpoint ep=webHost.AddServiceEndpoint(typeof(IRestService),新的WebHttpBinding(),“”);
ServiceDebugBehavior stp=webHost.Description.Behaviors.Find();
stp.HttpHelpPageEnabled=false;
webHost.Open();
Console.WriteLine(“服务已启动并运行”);
Console.WriteLine(“按enter键退出”);
Console.ReadLine();
webHost.Close();
}
}
}
为什么我会犯这个错误?如何修复它?尝试更改此行:
ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(IRestService),
new WebHttpBinding(), "");
为此:
ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(RestServiceLibrary.IRestService),
new WebHttpBinding(), "");
有时它需要一个完全限定的名称
尝试更改此行:
ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(IRestService),
new WebHttpBinding(), "");
为此:
ServiceEndpoint ep = webHost.AddServiceEndpoint(typeof(RestServiceLibrary.IRestService),
new WebHttpBinding(), "");
有时它需要一个完全限定的名称
而不是这一行
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中查找合约。而不是此行
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中寻找合同。请原谅我问一个显而易见的问题,但是您的控制台应用程序中是否有对服务库(.dll)的引用?没有太明显的内容。:-)我确实参考过它。
RestService
的定义是什么?请原谅我问了一个明显的问题,但是您的控制台应用程序中是否有对服务库(.dll)的参考?没有太明显的东西。:-)我确实引用过它。RestService
的定义是什么?除非控制台应用程序中存在冲突的IRestService接口,否则这不会有任何区别。这与使用配置文件略有不同,在配置文件中,您要对程序集进行反射,有时需要更加具体。对于typeof(),编译器将负责确保引用是正确限定的。谢谢。除非控制台应用程序中存在冲突的IRestService接口,否则这不会有任何区别。这与使用配置文件略有不同,在配置文件中,您要对程序集进行反射,有时需要更加具体。对于typeof(),编译器将负责确保引用是正确限定的。谢谢,我被那个笨蛋骗了。谢谢,我被那个笨蛋骗了。谢谢