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?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
G Gr
  • 6,030
  • 20
  • 91
  • 184

2 Answers2

31

I suggest that you change this:

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

to this:

WebServiceHost host = new WebServiceHost(typeof(Service1));
Will Marcouiller
  • 23,773
  • 22
  • 96
  • 162
  • 2
    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.` – G Gr Mar 25 '12 at 20:17
  • 1
    @Garrith: You shouldn't get that first error with the code posted. Your ramblings about deleting files may play a part in this. – H H Mar 25 '12 at 20:28
  • 4
    Anyone who reads this answer please also see: http://stackoverflow.com/questions/9864006/servicecontractattribute-error-on-simple-wcf-service Becareful as WebServiceHost is Service1 and AddServiceEndpoint is IService1! – G Gr Mar 25 '12 at 21:33
  • how this can be an answer? It causes `contract type WcfService2.Service1 is not attributed with ServiceContractAttribute` error like @GarrithGraham said – oleksa Feb 14 '19 at 11:46
4

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

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

Read here for an example.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294