C# ServiceHost 仅支持类服务类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9863483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 10:59:10  来源:igfitidea点击:

ServiceHost only supports class service types

c#.netwcfexception-handlingwcf-rest

提问by G Gr

I have a service named WcfService2 (original i know) which has an IService.cs file with a public interface:

我有一个名为 WcfService2(我知道原来的)的服务,它有一个带有公共接口的 IService.cs 文件:

namespace WcfService2
{
    [ServiceContract]
    public interface IService1
    {    
        [OperationContract]
        [WebGet(UriTemplate = "/{value}")]
        string GetData(string value);            
    }
}

I then have my public class Service1.svc.cs file which returns a string for the value like so:

然后我有我的公共类 Service1.svc.cs 文件,它返回一个字符串,如下所示:

namespace WcfService2
{
    public class Service1 : IService1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

I am now trying to host this service with a console app like so:

我现在正在尝试使用控制台应用程序托管此服务,如下所示:

namespace Host
{
    class Program
    {
        static void Main(string[] args)
        {
            WebHttpBinding binding = new WebHttpBinding();
            WebServiceHost host =
            new WebServiceHost(typeof(IService1));
            host.AddServiceEndpoint(typeof(IService1),
            binding,
            "http://localhost:8000/Hello");
            host.Open();
            Console.WriteLine("I DONT LIKE REST!");
            Console.WriteLine("Press <RETURN> to KILL REST FOR GOOD");
            Console.ReadLine();
        }
    }
}

But I get an error after I run it:

但是我运行后报错:

ServiceHost only supports class service types.

ServiceHost 仅支持类服务类型。

So this obviously relates to my IService being of public interface type. But I dont know how else to create it, when I first created the WCF Service applicationit gives you the two standard files IService and Service.svc files if I delete either or, and only Implement this solution in one class when I try to add the web service in local soultion nothing is found.

所以这显然与我的 IService 是公共接口类型有关。但是我不知道如何创建它,当我第一次创建它时,WCF Service application它会为您提供两个标准文件 IService 和 Service.svc 文件,如果我删除或删除,并且仅在我尝试添加 Web 服务时在一个类中实现此解决方案在当地没有找到任何东西。

Is there a way to fiddle with the host code?

有没有办法摆弄主机代码?

采纳答案by Will Marcouiller

I suggest that you change this:

我建议你改变这个:

WebServiceHost host = new WebServiceHost(typeof(IService1));

to this:

对此:

WebServiceHost host = new WebServiceHost(typeof(Service1));

回答by Joachim Isaksson

You should create the WebServiceHost with the class implementing the service;

您应该使用实现服务的类创建 WebServiceHost;

WebServiceHost host = new WebServiceHost(typeof(Service1));

Read herefor an example.

阅读此处获取示例。