ServiceHost only supports class service types
Asked Answered
C

2

14

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?

Circumstantiality answered 25/3, 2012 at 19:57 Comment(1)
There is another answer to try @ https://mcmap.net/q/827818/-quot-servicehost-only-supports-class-service-types-quot/16391Supra
J
31

I suggest that you change this:

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

to this:

WebServiceHost host = new WebServiceHost(typeof(Service1));
Joyless answered 25/3, 2012 at 20:4 Comment(4)
I get this error if I do that: The contract type WcfService2.Service1 is not attributed with ServiceContractAttribute. In order to define a valid contract, the specified type (either contract interface or service class) must be attributed with ServiceContractAttribute.Circumstantiality
@Garrith: You shouldn't get that first error with the code posted. Your ramblings about deleting files may play a part in this.Pantin
Anyone who reads this answer please also see: #9864506 Becareful as WebServiceHost is Service1 and AddServiceEndpoint is IService1!Circumstantiality
how this can be an answer? It causes contract type WcfService2.Service1 is not attributed with ServiceContractAttribute error like @GarrithGraham saidCaroleecarolin
B
4

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

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

Read here for an example.

Brainstorm answered 25/3, 2012 at 20:4 Comment(1)
please see reply under Will Marcouliers answer.Circumstantiality

© 2022 - 2024 — McMap. All rights reserved.