Skip to content

Commit f280b67

Browse files
Web app sample docs review (#6957)
* Fix v8 * Fix v9 * Update sample.md
1 parent c4e2da6 commit f280b67

File tree

13 files changed

+93
-132
lines changed

13 files changed

+93
-132
lines changed

samples/web/asp-web-application/Core_8/Server/CommandMessageHandler.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,17 @@
11
using NServiceBus;
22
using System.Threading.Tasks;
3-
using NServiceBus.Logging;
3+
using Microsoft.Extensions.Logging;
44

55
#region Handler
66

7-
public class CommandMessageHandler :
7+
public class CommandMessageHandler(ILogger<CommandMessageHandler> logger) :
88
IHandleMessages<Command>
99
{
10-
static ILog log = LogManager.GetLogger<CommandMessageHandler>();
11-
1210
public Task Handle(Command message, IMessageHandlerContext context)
1311
{
14-
log.Info("Hello from CommandMessageHandler");
15-
Task reply;
16-
if (message.Id%2 == 0)
17-
{
18-
reply = context.Reply(ErrorCodes.Fail);
19-
}
20-
else
21-
{
22-
reply = context.Reply(ErrorCodes.None);
23-
}
24-
return reply;
12+
logger.LogInformation("Hello from CommandMessageHandler");
13+
14+
return context.Reply(message.Id%2 == 0 ? ErrorCodes.Fail : ErrorCodes.None);
2515
}
2616
}
2717

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
using System;
2-
using System.Threading.Tasks;
32
using NServiceBus;
3+
using Microsoft.Extensions.Hosting;
44

5-
class Program
5+
Console.Title = "Server";
6+
7+
var builder = Host.CreateDefaultBuilder(args);
8+
9+
builder.UseNServiceBus(_ =>
610
{
7-
public static async Task Main()
8-
{
9-
var endpointConfiguration = new EndpointConfiguration(Console.Title = "AsyncPagesServer");
10-
endpointConfiguration.EnableCallbacks(makesRequests: false);
11-
endpointConfiguration.UseTransport(new LearningTransport());
11+
var endpointConfiguration = new EndpointConfiguration("Samples.AsyncPages.Server");
12+
endpointConfiguration.EnableCallbacks(makesRequests: false);
13+
endpointConfiguration.UseTransport(new LearningTransport());
14+
15+
return endpointConfiguration;
16+
});
1217

13-
var endpointInstance = await Endpoint.Start(endpointConfiguration);
14-
Console.WriteLine("Press any key to exit");
15-
Console.ReadKey();
16-
await endpointInstance.Stop();
17-
}
18-
}
18+
await builder.Build().RunAsync();
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<OutputType>Exe</OutputType>
4-
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
5-
<LangVersion>10.0</LangVersion>
4+
<TargetFramework>net8.0</TargetFramework>
5+
<LangVersion>12.0</LangVersion>
66
</PropertyGroup>
77
<ItemGroup>
88
<ProjectReference Include="..\Shared\Shared.csproj" />
99
</ItemGroup>
1010
<ItemGroup>
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="2.*" />
1112
<PackageReference Include="NServiceBus.Callbacks" Version="4.*" />
1213
</ItemGroup>
1314
</Project>

samples/web/asp-web-application/Core_8/Shared/Shared.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
4-
<LangVersion>10.0</LangVersion>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<LangVersion>12.0</LangVersion>
55
</PropertyGroup>
66
<ItemGroup>
77
<PackageReference Include="NServiceBus" Version="8.*" />

samples/web/asp-web-application/Core_8/WebApp/Pages/Index.cshtml.cs

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,46 +4,38 @@
44
using Microsoft.AspNetCore.Mvc.RazorPages;
55
using NServiceBus;
66

7-
namespace WebApp.Pages
7+
namespace WebApp.Pages;
8+
9+
[IgnoreAntiforgeryToken]
10+
public class IndexModel(IMessageSession messageSession) : PageModel
811
{
9-
[IgnoreAntiforgeryToken]
10-
public class IndexModel : PageModel
12+
public string ResponseText { get; set; }
13+
14+
public async Task<IActionResult> OnPostAsync(string textField)
1115
{
12-
IMessageSession messageSession;
16+
if (string.IsNullOrWhiteSpace(textField))
17+
{
18+
return Page();
19+
}
1320

14-
public string ResponseText { get; set; }
21+
#region ActionHandling
1522

16-
public IndexModel(IMessageSession messageSession)
23+
if (!int.TryParse(textField, out var number))
1724
{
18-
this.messageSession = messageSession;
25+
return Page();
1926
}
20-
21-
public async Task<IActionResult> OnPostAsync(string textField)
27+
var command = new Command
2228
{
23-
if (string.IsNullOrWhiteSpace(textField))
24-
{
25-
return Page();
26-
}
27-
28-
#region ActionHandling
29-
30-
if (!int.TryParse(textField, out var number))
31-
{
32-
return Page();
33-
}
34-
var command = new Command
35-
{
36-
Id = number
37-
};
29+
Id = number
30+
};
3831

39-
var sendOptions = new SendOptions();
40-
sendOptions.SetDestination("Samples.AsyncPages.Server");
32+
var sendOptions = new SendOptions();
33+
sendOptions.SetDestination("Samples.AsyncPages.Server");
4134

42-
var code = await messageSession.Request<ErrorCodes>(command, sendOptions);
43-
ResponseText = Enum.GetName(typeof(ErrorCodes), code);
35+
var code = await messageSession.Request<ErrorCodes>(command, sendOptions);
36+
ResponseText = Enum.GetName(typeof(ErrorCodes), code);
4437

45-
return Page();
46-
#endregion
47-
}
38+
return Page();
39+
#endregion
4840
}
49-
}
41+
}

samples/web/asp-web-application/Core_8/WebApp/Program.cs

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using NServiceBus;
44

5-
class Program
5+
#region ApplicationStart
6+
7+
var builder = WebApplication.CreateBuilder();
8+
9+
builder.Host.UseNServiceBus(_ =>
610
{
7-
public static void Main()
8-
{
9-
#region ApplicationStart
10-
var builder = WebApplication.CreateBuilder();
11-
12-
builder.Host.UseNServiceBus(context =>
13-
{
14-
var endpointConfiguration = new EndpointConfiguration("Samples.AsyncPages.WebApplication");
15-
endpointConfiguration.MakeInstanceUniquelyAddressable("1");
16-
endpointConfiguration.EnableCallbacks();
17-
endpointConfiguration.UseTransport(new LearningTransport());
18-
return endpointConfiguration;
19-
});
20-
#endregion
21-
22-
builder.Services.AddRazorPages();
23-
24-
var app = builder.Build();
25-
26-
app.MapRazorPages();
27-
28-
app.Run();
29-
}
30-
}
11+
var endpointConfiguration = new EndpointConfiguration("Samples.AsyncPages.WebApplication");
12+
endpointConfiguration.MakeInstanceUniquelyAddressable("1");
13+
endpointConfiguration.EnableCallbacks();
14+
endpointConfiguration.UseTransport(new LearningTransport());
15+
return endpointConfiguration;
16+
});
17+
18+
#endregion
19+
20+
builder.Services.AddRazorPages();
21+
22+
var app = builder.Build();
23+
24+
app.MapRazorPages();
25+
26+
await app.RunAsync();

samples/web/asp-web-application/Core_8/WebApp/WebApp.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22
<PropertyGroup>
3-
<TargetFrameworks>net8.0;net6.0</TargetFrameworks>
4-
<LangVersion>10.0</LangVersion>
3+
<TargetFramework>net8.0</TargetFramework>
4+
<LangVersion>12.0</LangVersion>
55
</PropertyGroup>
66
<ItemGroup>
77
<ProjectReference Include="..\Shared\Shared.csproj" />

samples/web/asp-web-application/Core_9/Server/CommandMessageHandler.cs

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
using NServiceBus;
22
using System.Threading.Tasks;
3+
using Microsoft.Extensions.Logging;
34
using NServiceBus.Logging;
45

56
#region Handler
67

7-
public class CommandMessageHandler :
8+
public class CommandMessageHandler(ILogger<CommandMessageHandler> logger) :
89
IHandleMessages<Command>
910
{
10-
static ILog log = LogManager.GetLogger<CommandMessageHandler>();
11-
1211
public Task Handle(Command message, IMessageHandlerContext context)
1312
{
14-
log.Info("Hello from CommandMessageHandler");
15-
Task reply;
16-
if (message.Id%2 == 0)
17-
{
18-
reply = context.Reply(ErrorCodes.Fail);
19-
}
20-
else
21-
{
22-
reply = context.Reply(ErrorCodes.None);
23-
}
24-
return reply;
13+
logger.LogInformation("Hello from CommandMessageHandler");
14+
15+
return context.Reply(message.Id%2 == 0 ? ErrorCodes.Fail : ErrorCodes.None);
2516
}
2617
}
2718

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
using System;
2-
using System.Threading.Tasks;
32
using NServiceBus;
3+
using Microsoft.Extensions.Hosting;
44

5-
class Program
6-
{
7-
public static async Task Main()
8-
{
9-
var endpointConfiguration = new EndpointConfiguration(Console.Title = "AsyncPagesServer");
10-
endpointConfiguration.EnableCallbacks(makesRequests: false);
11-
endpointConfiguration.UseTransport(new LearningTransport());
12-
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
5+
Console.Title = "Server";
136

14-
var endpointInstance = await Endpoint.Start(endpointConfiguration);
15-
Console.WriteLine("Press any key to exit");
16-
Console.ReadKey();
17-
await endpointInstance.Stop();
18-
}
19-
}
7+
var builder = Host.CreateApplicationBuilder(args);
8+
9+
var endpointConfiguration = new EndpointConfiguration("Samples.AsyncPages.Server");
10+
endpointConfiguration.EnableCallbacks(makesRequests: false);
11+
endpointConfiguration.UseTransport(new LearningTransport());
12+
endpointConfiguration.UseSerialization<SystemJsonSerializer>();
13+
14+
builder.UseNServiceBus(endpointConfiguration);
15+
16+
await builder.Build().RunAsync();

samples/web/asp-web-application/Core_9/Server/Server.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
<ProjectReference Include="..\Shared\Shared.csproj" />
99
</ItemGroup>
1010
<ItemGroup>
11+
<PackageReference Include="NServiceBus.Extensions.Hosting" Version="3.*" />
1112
<PackageReference Include="NServiceBus.Callbacks" Version="5.*" />
1213
</ItemGroup>
1314
</Project>

0 commit comments

Comments
 (0)