Skip to content

Commit

Permalink
AspNetCoreServiceProviderSlackRequestListener creates its own scope w…
Browse files Browse the repository at this point in the history
…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.
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);
}
}
}
}

0 comments on commit 6c7fa04

Please sign in to comment.