Initial Commit;

This commit is contained in:
2024-03-10 00:43:57 +07:00
commit 5d80c6351c
29 changed files with 797 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
using System.Reflection;
using InServiceQue.Core.Models;
using InServiceQue.Core.Repositories;
using InServiceQue.InMemory;
using InServiceQue.Services;
using Microsoft.Extensions.DependencyInjection.Extensions;
namespace InServiceQue.Sample;
public static class DIExtensions
{
public static IServiceCollection RegisterInternals(this IServiceCollection services)
{
services.AddSingleton<ITypeRegistry, QueueTypeRegistry>();
services.AddTransient<ITaskRepository, TaskRepositoryInMemory>();
return services;
}
public static IServiceCollection RegisterQueues(this IServiceCollection services)
{
using var sp = services.BuildServiceProvider();
// find all types in the assembly that implement IQueueHandler<T>
var queueTypes = Assembly.GetExecutingAssembly().GetTypes()
.Where(t => t.IsClass && !t.IsAbstract && t.GetInterfaces()
.Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IQueueHandler<>)));
var hostedServiceRegistrator = new HostedServiceRegistrator();
// register each query type with its corresponding interface
foreach (var queueType in queueTypes)
{
// get the T from IQueueHandler<T>
var type = queueType.GetInterfaces()
.Single(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IQueueHandler<>));
// register the query type as a scoped service with its corresponding interface
services.AddScoped(queueType);
var typeRegistry = sp.GetRequiredService<ITypeRegistry>();
typeRegistry.RegisterTaskType(type.GenericTypeArguments.First().Name, type);
services.AddScoped(typeof(IQueueHandler<>)
.MakeGenericType(type.GenericTypeArguments), queueType);
//todo: bug here
var hostedServiceType = typeof(QueueService<>).MakeGenericType(type.GenericTypeArguments);
hostedServiceRegistrator.RegisterHostedService(services, hostedServiceType);
// services.AddSingleton(typeof(IQueueService<>)
// .MakeGenericType(type.GenericTypeArguments), );
}
return services;
}
}

View File

@@ -0,0 +1,19 @@
using System.Reflection;
namespace InServiceQue.Sample;
public class HostedServiceRegistrator
{
public void RegisterHostedService(IServiceCollection services, Type hostedServiceType)
{
Type servicesType = typeof(HostedServiceRegistrator);
MethodInfo methodInfo = servicesType.GetMethod("AddHostedService");
MethodInfo genericMethod = methodInfo.MakeGenericMethod(hostedServiceType);
genericMethod.Invoke(this, new object[] { services });
}
// Needed as a work-arround because we can't call the extension method with reflection.
public IServiceCollection AddHostedService<THostedService>(IServiceCollection services)
where THostedService : class, IHostedService =>
services.AddHostedService<THostedService>();
}

View File

@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\InServiceQue.Core\InServiceQue.Core.csproj" />
<ProjectReference Include="..\InServiceQue.InMemory\InServiceQue.InMemory.csproj" />
<ProjectReference Include="..\InServiceQue.Postgres\InServiceQue.Postgres.csproj" />
<ProjectReference Include="..\InServiceQue\InServiceQue.csproj" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,14 @@
using InServiceQue.Core.Models;
using InServiceQue.Core.Repositories;
using InServiceQue.Sample;
var builder = WebApplication.CreateBuilder(args);
builder.Services.RegisterInternals();
builder.Services.RegisterQueues();
var app = builder.Build();
app.MapGet("/", (string msg) => app.Services.GetService<ITaskRepository>().Insert(new QueueTask(new SendMessageTask(new SendMessagePayload(){To = "John", From = "Garry", Message = msg}))));
app.Run();

View File

@@ -0,0 +1,37 @@
{
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:61951",
"sslPort": 44361
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:5121",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:7149;http://localhost:5121",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}

View File

@@ -0,0 +1,17 @@
using InServiceQue.Services;
namespace InServiceQue.Sample;
public class SendMessageHandler: IQueueHandler<SendMessageTask>
{
public bool Handle(string payload)
{
throw new NotImplementedException();
}
public Task<bool> HandleAsync(string payload)
{
Console.WriteLine(payload);
return Task.FromResult<bool>(true);
}
}

View File

@@ -0,0 +1,33 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using InServiceQue.Core.Models;
namespace InServiceQue.Sample;
public class SendMessagePayload
{
public string From { get; set; }
public string To { get; set; }
public string Message { get; set; }
}
public class SendMessageTask: IQueueTask
{
private SendMessagePayload _payload;
public SendMessageTask(SendMessagePayload payload)
{
_payload = payload;
}
public string GetTypeString()
{
return nameof(SendMessageTask);
}
public string GetPayloadString()
{
return JsonSerializer.Serialize<SendMessagePayload>(_payload);
}
}

View File

@@ -0,0 +1,8 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
}
}

View File

@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}