【问题标题】:WCF SharedInterface definition with Async Services带有异步服务的 WCF SharedInterface 定义
【发布时间】:2013-06-25 12:06:59
【问题描述】:

我对定义我的服务的接口有疑问。我在 Silverlight 和 WPF 以及我的后端中使用相同的界面。

例如:

#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中使用Async方式,但是我不想在Server端实现Begin和End!但我的 WPF 项目正在链接到实现此接口的同一个 DLL!

有什么方法可以轻松实现吗?

【问题讨论】:

    标签: c# wpf wcf asynchronous


    【解决方案1】:

    幸运的是,在 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 将无法连接到服务。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-03
      • 2011-05-04
      • 1970-01-01
      相关资源
      最近更新 更多