3

合同类型 HelloIndigo.Service 不属于 ServiceContractAttribute。为了定义一个有效的契约,指定的类型(契约接口或服务类)必须用 ServiceContractAttribute 来赋予属性。

我构建了一个库类并在控制台应用程序中引用了该类。

图书馆类:

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();
  }
}

控制台应用程序:

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();
      }
    }
  }
}
4

1 回答 1

7

添加端点时,您应该提供作为合约的接口:

host.AddServiceEndpoint(typeof(HelloIndigo.IHelloIndigoService),
                                new BasicHttpBinding(),"Service");
于 2012-01-07T19:03:36.737 回答