-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
AspNetCoreServiceProviderSlackRequestListener creates its own scope w…
…hen in socket mode
- Loading branch information
Simon Oxtoby
committed
May 1, 2021
1 parent
e25c944
commit 6c7fa04
Showing
1 changed file
with
28 additions
and
4 deletions.
There are no files selected for viewing
32 changes: 28 additions & 4 deletions
32
SlackNet.AspNetCore/AspNetCoreServiceProviderSlackRequestListener.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,38 @@ | ||
using Microsoft.AspNetCore.Http; | ||
using System; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using SlackNet.Extensions.DependencyInjection; | ||
|
||
namespace SlackNet.AspNetCore | ||
{ | ||
class AspNetCoreServiceProviderSlackRequestListener : IServiceProviderSlackRequestListener | ||
{ | ||
private readonly IHttpContextAccessor _httpContextAccessor; | ||
public AspNetCoreServiceProviderSlackRequestListener(IHttpContextAccessor httpContextAccessor) => _httpContextAccessor = httpContextAccessor; | ||
private readonly IServiceProvider _serviceProvider; | ||
|
||
public void OnRequestBegin(SlackRequestContext context) => | ||
context.SetServiceProvider(_httpContextAccessor.HttpContext.RequestServices); | ||
public AspNetCoreServiceProviderSlackRequestListener(IHttpContextAccessor httpContextAccessor, IServiceProvider serviceProvider) | ||
{ | ||
_httpContextAccessor = httpContextAccessor; | ||
_serviceProvider = serviceProvider; | ||
} | ||
|
||
public void OnRequestBegin(SlackRequestContext context) | ||
{ | ||
if (context.ContainsKey("Envelope")) // Socket mode | ||
{ | ||
var scope = _serviceProvider.CreateScope(); | ||
context.SetServiceProvider(scope.ServiceProvider); | ||
context.OnComplete(() => | ||
{ | ||
scope.Dispose(); | ||
return Task.CompletedTask; | ||
}); | ||
} | ||
else | ||
{ | ||
context.SetServiceProvider(_httpContextAccessor.HttpContext.RequestServices); | ||
} | ||
} | ||
} | ||
} |