Calling async WCF without service reference using ChannelFactory

被刻印的时光 ゝ 提交于 2019-12-10 22:21:47

问题


I'm using .NET 3.5 This is a related question but using TPL Async Library, since I'm in 3.5 I need another approach.

I used to call a WCF asynchronously by adding a service reference and creating its async operations using Visual Studio 2010.

Now I've created a dynamic proxy using the CreateChannel<T> of the ChannelFactory class and I need to call a method in async way. This is how I create the WCF proxy:

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

I don't need the service response.


回答1:


I'm not sure if I understood you correctly, but if you want to make your Proxy.MyMethod to run async by means of .NET 3.5, you can use standart BeginInvoke of the Delegate class, like this:

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

Then in code, you just call your method async:

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();

If you need to check something about results, use result variable for this



来源:https://stackoverflow.com/questions/17658770/calling-async-wcf-without-service-reference-using-channelfactory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!