BlackFire27
BlackFire27

Reputation: 1550

I get an InvalidOperationException When Invoking a Service

The contract type HelloIndigo.Service 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.

I build a library class and referenced to the class in the console application.

The library class:

namespace HelloIndigo
{
  public class Service : IHelloIndigoService
  {
      public string HelloIndigo()
      {
        return "Hello Indigo";
      }
  }

  [ServiceContract(Namespace = "http://www.thatindigogirl.com/samples/2006/06")]
  interface IHelloIndigoService
  {
    [OperationContract]
    string HelloIndigo();
  }
}

The console application:

namespace Host
{
  class Program
  {
    static void Main(string[] args)
    {
      using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.Service), 
                                    new Uri("http://localhost:8000/HelloIndigo")))
      {
        host.AddServiceEndpoint(typeof(HelloIndigo.Service),
                                    new BasicHttpBinding(),"Service");
        host.Open();
        Console.WriteLine("Press enter to terminate the host service");
        Console.ReadLine();
      }
    }
  }
}

Upvotes: 3

Views: 2414

Answers (1)

Anders Abel
Anders Abel

Reputation: 69250

When you add the endpoint, you should supply the interface that is the contract:

host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
                                new BasicHttpBinding(),"Service");

Upvotes: 7

Related Questions