2013-06-25 52 views
0

我有一個界面問題,它定義了我的服務。我在Silverlight和WPF以及我的後端使用相同的接口。WCF與異步服務的SharedInterface定義

例如:

#if !SILVERLIGHT 
    [OperationContract(IsOneWay = false)] 
    SecurityOperationInfo LogonUser(string sessionId, string username, string password); 
#else 
    [OperationContract(IsOneWay = false, AsyncPattern = true)] 
    IAsyncResult BeginLogonUser(string sessionId, string username, string password, AsyncCallback callback, object state);   
    SecurityOperationInfo EndLogonUser(IAsyncResult result); 
#endif 

現在的問題是,我使用在Silverlight的界面(它是工作良好)。現在我也想在WPF中使用異步方式,但我不想在服務器端實現開始和結束!但我的WPF項目鏈接到實現此接口的同一個DLL!

有什麼辦法以簡單的方式實現這一點?

回答

0

幸運的是,在WCF中,您可以爲客戶端和服務器端實現提供單獨的接口。您的服務器端實現可以完全異步,而客戶端接口具有同步操作,反之亦然。 在你的情況下,我會爲客戶端異步操作創建派生接口。這樣您的服務器端實現可以保持同步,而客戶端可以異步實現這些操作。

[ServiceContract(Name = "IMyService", ...)] 
public interface IMyService { 
    [OperationContract(IsOneWay=false)] 
    SecurityOperationInfo LogonUser(string sessionId, string username, string password); 

    // other methods ... 
} 

[ServiceContract(Name = "IMyService", ...)] 
public interface IMyServiceAsync : IMyService { 
    [OperationContract(IsOneWay = false, AsyncPattern = true)] 
    IAsyncResult BeginLogonUser(string sessionId, string username, string password, AsyncCallback callback, object state);   
    SecurityOperationInfo EndLogonUser(IAsyncResult result); 
} 

請注意,兩個服務合同的名稱必須匹配,否則WCF將無法連接到該服務。

+0

正是我所搜索的...不知道爲什麼我不這樣做之前... –