我一直在閱讀「學習WCF」一書,試圖讓我的頭腦創建和使用WCF Web服務。本書的第一部分通過創建並消費一個簡單的「Hello World」Web服務來引導讀者。客戶端使用WCF服務是否需要接口定義?
我遇到的問題是客戶端應用程序在調用它時似乎並不知道WCF接口的結構。如果可以使用任意數量的編程語言調用Web服務,這怎麼可能是必要的?
我在下面列出了相關的例子代碼...
下面是接口
[ServiceContract(Namespace="http: //www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
這裏的代碼爲承載網絡服務的服務代碼
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(HelloIndigo.HelloIndigoService) ,
new Uri("http: //localhost: 8000/HelloIndigo")))
{
host. AddServiceEndpoint(typeof(HelloIndigo. IHelloIndigoService) ,
new BasicHttpBinding(), "HelloIndigoService");
host. Open();
Console. WriteLine("Press <ENTER> to terminate the service host") ;
Console. ReadLine();
}
}
這裏是客戶端的代碼ap p消耗WCF服務 這是我困惑的地方。由於此客戶端應用程序可以是任何能夠調用Web服務的語言,爲什麼有必要了解接口的定義?
using System. ServiceModel;
[ServiceContract(Namespace = "http: //www.thatindigogirl.com/samples/2006/06")]
public interface IHelloIndigoService
{
[OperationContract]
string HelloIndigo();
}
static void Main(string[ ] args)
{
EndpointAddress ep = new
EndpointAddress("http: //localhost: 8000/HelloIndigo/HelloIndigoService") ;
IHelloIndigoService proxy = ChannelFactory<IHelloIndigoService>.
CreateChannel(new BasicHttpBinding(), ep);
string s = proxy. HelloIndigo();
Console. WriteLine(s) ;
Console. WriteLine("Press <ENTER> to terminate Client.") ;
Console. ReadLine();
}
我在這裏錯過了重要的一點嗎?也許我對消費網絡所需要的知識缺乏瞭解。我的目標是能夠使用WCF創建可以從任何編程語言或環境調用的.NET服務。
任何人都可以提出一個關於從.NET創建「跨平臺可使用」Web服務的好教程嗎?謝謝!
+1感謝您的解釋! – webworm 2010-12-15 16:41:51