c# - ServiceHost 只支持类服务类型

标签 c# .net wcf exception-handling wcf-rest

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

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

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

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

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

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

但是运行后出现错误:

ServiceHost only supports class service types.

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

有没有办法修改主机代码?

最佳答案

我建议你改变这个:

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

为此:

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

关于c# - ServiceHost 只支持类服务类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9863483/

相关文章:

c# - 我们如何通过代码在数据 GridView 上多选和删除多行?

c# - 以集合作为输入多次调用 Action<T>

c# - 单独程序集中的 MVVM Light ViewModelLocator?设计时模式问题

c# - 跟踪同一应用程序的不同日志文件

c# - 找不到服务端点?

javascript - 无法逃脱撇号

c# - 从专有名称中提取通用名称

c# - 比较具有公差的字符串

c# - 如何访问有关 WCF 服务部署位置的信息

wcf - Jenkins 与 Visual Studio Online 集成认证