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. 我
我构建了一个库类,并在控制台应用程序中引用了该类. 图书馆类: 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"); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |