Closed zengyu1221 closed 4 years ago
没有,需要自己针对EventCenter.ServiceInstance事件来封装. 可参考https://github.com/IKende/FastHttpApi/tree/master/Extend/BeetleX.FastHttpApi.Hosting
有内置的IServiceCollection吗?或者我该怎么让Service的构造函数支持依赖注入呢?
我简单的实现一个 仅供参考
public static class FastXrpcExtension
{
public static IServiceCollection UseXrpc(this IServiceCollection service,
bool logToConsole,LogType logType, params System.Reflection.Assembly[] assemblies)
{
XRpcSettingHandler settingHandler = new XRpcSettingHandler();
settingHandler.Assemblies = assemblies;
settingHandler.LogToConsole = logToConsole;
settingHandler.LogTypeInfo = logType;
settingHandler.Services = service;
service.AddSingleton
}
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton
public class XRpcSettingHandler { public bool LogToConsole { get; set; }
public BeetleX.EventArgs.LogType LogTypeInfo { get; set; }
public System.Reflection.Assembly[] Assemblies { get; set; }
public IServiceCollection Services { get; set; }
}
public class XrpcServer : BackgroundService
{
public XrpcServer(XRpcSettingHandler handler)
{
_xRpcSettingHandler = handler;
}
private ServiceCollection _serviceCollection = new ServiceCollection();
private IServiceProvider _serviceProvider;
private XRpcSettingHandler _xRpcSettingHandler;
XRPCServer mXRPCServer;
protected override Task ExecuteAsync(CancellationToken stoppingToken)
{
mXRPCServer=new XRPCServer();
mXRPCServer.RPCOptions.LogToConsole = _xRpcSettingHandler.LogToConsole;
mXRPCServer.ServerOptions.LogLevel = _xRpcSettingHandler.LogTypeInfo;
mXRPCServer.EventCenter.ServiceInstance += (o, e) =>
{
// e.Service = _serviceProvider.GetService(typeof(XRPCServer));
var item=_serviceProvider.GetService(e.Type);
e.Service = item;
};
ServiceDescriptor[] items = new ServiceDescriptor[_xRpcSettingHandler.Services.Count];
_xRpcSettingHandler.Services.CopyTo(items, 0);
foreach (var item in items)
{
_serviceCollection.Insert(0,item);
}
foreach (Assembly item in _xRpcSettingHandler.Assemblies)
{
Type[] types = item.GetTypes();
foreach (Type type in types)
{
ServiceAttribute ca = type.GetCustomAttribute<ServiceAttribute>(false);
if (ca != null)
{
if (ca.SingleInstance)
{
_serviceCollection.AddSingleton(type);
}
else
{
_serviceCollection.AddScoped(type);
}
}
}
}
_serviceCollection.AddSingleton(mXRPCServer);
_serviceProvider = _serviceCollection.BuildServiceProvider();
if (_xRpcSettingHandler.Assemblies != null)
mXRPCServer.Register(_xRpcSettingHandler.Assemblies);
mXRPCServer.Open();
return Task.CompletedTask;
}
}
需要引入EventNext源码 nuget 版本跟源码不一致。导致初始化异常
使用 BeetleX.XRPC.Hosting
有内置的IServiceCollection吗?或者我该怎么让Service的构造函数支持依赖注入呢?