Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在寻找在Silverlight中使用async调用web服务的示例?_Silverlight_Visual Studio 2012_C# 5.0 - Fatal编程技术网

正在寻找在Silverlight中使用async调用web服务的示例?

正在寻找在Silverlight中使用async调用web服务的示例?,silverlight,visual-studio-2012,c#-5.0,Silverlight,Visual Studio 2012,C# 5.0,我正在寻找一个在Silverlight中使用new async关键字调用web服务的示例 这是我试图转换的代码: var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress); client.SelectActiveDealsCompleted += (s, e) => m_Parent.BeginInvoke(() => Re

我正在寻找一个在Silverlight中使用new async关键字调用web服务的示例

这是我试图转换的代码:

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
client.SelectActiveDealsCompleted += (s, e) => m_Parent.BeginInvoke(() => RefreshDealsGridComplete(e.Result, e.Error));
client.SelectActiveDealsAsync();

您必须使用目标版本.Net 4.5重新生成服务代码。然后可以使用
async
关键字。编写如下代码:

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
// Wait here until you get the result ...
var result = await client.SelectActiveDealsAsync();
// ... then refresh the ui synchronously.
RefreshDealsGridComplete(result);
或者使用
ContinueWith
方法:

var client = new DashboardServicesClient("BasicHttpBinding_IDashboardServices", App.DashboardServicesAddress);
// Start the call ...
var resultAwaiter = client.SelectActiveDealsAsync();
// ... and define, what to do after finishing (while the call is running) ...
resultAwaiter.ContinueWith( async task => RefreshDealsGridComplete(await resultAwaiter));
// ... and forget it

你可以自己做:

static class DashboardServicesClientExtensions
{
    //typeof(TypeIDontKnow) == e.Result.GetType()
    public static Task<TypeIDontKnow>> SelectActiveDealsTaskAsync()
    {
        var tcs = new TaskCompletionSource<TypeIDontKnow>();

        client.SelectActiveDealsCompleted += (s, e) => m_Parent.BeginInvoke(() =>
        {
            if (e.Erorr != null)
                tcs.TrySetException(e.Error);
            else
                tcs.TrySetResult(e.Result);
        };
        client.SelectActiveDealsAsync();

        return tcs.Task;
    }
};

// calling code
// you might change the return type to Tuple if you want :/
try
{
    // equivalent of e.Result
    RefreshDealsGridComplete(await client.SelectActiveDealsTaskAsync(), null);
}
catch (Exception e)
{
    RefreshDealsGridComplete(null, e);
}
静态类仪表板服务客户端扩展
{
//typeof(TypeIDontKnow)==e.Result.GetType()
公共静态任务>选择ActiveDealsTaskAsync()
{
var tcs=new TaskCompletionSource();
client.SelectActiveDealsCompleted+=(s,e)=>m_Parent.BeginInvoke(()=>
{
如果(e.Erorr!=null)
tcs.TrySetException(即错误);
其他的
tcs.TrySetResult(e.Result);
};
client.SelectActiveDealsAsync();
返回tcs.Task;
}
};
//呼叫码
//如果需要,可以将返回类型更改为Tuple:/
尝试
{
//e.结果的等价物
RefreshDealsGridComplete(等待客户端。选择ActiveDealsTaskAsync(),null);
}
捕获(例外e)
{
RefreshDealsGridComplete(null,e);
}

不起作用,我仍然只看到非基于任务的版本。此外,“服务参考设置”对话框中的“生成基于任务的操作”选项变灰。