c# - 调用服务时出现 InvalidOperationException

标签 c# .net service invalidoperationexception

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

最佳答案

添加端点时,您应该提供作为契约(Contract)的接口(interface):

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

关于c# - 调用服务时出现 InvalidOperationException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8772138/

相关文章:

c# - 如何从 C# wpf 应用程序使用 F# 类型

c# - 异步使用 WinSCP .NET

c# - TPL 延续任务 : not sure who is the parent task

.net-2.0 - 为什么 Log4Net 在我的 Windows 服务中运行如此缓慢?

android - 在 sd 完成加载后设备启动时运行服务

c# - 如何在 C# 中执行 cmd,然后在同一窗口中执行另一个命令?

c# - .NET 或 Win32 中可重用的 "save credentials"对话框(如 IE 或 Vista 的)

c# - DropDownList对于没有 IEnumerable 类型的 ViewData 项

android - stopSelf() vs stopSelf(int) vs stopService(Intent)

c# - 检测我是否点击了文本的特定部分