问题
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