ServiceHost仅支持类服务类型

时间:2012-03-25 19:57:06

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

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

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

然后我有我的公共类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仅支持类服务类型。

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

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

2 个答案:

答案 0 :(得分:29)

我建议你改变这个:

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

到此:

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

答案 1 :(得分:4)

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

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

阅读here以获取示例。