2

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.

Community
  • 1
  • 1
anmarti
  • 5,045
  • 10
  • 55
  • 96
  • 2
    refer to http://stackoverflow.com/questions/400798/how-to-make-a-call-to-my-wcf-service-asynchronous – vibhu Jul 15 '13 at 16:15

1 Answers1

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

Alex
  • 8,827
  • 3
  • 42
  • 58
  • Thank you. Since is not the solution I was looking for it seems to works as async manner. However I edit your answer because the way you declared the delegate didn't allowed me to run in async way. – anmarti Jul 18 '13 at 08:56
  • @senyorToni You are welcome. Also if it is suiable for you to use proxi instead of ChannelFactory, you can create proxi class for your client using svcutil with /async directive. This will automaticly generate you BeginMyMethod method, which would start async. Without results tour call would be a single line of code: new YourProxiClient().BeginMyMethod(); – Alex Jul 18 '13 at 09:46