Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ASP.NET Core 3 DI sample targeting NSB v8 #5667

Merged
merged 2 commits into from
Mar 16, 2022
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Sample\Sample.csproj
25 changes: 25 additions & 0 deletions samples/dependency-injection/aspnetcore/Core_8/ASPNetCore.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29728.190
MinimumVisualStudioVersion = 15.0.26730.12
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Sample", "Sample\Sample.csproj", "{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SampleAutofac", "SampleAutofac\SampleAutofac.csproj", "{6FA6EAAD-E749-4511-BBA9-DB5709835195}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6E6C9C2A-8D9B-41AF-B5E5-1AF5310686E7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6FA6EAAD-E749-4511-BBA9-DB5709835195}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{6FA6EAAD-E749-4511-BBA9-DB5709835195}.Debug|Any CPU.Build.0 = Debug|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0D70AFFA-DAF9-43E5-AA4A-59BA214FD0A9}
EndGlobalSection
EndGlobal
21 changes: 21 additions & 0 deletions samples/dependency-injection/aspnetcore/Core_8/Sample/MyHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;
using NServiceBus;

#region InjectingDependency
public class MyHandler :
IHandleMessages<MyMessage>
{
MyService myService;

public MyHandler(MyService myService)
{
this.myService = myService;
}

public Task Handle(MyMessage message, IMessageHandlerContext context)
{
myService.WriteHello();
return Task.CompletedTask;
}
}
#endregion
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using NServiceBus;

public class MyMessage :
IMessage
{
}
16 changes: 16 additions & 0 deletions samples/dependency-injection/aspnetcore/Core_8/Sample/MyService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Logging;

public class MyService
{
readonly ILogger logger;

public MyService(ILogger<MyService> logger)
{
this.logger = logger;
}

public void WriteHello()
{
logger.LogInformation("Hello from MyService.");
}
}
35 changes: 35 additions & 0 deletions samples/dependency-injection/aspnetcore/Core_8/Sample/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System;
using Microsoft.AspNetCore.Hosting;
using System.Threading.Tasks;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NServiceBus;

static class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.StartAsync();

Console.WriteLine("Press any key to shutdown");
Console.ReadKey();
await host.StopAsync();
}

static IHostBuilder CreateHostBuilder(string[] args) =>
#region ContainerConfiguration
Host.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddSingleton<MyService>();
})
.UseNServiceBus(c =>
{
var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseTransport<LearningTransport>();
return endpointConfiguration;
})
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
#endregion
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52774/",
"sslPort": 0
}
},
"profiles": {
"Sample.Core": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:52775/"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.0.0-beta.1" />
<PackageReference Include="NServiceBus" Version="8.0.0-beta.1" />
</ItemGroup>
</Project>
46 changes: 46 additions & 0 deletions samples/dependency-injection/aspnetcore/Core_8/Sample/Startup.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using NServiceBus;

public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddConsole());
services.AddSingleton<MyService>();
}

public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
applicationBuilder.UseDeveloperExceptionPage();
}

#region RequestHandling

applicationBuilder.Run(
handler: context =>
{
if (context.Request.Path != "/")
{
// only handle requests at the root
return Task.CompletedTask;
}
var applicationServices = applicationBuilder.ApplicationServices;
var endpointInstance = applicationServices.GetService<IMessageSession>();
var myMessage = new MyMessage();

return Task.WhenAll(
endpointInstance.SendLocal(myMessage),
context.Response.WriteAsync("Message sent"));
});

#endregion
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System.Threading.Tasks;
using NServiceBus;

public class MyHandler :
IHandleMessages<MyMessage>
{
MyService myService;

public MyHandler(MyService myService)
{
this.myService = myService;
}

public Task Handle(MyMessage message, IMessageHandlerContext context)
{
myService.WriteHello();
return Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
using NServiceBus;

public class MyMessage :
IMessage
{
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Microsoft.Extensions.Logging;

public class MyService
{
readonly ILogger logger;

public MyService(ILogger<MyService> logger)
{
this.logger = logger;
}

public void WriteHello()
{
logger.LogInformation("Hello from MyService.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using Microsoft.AspNetCore.Hosting;
using System.Threading.Tasks;
using Autofac.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using NServiceBus;

static class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
await host.StartAsync();

Console.WriteLine("Press any key to shutdown");
Console.ReadKey();
await host.StopAsync();
}

static IHostBuilder CreateHostBuilder(string[] args) =>
#region ServiceProviderFactoryAutofac
Host.CreateDefaultBuilder(args)
.UseServiceProviderFactory(new AutofacServiceProviderFactory())
#endregion
.UseNServiceBus(c =>
{
var endpointConfiguration = new EndpointConfiguration("Sample.Core");
endpointConfiguration.UseTransport<LearningTransport>();
return endpointConfiguration;
})
.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>());
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:52774/",
"sslPort": 0
}
},
"profiles": {
"Sample.Core": {
"commandName": "Project",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "http://localhost:52775/"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<LangVersion>7.3</LangVersion>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="7.2.0" />
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.0.0-beta.1" />
<PackageReference Include="NServiceBus" Version="8.0.0-beta.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using NServiceBus;
using System.Threading.Tasks;
using Autofac;
using Microsoft.Extensions.Hosting;

public class Startup
{
#region ContainerConfigurationAutofac
public void ConfigureServices(IServiceCollection services)
{
services.AddLogging(loggingBuilder => loggingBuilder.AddConsole());
}

public void ConfigureContainer(ContainerBuilder containerBuilder)
{
containerBuilder.RegisterType<MyService>().SingleInstance();
}
#endregion

public void Configure(IApplicationBuilder applicationBuilder, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
applicationBuilder.UseDeveloperExceptionPage();
}

applicationBuilder.Run(
handler: context =>
{
if (context.Request.Path != "/")
{
// only handle requests at the root
return Task.CompletedTask;
}

var applicationServices = applicationBuilder.ApplicationServices;
var endpointInstance = applicationServices.GetService<IMessageSession>();
var myMessage = new MyMessage();

return Task.WhenAll(
endpointInstance.SendLocal(myMessage),
context.Response.WriteAsync("Message sent"));
});
}
}
Empty file.