ServiceHost only supports class service types

落爺英雄遲暮 提交于 2019-12-10 01:04:49

问题


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

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:

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.

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 application it 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.

Is there a way to fiddle with the host code?


回答1:


I suggest that you change this:

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

to this:

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



回答2:


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

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

Read here for an example.



来源:https://stackoverflow.com/questions/9863483/servicehost-only-supports-class-service-types

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!