2013-07-15 53 views
2

我使用的是.NET 3.5This是一個相關的問題,但使用TPL異步庫,因爲我在3.5我需要另一種方法。使用ChannelFactory調用不帶服務引用的異步WCF

我曾經通過添加服務引用和創建它的異步操作使用Visual Studio 2010

現在我已經創建使用的ChannelFactory類的CreateChannel<T>動態代理異步調用WCF,我需要調用異步方式。這是我如何創建WCF代理:

public MyInterface Proxy { get; set; } 

    BasicHttpBinding binding = new BasicHttpBinding(); 
    EndpointAddress ep = new EndpointAddress("http://localhost/myEndpoint"); 
    Proxy = ChannelFactory<MyInterface>.CreateChannel(binding, ep); 

    // I call my method 
    Proxy.MyMethod(); 

    [ServiceContract] 
    public Interface MyInterface 
    { 
     [OperationContract] 
     void MyMethod(); 
    } 

我不需要服務響應。

+2

指http://stackoverflow.com/ questio ns/400798/how-to-make-a-call-to-my-wcf-service-asynchronous – vibhu

回答

1

我不知道如果我理解正確的你,但如果你想使你的Proxy.MyMethod通過.NET 3.5的方式來運行異步,您可以使用Delegate類的非標準BeginInvoke的,就像這樣:

//Make a delegate for your Proxy.MyMethod 
private delegate void MyDelegate(); 

然後在代碼中,你只需要調用你的方法異步:

BasicHttpBinding binding = new BasicHttpBinding(); 
EndpointAddress ep = new EndpointAddress("http://localhost/myEndpoint"); 
Proxy = ChannelFactory<MyInterface>.CreateChannel(binding, ep); 
var delInstance = new MyDelegate(Proxy.MyMethod); 
var result = delInstance.BeginInvoke(); 

如果您需要檢查一些有關結果,用結果變量此

+0

謝謝。既然不是我尋找的解決方案,它似乎以異步方式工作。然而,我編輯你的答案,因爲你聲明委託的方式不允許我以異步方式運行。 – anmarti

+0

@senyorToni不客氣。另外,如果可以使用proxi而不是ChannelFactory,則可以使用帶/ async指令的svcutil爲您的客戶端創建proxi類。這會自動生成BeginMyMethod方法,該方法將啓動異步。如果沒有結果,巡視會調用一行代碼:new YourProxiClient()。BeginMyMethod(); – Alex