Skip to content

Commit 466dbd7

Browse files
committed
Add simple Autofac sample
1 parent aefd8e8 commit 466dbd7

File tree

6 files changed

+113
-0
lines changed

6 files changed

+113
-0
lines changed

Mediator.sln

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AspNetCoreIndirect.Implemen
104104
EndProject
105105
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Notifications", "samples\Notifications\Notifications.csproj", "{78D25AC1-4DDD-4049-8B26-D287931905E5}"
106106
EndProject
107+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleAutoFac", "samples\SimpleAutoFac\SimpleAutoFac.csproj", "{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}"
108+
EndProject
107109
Global
108110
GlobalSection(SolutionConfigurationPlatforms) = preSolution
109111
Debug|Any CPU = Debug|Any CPU
@@ -462,6 +464,18 @@ Global
462464
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x64.Build.0 = Release|Any CPU
463465
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x86.ActiveCfg = Release|Any CPU
464466
{78D25AC1-4DDD-4049-8B26-D287931905E5}.Release|x86.Build.0 = Release|Any CPU
467+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
468+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|Any CPU.Build.0 = Debug|Any CPU
469+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|x64.ActiveCfg = Debug|Any CPU
470+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|x64.Build.0 = Debug|Any CPU
471+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|x86.ActiveCfg = Debug|Any CPU
472+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Debug|x86.Build.0 = Debug|Any CPU
473+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|Any CPU.ActiveCfg = Release|Any CPU
474+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|Any CPU.Build.0 = Release|Any CPU
475+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|x64.ActiveCfg = Release|Any CPU
476+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|x64.Build.0 = Release|Any CPU
477+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|x86.ActiveCfg = Release|Any CPU
478+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53}.Release|x86.Build.0 = Release|Any CPU
465479
EndGlobalSection
466480
GlobalSection(SolutionProperties) = preSolution
467481
HideSolutionNode = FALSE
@@ -498,6 +512,7 @@ Global
498512
{DB9A7862-14B1-4DAC-878C-D6B02038F068} = {BC759BA5-C064-460E-ACB0-9B2DA12B9284}
499513
{7F7DB4D8-3D94-4648-99FA-E7E06A913FCD} = {BC759BA5-C064-460E-ACB0-9B2DA12B9284}
500514
{78D25AC1-4DDD-4049-8B26-D287931905E5} = {D3569CDD-7E19-429E-B9AD-75CC05F6C4AA}
515+
{FEF7708E-D94E-49D0-8B0F-3A60A4008F53} = {D3569CDD-7E19-429E-B9AD-75CC05F6C4AA}
501516
EndGlobalSection
502517
GlobalSection(ExtensibilityGlobals) = postSolution
503518
SolutionGuid = {D45B5457-4190-49B6-BF89-7FA5F4C8ABE2}

samples/SimpleAutoFac/Program.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using Autofac.Extensions.DependencyInjection;
2+
using Mediator;
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());
8+
9+
builder.Services.AddMediator(
10+
config =>
11+
{
12+
config.Namespace = "Foo.Generated";
13+
config.ServiceLifetime = ServiceLifetime.Scoped;
14+
}
15+
);
16+
17+
var app = builder.Build();
18+
19+
app.MapGet(
20+
"/",
21+
async ([FromServices] Foo.Generated.Mediator mediator) =>
22+
{
23+
var id = Guid.NewGuid();
24+
var request = new Ping(id);
25+
var response = await mediator.Send(request);
26+
return Results.Ok(response);
27+
}
28+
);
29+
30+
app.Run();
31+
32+
public sealed record Ping(Guid Id) : IRequest<Pong>;
33+
34+
public sealed record Pong(Guid Id);
35+
36+
public sealed class PingHandler : IRequestHandler<Ping, Pong>
37+
{
38+
public ValueTask<Pong> Handle(Ping request, CancellationToken cancellationToken)
39+
{
40+
return new ValueTask<Pong>(new Pong(request.Id));
41+
}
42+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"profiles": {
3+
"AutoFac": {
4+
"commandName": "Project",
5+
"dotnetRunMessages": true,
6+
"launchBrowser": false,
7+
"applicationUrl": "http://localhost:5084",
8+
"environmentVariables": {
9+
"ASPNETCORE_ENVIRONMENT": "Development"
10+
}
11+
}
12+
}
13+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<EmitCompilerGeneratedFiles>true</EmitCompilerGeneratedFiles>
8+
<CompilerGeneratedFilesOutputPath>Generated</CompilerGeneratedFilesOutputPath>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<Compile Remove="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
13+
<None Include="$(CompilerGeneratedFilesOutputPath)/**/*.cs" />
14+
</ItemGroup>
15+
16+
<ItemGroup>
17+
<ProjectReference Include="..\..\src\Mediator.SourceGenerator.Implementation\Mediator.SourceGenerator.Implementation.csproj" OutputItemType="Analyzer" />
18+
<ProjectReference Include="..\..\src\Mediator.SourceGenerator.Roslyn40\Mediator.SourceGenerator.Roslyn40.csproj" OutputItemType="Analyzer" />
19+
<ProjectReference Include="..\..\src\Mediator\Mediator.csproj" />
20+
</ItemGroup>
21+
22+
<ItemGroup>
23+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="8.0.0" />
24+
</ItemGroup>
25+
26+
</Project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
}
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)