Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Src/SmtpServer/ISessionServiceProviderFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Threading.Tasks;

namespace SmtpServer
{
/// <summary>
/// Allows customization of the IServiceProvider instance that is used in the session context.
/// </summary>
public interface ISessionServiceProviderFactory
{
/// <summary>
/// Creates an IServiceProvider instance for one session context.
/// </summary>
IServiceProvider CreateServiceProvider(IServiceProvider rootServiceProvider);
}
}
2 changes: 1 addition & 1 deletion Src/SmtpServer/Net/IEndpointListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace SmtpServer.Net
public interface IEndpointListener : IDisposable
{
/// <summary>
/// Returns a securtable pipe to the endpoint.
/// Returns a securable pipe to the endpoint.
/// </summary>
/// <param name="context">The session context that the pipe is being created for.</param>
/// <param name="cancellationToken">The cancellation token.</param>
Expand Down
7 changes: 5 additions & 2 deletions Src/SmtpServer/SmtpServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class SmtpServer
readonly ISmtpServerOptions _options;
readonly IServiceProvider _serviceProvider;
readonly IEndpointListenerFactory _endpointListenerFactory;
readonly ISessionServiceProviderFactory _sessionServiceProviderFactory;
readonly SmtpSessionManager _sessions;
readonly CancellationTokenSource _shutdownTokenSource = new CancellationTokenSource();
readonly TaskCompletionSource<bool> _shutdownTask = new TaskCompletionSource<bool>();
Expand All @@ -50,6 +51,7 @@ public SmtpServer(ISmtpServerOptions options, IServiceProvider serviceProvider)
_serviceProvider = serviceProvider;
_sessions = new SmtpSessionManager(this);
_endpointListenerFactory = serviceProvider.GetServiceOrDefault(EndpointListenerFactory.Default);
_sessionServiceProviderFactory = serviceProvider.GetServiceOrDefault<ISessionServiceProviderFactory>(null);
}

/// <summary>
Expand Down Expand Up @@ -128,7 +130,8 @@ async Task ListenAsync(IEndpointDefinition endpointDefinition, CancellationToken

while (cancellationTokenSource.Token.IsCancellationRequested == false)
{
var sessionContext = new SmtpSessionContext(_serviceProvider, _options, endpointDefinition);
var spForCtx = _sessionServiceProviderFactory?.CreateServiceProvider(_serviceProvider);
var sessionContext = new SmtpSessionContext(spForCtx ?? _serviceProvider, _options, endpointDefinition);

try
{
Expand All @@ -144,7 +147,7 @@ async Task ListenAsync(IEndpointDefinition endpointDefinition, CancellationToken

if (sessionContext.Pipe != null)
{
_sessions.Run(sessionContext, cancellationTokenSource.Token);
_sessions.Run(sessionContext, cancellationTokenSource.Token, spForCtx);
}
}
}
Expand Down
20 changes: 16 additions & 4 deletions Src/SmtpServer/SmtpSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal SmtpSessionManager(SmtpServer smtpServer)
_smtpServer = smtpServer;
}

internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellationToken)
internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellationToken, IServiceProvider sessionServiceProvider)
{
var handle = new SmtpSessionHandle(new SmtpSession(sessionContext), sessionContext);
var handle = new SmtpSessionHandle(new SmtpSession(sessionContext), sessionContext, sessionServiceProvider);
Add(handle);

handle.CompletionTask = RunAsync(handle, cancellationToken).ContinueWith(task =>
Expand All @@ -27,7 +27,7 @@ internal void Run(SmtpSessionContext sessionContext, CancellationToken cancellat
});
}

async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToken)
private async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToken)
{
using var sessionTimeoutCancellationTokenSource = new CancellationTokenSource(handle.SessionContext.EndpointDefinition.SessionTimeout);

Expand Down Expand Up @@ -58,6 +58,15 @@ async Task RunAsync(SmtpSessionHandle handle, CancellationToken cancellationToke
await handle.SessionContext.Pipe.Input.CompleteAsync();

handle.SessionContext.Pipe.Dispose();

if (handle.SessionServiceProvider is IAsyncDisposable asyncDisposable)
{
await asyncDisposable.DisposeAsync();
}
else if (handle.SessionServiceProvider is IDisposable disposable)
{
disposable.Dispose();
}
}
}

Expand Down Expand Up @@ -91,16 +100,19 @@ void Remove(SmtpSessionHandle handle)

class SmtpSessionHandle
{
public SmtpSessionHandle(SmtpSession session, SmtpSessionContext sessionContext)
public SmtpSessionHandle(SmtpSession session, SmtpSessionContext sessionContext, IServiceProvider sessionServiceProvider)
{
Session = session;
SessionContext = sessionContext;
SessionServiceProvider = sessionServiceProvider;
}

public SmtpSession Session { get; }

public SmtpSessionContext SessionContext { get; }

public IServiceProvider SessionServiceProvider { get; }

public Task CompletionTask { get; set; }
}
}
Expand Down