2012-01-07 50 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

相關問題