2012-01-07 54 views
3

契约类型HelloIndigo.Service不归属于 ServiceContractAttribute。为了定义有效的合同,指定类型(合约界面或服务类别)必须为 ,且归属于ServiceContractAttribute。当调用一个服务时,我得到一个InvalidOperationException

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

图书馆类:

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

请支付一些注意你的问题的格式。在写问题时有一个预览,在发布之前查看它。 (我已经为你解决了这个问题)。 – 2012-01-07 19:06:10

回答

7

当您添加端点,您应该提供属于合同接口:

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

Thanks..it正在工作.. – BlackFire27 2012-01-07 19:08:27

相关问题