开发者

ASP.NET MVC async call a WCF service

After complete of asynchronous call to WCF service I want set succes开发者_开发技巧s message into session and show user the notification .

I tried use two ways for complete this operation.

1) Event Based Model.

client.GetDataCompleted += new EventHandler<GetDataCompletedEventArgs>(GetDataCompleted);
client.GetDataAsync(id, client);
private void GetDataCompleted(object obj, GetDataCompletedEventArgs e) 
{
   this.SetNotification(new Notification() { Message = e.Result, Type =    NotificationType.Success });
}

In MyOperationCompleted event i can set notification to HttpContext.Current.Session, but I must waiting before this operation will completed and can't navigate to others pages.

2) IAsyncResult Model. In this way I can navigate to other pages and make asynchronous calls to wcf service, but in GetDataCallback method can't set notification, becouse session = null.

client.BeginGetData(id, GetDataCallback, client);
private void GetDataCallback(IAsyncResult ar)
{
    string name = ((ServiceReference1.Service1Client)ar.AsyncState).EndGetData(ar);
    this.SetNotification(new Notification() { Message = name, Type = NotificationType.Success });
}

"Generate asynchronous operations" in service reference enabled.

Please help me with this trouble. Thanks.


I'm no wcf expert, but what I've found to work is wrapping your call to the Async version of your method in ThreadPool.QueueUserWorkItem. Without this, I had same blocking issue. So this seems to free up the main thread in your asp mvc to move on while another worker thread waits for the callback.

Also, I used AsyncController, although that alone was not enough without the worker thread.

See this: http://msdn.microsoft.com/en-us/library/ee728598.aspx

I used this as a guide, but still needed the ThreadPool.

Cheers

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜