Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.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

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
C# netcore 3.1中触发和忘记信号器调用的简单方法_C#_Asp.net Core_Async Await_Signalr_Asp.net Core 3.1 - Fatal编程技术网

C# netcore 3.1中触发和忘记信号器调用的简单方法

C# netcore 3.1中触发和忘记信号器调用的简单方法,c#,asp.net-core,async-await,signalr,asp.net-core-3.1,C#,Asp.net Core,Async Await,Signalr,Asp.net Core 3.1,我在.NETCore3.1中有一个web服务,它合理地大量使用了信号器。原始代码只调用调用SignalR SendAsync方法的方法,虽然可能有一些不必要的任务包装,但最终与SignalR调用相关的任何任务都不会执行任何操作: //in the hub manager public async Task ShowBanner(string group) { await _signalrHubContext.Clients.Group(group).Sen

我在.NETCore3.1中有一个web服务,它合理地大量使用了信号器。原始代码只调用调用SignalR SendAsync方法的方法,虽然可能有一些不必要的任务包装,但最终与SignalR调用相关的任何任务都不会执行任何操作:

    //in the hub manager
    public async Task ShowBanner(string group)
    {
        await _signalrHubContext.Clients.Group(group).SendAsync("showBanner");
    }

    //in the controller
    [HttpGet("Show/Banner")]
    public async Task<ActionResult> ShowBanner()
    {
        try {
            //some other await-necessary db stuff here
            await dbcontext.Blah....;

            somehubmanager.ShowBanner(); //because this call is not awaited...

            return Ok();
        }
        catch (Exception e)
        {
            return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
        }
    }
//在集线器管理器中
公共异步任务ShowBanner(字符串组)
{
wait_signalhubcontext.Clients.Group(Group.SendAsync(“showBanner”);
}
//在控制器中
[HttpGet(“显示/横幅”)]
公共异步任务是解决这个问题的方法吗?还是我想得太多了,我应该放弃这个任务?SendAsync的文档说“不等待…”,现在一切正常,但我也很好奇这是否纯粹是巧合,因为信号器比完成服务请求所需的时间更快/更轻;我们是否有一天会放慢速度,这样我们会发现web服务请求在SignalR消息发生任何事情之前完成并结束,而消息甚至不会被尝试(即,在调用ShowBanner之前,DI是否会处理作用域hubmanager)

我只是不确定在特定的信号器环境下,我需要“处理”这个任务多长时间;如果我这样做,底层消息是否仍能正常工作:

    public Task ShowBanner(string group)
    {
        return _signalrHubContext.Clients.Group(group).SendAsync("showBanner");
    }

    [HttpGet("Show/Banner")]
    public async Task<ActionResult> ShowBanner()
    {
        try {
            //some other await-necessary db stuff here
            await dbcontext.Blah....;

            _ = somehubmanager.ShowBanner();

            return Ok();
        }
        catch (Exception e)
        {
            return StatusCode((int)HttpStatusCode.InternalServerError, e.Message);
        }
    }
公共任务显示横幅(字符串组)
{
返回_signalhubcontext.Clients.Group(Group.sendsync)(“showBanner”);
}
[HttpGet(“显示/横幅”)]
公共异步任务ShowBanner()
{
试一试{
//还有一些其他的东西在这里等着
等待上下文。诸如此类。。。。;
_=somehubmanager.ShowBanner();
返回Ok();
}
捕获(例外e)
{
返回状态码((int)HttpStatusCode.InternalServerError,e.Message);
}
}
实现MSDN中详述的后台工作队列功能是实现这一点的方法吗?还是我想得太多了,我应该放弃这个任务

后台队列将为您提供运行代码的“最大努力”。后台队列为您提供了一种与宿主进程交互的方法,如果可能,它将延迟关机以运行代码。也就是说,如果不是那么重要,放弃任务就更容易了

如果我这样改变了事情,那么底层消息是否仍能正常工作

对<代码>方法异步()
\=MethodAsync()相同。这两行都将调用该方法,然后忽略返回的任务。唯一的区别是显式放弃(
\uuu
),本质上就是告诉编译器“是的,我知道这个任务没有等待,我是故意的”

实现MSDN中详述的后台工作队列功能是实现这一点的方法吗?还是我想得太多了,我应该放弃这个任务

后台队列将为您提供运行代码的“最大努力”。后台队列为您提供了一种与宿主进程交互的方法,如果可能,它将延迟关机以运行代码。也就是说,如果不是那么重要,放弃任务就更容易了

如果我这样改变了事情,那么底层消息是否仍能正常工作

对<代码>方法异步()
\=MethodAsync()相同。这两行都将调用该方法,然后忽略返回的任务。唯一的区别是显式放弃(
\uuu
),本质上就是告诉编译器“是的,我知道这个任务没有等待,我是故意的”