加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 调用服务时出现InvalidOperationException

发布时间:2020-12-16 01:50:29 所属栏目:百科 来源:网络整理
导读: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. 我

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.

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

图书馆类:

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

解决方法

添加端点时,应提供合同接口:

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读