异步调用外部Web服务方法没有返回预期的结果 [英] Async call to external webservice method not returning expected result

查看:216
本文介绍了异步调用外部Web服务方法没有返回预期的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

**注意我问的这个问题,得到了标记为重复,但重复的问题,在解决我遇到下面的问题没有帮助。

**Note I had asked this question which got marked as Duplicate but the Duplicate question did not help in solving the issue I am having below.

新手到异步编程在C#中,我有在得到下面的工作有点困难。

Newbie to async programming in C# and I am having some difficulty in getting the below working.

我使用的是外部的WebService获取CarInfo - 他们已经提供了异步方法,这在下面的例子中返回一个字符串的汽车的多个对象传入我有我自己的此WebService接口中的类似如下的抽​​象。

I am using an External WebService to Obtain CarInfo - they have provided Async methods which in the example below returns a string for a Number of Cars objects passed in. I have my own abstraction of this WebService in an interface something like below:

Task<string> GetTicketIdAsync(Cars[] cars);

我的接口方法的实现如下:

My Implementation of interface method is as below:

public async Task<string> GetTicketIdAsync(Ccars[] cars)
{
    try
    {
        if (_externalServiceClient == null)
        {
            _externalServiceClient = new ExternalServiceClient("WSHttpBinding_IExternalService");
        }

        string ticketID =
            await _externalServiceClient .GetCarInfosAsync(cars)

        return ticketID;
    }
    catch (Exception ex)
    {
        //TODO log 4 net 
        throw new Exception("Failed" + ex.Message);
    }
    finally
    {
        //WCF - Dispose and close client and then set to null
        CloseClient(_externalServiceClient );
        _externalServiceClient = null;
    }

在另一个类,那么我有一个方法如下里面加一些车到数据库,然后调用同一个类中的私有方法从web服务获得门票吧。

In another class then I have a method as below which add some cars to DB and then calls a private method in same class to get ticket it from webservice.

public void AddCars(List<Cars> cars)
    {
        var ticketId = UpdateCarWithTicketId(cars);

        string test = "Hello-World";
    }

我与TicketId方法UpdateCars是如下:

My UpdateCars with TicketId method is as below:

private async Task<string> UpdateCarWithTicketId(List<Cars> cars)
{
    //Call my abstraction of external web service method
    string ticketId = await _myService.GetTicketIdAsync(cars);

    foreach (var car in cars)
    {
        cars.TicketId = ticketId;
    }

    //Update DB
    _carRepository.Update(cars);

    return ticketId;
}

这几件事情 - 如果我在foreach循环变种车在汽车中设置断点它似乎从来就没打了这么数据库没有更新。然后,如果我在我的上线测试AddCars方法上设置断点=Hello World的路线 - ticketId的值显示,当我希望从我的调用外部Web服务返回一个唯一的字符串状态WaitingForActivati​​on

A few things - if I set a breakpoint on the foreach loop var car in cars it never seems to get hit so the DB does not get updated. And then if I set a breakpoint on my AddCars method on the string test = "hello world" line - the value of ticketId shows status WaitingForActivation when I was expecting a unique string returned from my call to external web service.

我尝试添加ConfigureAwait(假)这两个在那里等待被使用,但仍然得到同样的结果的话费。有什么我在配置异步方法才能正确运行很想念?

I tried adding ConfigureAwait(false) to both of the calls where await was used but still getting same result. Is there something I am missing in configuring async method to run correctly?

编辑 -

目前这是从MVC控制器,当用户点击屏幕上的按钮调用 -

Currently this is being call from MVC Controller when User hits button on screen -

    [AcceptVerbs(HttpVerbs.Post)]
    public async Task<ActionResult>Upload(CarImport viewModel)
    {
         //list of cars uploaded by user in Excel sheet - code to extract that removed

            await _CarInfoService.AddCars(cars);
        }
        return RedirectToAction("Home");
    }

不过我上建一个错误说不能等待无效 - 在我的_CarInfoService的AddCars方法签名是无效的? - 应该是改为别的东西。

However I get a error on Build saying cannot await void - the AddCars method signature on my _CarInfoService is void - should that be changed to something else?

推荐答案

UpdateCarWithTicketId 返回任务&LT;字符串&GT; 不是字符串。你可以得到一个字符串出它通过做这样的事情:

UpdateCarWithTicketId returns a Task<string> not a string. You could get a string out of it by doing something like this:

public async void AddCars(List<Cars> cars)
{
    var ticketId = await UpdateCarWithTicketId(cars);

    string test = "Hello-World";
}

这篇关于异步调用外部Web服务方法没有返回预期的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆