Basic service configuration

This commit is contained in:
danielgrabowski 2019-10-31 12:53:57 +01:00
parent bca6a14f1a
commit 7348833ab8
7 changed files with 128 additions and 20 deletions

View File

@ -0,0 +1,10 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Squirrowse.Service.Hubs
{
public interface IStreamHub
{
Task UploadByteStream(IAsyncEnumerable<byte[]> stream);
}
}

View File

@ -0,0 +1,29 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.Logging;
using NLog;
namespace Squirrowse.Service.Hubs
{
public class StreamHub : Hub, IStreamHub
{
private readonly ILogger<StreamHub> logger;
public StreamHub(ILogger<StreamHub> logger)
{
this.logger = logger;
}
public async Task UploadByteStream(IAsyncEnumerable<byte[]> stream)
{
await foreach (var frame in stream)
{
logger.LogInformation($"Got frame size: {frame.Length} ");
await Task.Delay(100); //leave some delay for debug purpose
}
}
}
}

View File

@ -1,26 +1,68 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using NLog;
using NLog.Extensions.Logging;
using NLog.Web;
namespace Squirrowse.Service namespace Squirrowse.Service
{ {
public class Program public class Program
{ {
public static IConfigurationRoot Configuration { get; set; }
public static void Main(string[] args) public static void Main(string[] args)
{ {
CreateHostBuilder(args).Build().Run(); Configuration = CreateConfigurationBuilder().Build();
var logger = GetLogger();
try
{
logger.Debug("Application started");
CreateHostBuilder(args)
.Build()
.Run();
}
catch (Exception ex)
{
logger.Error(ex, "Stopped program because of exception when building WebHost");
throw;
}
finally
{
LogManager.Shutdown();
}
} }
public static IHostBuilder CreateHostBuilder(string[] args) => public static IHostBuilder CreateHostBuilder(string[] args)
Host.CreateDefaultBuilder(args) {
return Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder => .ConfigureWebHostDefaults(webBuilder =>
{ {
webBuilder.UseStartup<Startup>(); webBuilder.UseStartup<Startup>();
webBuilder.UseConfiguration(Configuration);
webBuilder.UseNLog();
}); });
}
private static IConfigurationBuilder CreateConfigurationBuilder()
{
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", false, true)
.AddJsonFile($"appsettings.{environment}.json", false, true);
}
private static ILogger GetLogger()
{
var nlogConfigSection = Configuration.GetSection("NLog");
LogManager.Configuration = new NLogLoggingConfiguration(nlogConfigSection);
ILogger logger = LogManager.GetCurrentClassLogger();
return logger;
}
} }
} }

View File

@ -11,8 +11,8 @@
"profiles": { "profiles": {
"IIS Express": { "IIS Express": {
"commandName": "IISExpress", "commandName": "IISExpress",
"launchBrowser": true, "launchBrowser": false,
"launchUrl": "weatherforecast", "launchUrl": "",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@ -11,6 +11,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.App" Version="3.0.0-*" /> <PackageReference Include="Microsoft.AspNetCore.App" Version="3.0.0-*" />
<PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.0.0" /> <PackageReference Include="Microsoft.AspNetCore.SignalR.Protocols.MessagePack" Version="3.0.0" />
<PackageReference Include="NLog.Web.AspNetCore" Version="4.9.0" />
<PackageReference Include="System.Reactive.Linq" Version="4.0.0" /> <PackageReference Include="System.Reactive.Linq" Version="4.0.0" />
<PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" /> <PackageReference Include="OpenCvSharp4.Windows" Version="4.1.1.20191021" />
</ItemGroup> </ItemGroup>

View File

@ -1,15 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging; using Squirrowse.Service.Hubs;
namespace Squirrowse.Service namespace Squirrowse.Service
{ {
@ -26,15 +20,15 @@ namespace Squirrowse.Service
public void ConfigureServices(IServiceCollection services) public void ConfigureServices(IServiceCollection services)
{ {
services.AddControllers(); services.AddControllers();
services.AddSignalR()
.AddHubOptions<StreamHub>(opt => opt.MaximumReceiveMessageSize = 102400000)//~100mb per frame instead of 32kb default
.AddMessagePackProtocol();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{ {
if (env.IsDevelopment()) if (env.IsDevelopment()) app.UseDeveloperExceptionPage();
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection(); app.UseHttpsRedirection();
@ -45,6 +39,7 @@ namespace Squirrowse.Service
app.UseEndpoints(endpoints => app.UseEndpoints(endpoints =>
{ {
endpoints.MapControllers(); endpoints.MapControllers();
endpoints.MapHub<StreamHub>($"{nameof(StreamHub)}");
}); });
} }
} }

View File

@ -6,5 +6,36 @@
"Microsoft.Hosting.Lifetime": "Information" "Microsoft.Hosting.Lifetime": "Information"
} }
}, },
"NLog": {
"autoreload": true,
"throwConfigExceptions": true,
"variables": {
"logDirectory": "${basedir}/logs"
},
"targets": {
"FileLogger": {
"type": "AsyncWrapper",
"target": {
"wrappedFile": {
"type": "File",
"fileName": "${logDirectory}/${machinename}.Log.txt",
"layout": "${longdate} ${level}: ${message} ${exception:format=tostring}",
"archiveFileName": "${logDirectory}/archives/${machinename}.Log_{#}.txt",
"archiveDateFormat": "yyyy-MM-dd",
"archiveAboveSize": "5242880",
"archiveEvery": "Day",
"archiveNumbering": "DateAndSequence",
"maxArchiveFiles": "90"
}
}
}
},
"rules": [
{
"logger": "*",
"writeTo": "FileLogger"
}
]
},
"AllowedHosts": "*" "AllowedHosts": "*"
} }