任务<t>在WebApi中没有响应,但返回非任务工作</t>

时间:2014-05-19 15:07:11

标签: c# task asp.net-web-api

我有一个不会向浏览器返回响应的方法,我尝试使用浏览器本身和chrome扩展名POSTMan。

    [AllowAnonymous]
    [Route("getNotifications")]
    [AcceptVerbs("Get", "Post")]
    public Task<List<UserNotifications>> GetNotifications()
    {
        Task<List<UserNotifications>> userNotList = new Task<List<UserNotifications>>(() => new List<UserNotifications>{
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 1",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 1"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 2",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 2"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 3",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 3"
            }
        });
        return userNotList;
    }

但是,如果我将方法更改为不返回类型任务...

[AllowAnonymous]
[Route("getNotifications")]
[Authorize]
[AcceptVerbs("Get", "Post")]
public List<UserNotifications> GetNotifications()
{
    var newstring = "";
    List<UserNotifications> userNotList = new List<UserNotifications>{
        new UserNotifications()
        {
            UserId = new Guid(),
            MessageText = "Message 1",
            MessageTime = DateTime.Now,
            Status = true,
            Subject = "new message 1"
        },
        new UserNotifications()
        {
            UserId = new Guid(),
            MessageText = "Message 2",
            MessageTime = DateTime.Now,
            Status = true,
            Subject = "new message 2"
        },
        new UserNotifications()
        {
            UserId = new Guid(),
            MessageText = "Message 3",
            MessageTime = DateTime.Now,
            Status = true,
            Subject = "new message 3"
        }
    };

    return userNotList;
}

我每次都会得到以下回复

[
    {
        "userId": "00000000-0000-0000-0000-000000000000",
        "subject": "new message 1",
        "messageText": "Message 1",
        "messageTime": "2014-05-19T11:03:32.707957-04:00",
        "status": true
    },
    {
        "userId": "00000000-0000-0000-0000-000000000000",
        "subject": "new message 2",
        "messageText": "Message 2",
        "messageTime": "2014-05-19T11:03:32.707957-04:00",
        "status": true
    },
    {
        "userId": "00000000-0000-0000-0000-000000000000",
        "subject": "new message 3",
        "messageText": "Message 3",
        "messageTime": "2014-05-19T11:03:32.707957-04:00",
        "status": true
    }
]

为什么当我使用任务时,没有任何东西会在测试调用中直接返回浏览器直接返回webapi服务?

编辑1

改变方法......由于某种原因仍然坐着旋转轮子

    [AllowAnonymous]
    [Route("getNotifications")]
    [AcceptVerbs("Get", "Post")]
    public async Task<List<UserNotifications>> GetNotifications()
    {
        Task<List<UserNotifications>> userNotList = new Task<List<UserNotifications>>(() => new List<UserNotifications>{
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 1",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 1"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 2",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 2"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 3",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 3"
            }
        });
        return await userNotList;
    }

然而,当我把它改成这个......

    [AllowAnonymous]
    [Route("getNotifications")]
    [AcceptVerbs("Get", "Post")]
    public Task<List<UserNotifications>> GetNotifications()
    {
        var newstring = "";
        List<UserNotifications> userNotList = new List<UserNotifications>{
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 1",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 1"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 2",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 2"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 3",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 3"
            }
        };

        return Task.FromResult(userNotList);
    }

它返回的结果很好......任何人都可以澄清这些方法之间的区别。我正在冒险进入Task,asnyc的土地,并等待,但我仍然需要能够使用像POSTMan这样的东西直接测试对webapi的调用。

编辑2 以下两种变体都有效......

    [AllowAnonymous]
    [Route("getNotifications")]
    [AcceptVerbs("Get", "Post")]
    public Task<List<UserNotifications>> GetNotifications()
    {
        var userNotList = Task.Run(
                () => new List<UserNotifications>{
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 1",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 1"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 2",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 2"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 3",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 3"
            }
        });

        return userNotList;
    }

    [AllowAnonymous]
    [Route("getNotifications")]
    [AcceptVerbs("Get", "Post")]
    public async Task<List<UserNotifications>> GetNotifications()
    {
        var userNotList = Task.Run(
                () => new List<UserNotifications>{
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 1",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 1"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 2",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 2"
            },
            new UserNotifications()
            {
                UserId = new Guid(),
                MessageText = "Message 3",
                MessageTime = DateTime.Now,
                Status = true,
                Subject = "new message 3"
            }
        });

        return await userNotList;
    }

1 个答案:

答案 0 :(得分:3)

已创建任务,但尚未实际启动,请尝试替换您的任务构造函数:

Task<List<UserNotifications>> userNotList = new Task<List<UserNotifications>>(...

以下内容:

var userNotList = Task<List<UserNotifications>>.Run(
                    () => new List<UserNotifications>{...});

更新

  1. 您添加async/await的已编辑示例的问题是相同的,您创建一个知道该做什么的对象Task,但实际上并不是告诉开始做这项工作Run方法。
  2. 在您使用Task.FromResult(userNotList)的第二个示例中,您已经拥有了结果列表,只需将其包装到已完成 Task中并将其返回。
  3. 有关此主题的更多信息,我强烈建议您阅读Task-based Asynchronous Pattern