2010-02-17 262 views
2

美好的一天人們。我從來沒有在這些類型的網站上發佈過,但是讓我們看看它是如何發展的。WCF服務客戶端

今天我第一次開始使用WCF,我看了幾個截屏,現在我準備跳入我的第一個實現它的解決方案。一切都很好,到目前爲止所有的工作,雖然我的問題來了,當我在我的調用程序/客戶端創建WCFServiceClient。

可以說我的ServiceContract /接口定義了我的方法暴露給客戶端,有許多方法每個都與某個實體對象有關。我怎樣才能將一個特定實體的所有相關方法邏輯分組在一起,以便在我的代碼中它看起來像

WCFServiceClient.Entity1.Insert(); 
WCFServiceClient.Entity1.Delete(); 
WCFServiceClient.Entity1.GetAll(); 
WCFServiceClient.Entity1.GetById(int id); 

WCFServiceClient.Entity2.AddSomething(); 
WCFServiceClient.Entity2.RemoveSomething(); 
WCFServiceClient.Entity2.SelectSomething(); 

...

而不是

WCFServiceClient.Insert(); 
WCFServiceClient.Delete(); 
WCFServiceClient.GetAll(); 
WCFServiceClient.GetById(int id); 
WCFServiceClient.AddSomething(); 
WCFServiceClient.RemoveSomething(); 
WCFServiceClient.SelectSomething(); 

我希望這是有道理的。我搜索谷歌,我已經嘗試了我自己的邏輯推理,但沒有運氣。任何想法,將不勝感激。

射擊 胡安

+0

歡迎!發佈代碼時,單擊工具欄上的格式代碼按鈕('101')以正確格式化。 – SLaks 2010-02-17 13:15:19

回答

0

WCFServiceClient.Entity1.Insert();

WCFServiceClient.Entity2.AddSomething();

這聞起來像兩個獨立的服務接口 - 讓每一個服務合同(接口)處理所有你需要一個單一的實體類型的方法:

[ServiceContract] 
interface IEntity1Services 
{ 
    [OperationContract] 
    void Insert(Entity1 newEntity); 
    [OperationContract] 
    void Delete(Entity1 entityToDelete); 
    [OperationContract] 
    List<Entity1> GetAll(); 
    [OperationContract] 
    Entity1 GetById(int id); 
} 

[ServiceContract] 
interface IEntity2Services 
{ 
    [OperationContract] 
    void AddSomething(Entity2 entity); 
    [OperationContract] 
    void RemoveSomething(Entity2 entity); 
    [OperationContract] 
    SelectSomething(Entity2 entity); 
} 

如果你願意,你可以有一個服務類實際上實現了兩個接口 - 這是完全可能的和有效的。

class ServiceImplementation : IEntity1Services, IEntity2Services 
{ 
    // implementation of all seven methods here 
} 

或者您可以創建兩個單獨的服務實現類 - 完全取決於您。

class ServiceImplementation1 : IEntity1Services 
{ 
    // implementation of four methods for Entity1 here 
} 

class ServiceImplementation2 : IEntity2Services 
{ 
    // implementation of three methods for Entity2 here 
} 

這有幫助嗎?

+0

是的,謝謝很多人。我昨天做的是創建一個部分類,每個類的實現實現不同的服務合同,併爲同一服務下的每個合同創建一個端點。 – Juan 2010-02-18 07:00:40

2

你不能真正做到這一點。你可以做的最好的做法是將所有的「實體1」方法放在一個服務合約中,而所有的「實體2」方法放在另一個服務合約中。單一服務可以實現多個服務合同。

+0

這將是我的下一個計劃,但是這意味着在我的客戶端,我將不得不在web.config中添加另一個參考Service2的權利? – Juan 2010-02-17 13:17:28

+0

@Juan:沒有。這是一項服務,有兩份合同。 – 2010-02-17 13:21:02

+0

好吧,我有點失落了。在WCF服務配置編輯器中,我將如何爲多個合同配置此服務? – Juan 2010-02-17 13:53:06