简体   繁体   English

带有异步服务的 WCF SharedInterface 定义

[英]WCF SharedInterface definition with Async Services

I have a problem with an Interface which defines my Services.我对定义我的服务的接口有问题。 I use the same interface in Silverlight and WPF and my Backend.我在 Silverlight 和 WPF 以及我的后端使用相同的界面。

For Example:例如:

#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

The problem is, I use the Interface in Silverlight (it is working good).问题是,我使用 Silverlight 中的接口(它运行良好)。 Now I also want to use the Async way in WPF, but I don't want to need to implement Begin and End on the Server side!现在我也想用WPF中的Async方式,但是又不想在Server端实现Begin和End! But my WPF Project is linking to the same DLL which implements this Interface!但是我的 WPF 项目正在链接到实现此接口的同一个 DLL!

Is there any way to achieve this in a easy way?有没有办法以简单的方式实现这一目标?

Luckily, in WCF you can have separate interfaces for client- and server-side implementations.幸运的是,在 WCF 中,您可以为客户端和服务器端实现提供单独的接口。 Your server-side implementation can be completely async while the client-side interface has a synchronous operation, and vice versa.您的服务器端实现可以是完全异步的,而客户端接口具有同步操作,反之亦然。 In your case I would create a derived interface for client-side async operations.在您的情况下,我将为客户端异步操作创建派生接口。 That way you serverside implementation can remain synchronous, while the client can implement the operations asynchronously.这样你的服务器端实现可以保持同步,而客户端可以异步实现操作。

[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);
}

Note that the name of the both service contracts must match, otherwise WCF won't be able to connect to the service.请注意,两个服务合同的名称必须匹配,否则 WCF 将无法连接到服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM