在silverlight上异步调用同步WCF操作合约方法

问题描述:

我们正在使用ChanellFacotry创建代理,从而在Silverlight应用程序上使用wcf服务。在silverlight上异步调用同步WCF操作合约方法

操作和数据合约暴露给silverlight槽组件,该组合由来自服务器端数据和操作合约libriary的共享文件组成。 (omg我希望你明白我在说什么)。

因此服务器和客户端正在使用相同的操作和数据合同。

Silverlight wcf客户端lib有限制,无法同步调用wcf方法,正如您所知,因此共享操作协定文件必须公开每个操作的异步版本。

如果不使用阻塞操作,编写异步WCF服务会有所帮助,但由于我们使用的是EF,所以通过将阻塞工作委托给线程池来实现异步。无论如何,这是WCF为同步方法所做的。这个事实让我想把我的眼睛掏出来(#@%!^%!@%)。

我们的项目顾问有权不允许客户端上生成动态代理调用同步操作合同法(谷歌耶夫亨博布罗夫Servelat件,如果你有兴趣)。所以我们必须在服务器端编写异步方法的senseles实现,没有任何性能增益(如你记得的那样阻塞调用)。

是否有可能使用其数据契约从silverlight调用wcf Web服务同步方法?

你有没有曾经遇到过这个问题,如果是的话,你是如何解决它的?

在我期待为客户端生成异步合约的情况下,只使用服务器端合约作为转换源。也许有一些t4模板可以很好地为我做?

对不起,文字的墙壁,只是一些代码混合到我的问题,这是异步合同执行如何着眼于momment:

/// <summary> 
    /// Subscribes to users of the specified organization. 
    /// </summary> 
    /// <param name="organizationId">The organization id.</param> 
    public void Unsubscribe(int organizationId) 
    { 
     var clientId = this.OperationContext.GetClientId(); 
     if (string.IsNullOrEmpty(clientId)) 
     { 
      return; 
     } 

     this.InternalUnsubscribe(organizationId, clientId); 
    } 

    /// <summary> 
    /// Begins an asynchronous operation to Unsubscribe. 
    /// </summary> 
    /// <param name="organizationId">The organization id.</param> 
    /// <param name="callback">The callback.</param> 
    /// <param name="passThroughData">The pass through data.</param> 
    /// <returns> 
    /// An implementation of <see cref="IAsyncResult"/> that provides access to the state or result of the operation. 
    /// </returns> 
    public IAsyncResult BeginUnsubscribe(int organizationId, AsyncCallback callback, object passThroughData) 
    { 
     var clientId = this.OperationContext.GetClientId(); 
     if (string.IsNullOrEmpty(clientId)) 
     { 
      return null; 
     } 

     var asyncResult = new VoidAsyncResult(callback, passThroughData); 
     Task.Factory.StartNew(() => 
      { 
       try 
       { 
        this.InternalUnsubscribe(organizationId, clientId); 
        asyncResult.SetAsCompleted(false); 
       } 
       catch (Exception ex) 
       { 
        asyncResult.SetAsCompleted(ex, false); 
       } 
      }); 
     return asyncResult; 
    } 

    /// <summary> 
    /// Ends an existing asynchronous operation to Unsubscribe. 
    /// </summary> 
    /// <param name="result">The <see cref="IAsyncResult"/> provided by the BeginUnsubscribe operation.</param> 
    public void EndUnsubscribe(IAsyncResult result) 
    { 
     var response = result as VoidAsyncResult; 
     if (response != null) 
     { 
      response.EndInvoke(); 
     } 
    } 

你并不需要编写WCF服务是异步。您简单地需要为定义了Async方法的服务制定ServiceContract。这里有一个简单的例子

[ServiceContract] 
public interface IMyService 
{ 
    [OperationContract] 
    int Foo(string input); 
} 

//tell wcf that this contract applies to IMyService 
[ServiceContract(Name = "IMyService")] 
public interface IMyServiceAsync 
{ 
    //setting AsyncPattern = true allows WCF to map the async methods to the sync ones. 
    [OperationContract(AsyncPattern = true)] 
    IAsyncResult BeginFoo(string input, AsyncCallback callback, object asyncState); 

    int EndFoo(IAsyncResult result}; 
} 

// you only need to implement the sync contract 
public class MyService : IMyService 
{ 
    public int Foo(string input) 
    { 
     return input.Length; 
    } 
} 

现在使用IMyServiceAsync您的ChannelFactory,一切都应该工作。

+0

谢谢你,我刚刚找到了一个可以做这个转换的t4模板,现在就开始了。 – v00d00

+0

让我知道它是如何工作的。我一直在寻找t4来做到这一点,但没有时间。 – cadrell0

+0

它的失败,会尽量明确指定操作名称 – v00d00