1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-02 16:42:50 -05:00

upgrade to aspnet core 3.1

This commit is contained in:
Kyle Spearrin
2020-01-10 08:33:13 -05:00
parent 8026912eeb
commit 29580684a3
60 changed files with 429 additions and 420 deletions

View File

@ -6,8 +6,7 @@ using Bit.Core;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
using Azure.Storage.Queues;
namespace Bit.Notifications
{
@ -19,7 +18,7 @@ namespace Bit.Notifications
private Task _executingTask;
private CancellationTokenSource _cts;
private CloudQueue _queue;
private QueueClient _queueClient;
public AzureQueueHostedService(
ILogger<AzureQueueHostedService> logger,
@ -55,33 +54,29 @@ namespace Bit.Notifications
private async Task ExecuteAsync(CancellationToken cancellationToken)
{
var storageAccount = CloudStorageAccount.Parse(_globalSettings.Notifications.ConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
_queue = queueClient.GetQueueReference("notifications");
_queueClient = new QueueClient(_globalSettings.Notifications.ConnectionString, "notifications");
while(!cancellationToken.IsCancellationRequested)
{
try
{
var messages = await _queue.GetMessagesAsync(32, TimeSpan.FromMinutes(1),
null, null, cancellationToken);
if(messages.Any())
var messages = await _queueClient.ReceiveMessagesAsync(32);
if(messages.Value?.Any() ?? false)
{
foreach(var message in messages)
foreach(var message in messages.Value)
{
try
{
await HubHelpers.SendNotificationToHubAsync(
message.AsString, _hubContext, cancellationToken);
await _queue.DeleteMessageAsync(message);
message.MessageText, _hubContext, cancellationToken);
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
}
catch(Exception e)
{
_logger.LogError("Error processing dequeued message: " +
$"{message.Id} x{message.DequeueCount}.", e);
$"{message.MessageId} x{message.DequeueCount}.", e);
if(message.DequeueCount > 2)
{
await _queue.DeleteMessageAsync(message);
await _queueClient.DeleteMessageAsync(message.MessageId, message.PopReceipt);
}
}
}

View File

@ -1,4 +1,4 @@
FROM mcr.microsoft.com/dotnet/core/aspnet:2.2
FROM mcr.microsoft.com/dotnet/core/aspnet:3.1
LABEL com.bitwarden.product="bitwarden"

View File

@ -2,15 +2,13 @@
<PropertyGroup>
<Version>1.33.0</Version>
<TargetFramework>netcoreapp2.2</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Bit.Notifications</RootNamespace>
<UserSecretsId>bitwarden-Notifications</UserSecretsId>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="1.1.5" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.1.0" />
<PackageReference Include="Microsoft.Azure.SignalR" Version="1.0.12" />
</ItemGroup>

View File

@ -1,7 +1,7 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Hosting;
using Bit.Core.Utilities;
using Serilog.Events;
using Microsoft.Extensions.Hosting;
namespace Bit.Notifications
{
@ -9,39 +9,42 @@ namespace Bit.Notifications
{
public static void Main(string[] args)
{
WebHost
Host
.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.ConfigureLogging((hostingContext, logging) =>
logging.AddSerilog(hostingContext, e =>
{
var context = e.Properties["SourceContext"].ToString();
if(context.Contains("IdentityServer4.Validation.TokenValidator") ||
context.Contains("IdentityServer4.Validation.TokenRequestValidator"))
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.UseStartup<Startup>();
webBuilder.ConfigureLogging((hostingContext, logging) =>
logging.AddSerilog(hostingContext, e =>
{
return e.Level > LogEventLevel.Error;
}
var context = e.Properties["SourceContext"].ToString();
if(context.Contains("IdentityServer4.Validation.TokenValidator") ||
context.Contains("IdentityServer4.Validation.TokenRequestValidator"))
{
return e.Level > LogEventLevel.Error;
}
if(e.Level == LogEventLevel.Error &&
e.MessageTemplate.Text == "Failed connection handshake.")
{
return false;
}
if(e.Level == LogEventLevel.Error &&
e.MessageTemplate.Text == "Failed connection handshake.")
{
return false;
}
if(e.Level == LogEventLevel.Error &&
e.MessageTemplate.Text.StartsWith("Failed writing message."))
{
return false;
}
if(e.Level == LogEventLevel.Error &&
e.MessageTemplate.Text.StartsWith("Failed writing message."))
{
return false;
}
if(e.Level == LogEventLevel.Warning &&
e.MessageTemplate.Text.StartsWith("Heartbeat took longer"))
{
return false;
}
if(e.Level == LogEventLevel.Warning &&
e.MessageTemplate.Text.StartsWith("Heartbeat took longer"))
{
return false;
}
return e.Level >= LogEventLevel.Warning;
}))
return e.Level >= LogEventLevel.Warning;
}));
})
.Build()
.Run();
}

View File

@ -8,13 +8,14 @@ using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.IdentityModel.Logging;
namespace Bit.Notifications
{
public class Startup
{
public Startup(IHostingEnvironment env, IConfiguration configuration)
public Startup(IWebHostEnvironment env, IConfiguration configuration)
{
CultureInfo.DefaultThreadCurrentCulture = new CultureInfo("en-US");
Configuration = configuration;
@ -22,7 +23,7 @@ namespace Bit.Notifications
}
public IConfiguration Configuration { get; }
public IHostingEnvironment Environment { get; set; }
public IWebHostEnvironment Environment { get; set; }
public void ConfigureServices(IServiceCollection services)
{
@ -81,8 +82,8 @@ namespace Bit.Notifications
public void Configure(
IApplicationBuilder app,
IHostingEnvironment env,
IApplicationLifetime appLifetime,
IWebHostEnvironment env,
IHostApplicationLifetime appLifetime,
GlobalSettings globalSettings)
{
IdentityModelEventSource.ShowPII = true;
@ -93,29 +94,38 @@ namespace Bit.Notifications
app.UseDeveloperExceptionPage();
}
// Add routing
app.UseRouting();
// Add Cors
app.UseCors(policy => policy.SetIsOriginAllowed(h => true)
.AllowAnyMethod().AllowAnyHeader().AllowCredentials());
// Add authentication to the request pipeline.
app.UseAuthentication();
app.UseAuthorization();
// Add SignlarR
if(!string.IsNullOrWhiteSpace(globalSettings.Notifications?.AzureSignalRConnectionString))
var useAzureSignalR = !string.IsNullOrWhiteSpace(
globalSettings.Notifications?.AzureSignalRConnectionString);
if(useAzureSignalR)
{
app.UseAzureSignalR(routes => routes.MapHub<NotificationsHub>("/hub"));
}
else
{
app.UseSignalR(routes => routes.MapHub<NotificationsHub>("/hub", options =>
{
options.ApplicationMaxBufferSize = 2048; // client => server messages are not even used
options.TransportMaxBufferSize = 4096;
}));
}
// Add MVC to the request pipeline.
app.UseMvc();
// Add endpoints to the request pipeline.
app.UseEndpoints(endpoints =>
{
if(!useAzureSignalR)
{
endpoints.MapHub<NotificationsHub>("/hub", options =>
{
options.ApplicationMaxBufferSize = 2048; // client => server messages are not even used
options.TransportMaxBufferSize = 4096;
});
}
endpoints.MapDefaultControllerRoute();
});
}
}
}