Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
      }
    }
  }
}
like image 830
BlackFire27 Avatar asked Nov 18 '25 06:11

BlackFire27


1 Answers

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

host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
                                new BasicHttpBinding(),"Service");
like image 182
Anders Abel Avatar answered Nov 19 '25 20:11

Anders Abel