You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.6 KiB
C#

1 year ago
using Admin.Core.Common;
using Admin.Core.Socket;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using TouchSocket;
using TouchSocket.Core;
using TouchSocket.Http;
using TouchSocket.Sockets;
namespace Admin.Core.Extensions
{
public static class TouchSocketSetup
{
public static void AddTouchSocketSetup(this IServiceCollection services)
{
if (services == null) throw new ArgumentNullException(nameof(services));
//services.AddSingleton<ITcpService, TcpService>();
services.AddSingleton<IHttpService, HttpService>();
//services.AddSingleton<ConsoleAction>();
services.AddSingleton<ITouchSocketService, TouchSocketService>();
//任务注入
var baseType = typeof(IHttpService);
var path = AppDomain.CurrentDomain.RelativeSearchPath ?? AppDomain.CurrentDomain.BaseDirectory;
var referencedAssemblies = System.IO.Directory.GetFiles(path, "Admin.Core.Socket.dll").Select(Assembly.LoadFrom).ToArray();
var types = referencedAssemblies
.SelectMany(a => a.DefinedTypes)
.Select(type => type.AsType())
.Where(x => x != baseType && baseType.IsAssignableFrom(x)).ToArray();
var implementTypes = types.Where(x => x.IsClass).ToArray();
foreach (var implementType in implementTypes)
{
services.AddTransient(implementType);
}
}
}
}