public override IServiceProvider GetServiceProvider(IServiceProvider serviceProvider = null) { try { if (serviceProvider == null) { serviceProvider = this.serviceProviderFactory != null?this.serviceProviderFactory.Invoke() : serviceCollection.BuildServiceProvider(); } PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>(); IPacketHandlers packetHandlers = serviceProvider.GetService <IPacketHandlers>(); foreach (var packetHandlerModule in this.modules) { foreach (var packetHandler in packetHandlerModule.GetPacketHandlers()) { packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default", (IPacketHandler)serviceProvider.GetService(packetHandler.Value)); } } if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1) { throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type"); } } catch (Exception ex) { } return(serviceProvider); }
protected IServiceProvider GetServiceProvider() { var serviceProvider = this.serviceProviderFactory != null?serviceProviderFactory.Invoke() : this.serviceCollection.BuildServiceProvider(); PacketSerialiserProvider.PacketSerialiser = serviceProvider.GetService <IPacketSerialiser>(); serviceProvider.GetService <ILogLevelProvider>().SetLogLevel(this.options.LogLevel); IPacketHandlers packetHandlers = serviceProvider.GetService <IPacketHandlers>(); foreach (var packetHandlerModule in this.modules) { foreach (var packetHandler in packetHandlerModule.GetPacketHandlers()) { packetHandlers.Add(PacketSerialiserProvider.PacketSerialiser.CanReadName ? packetHandler.Key.Name : "Default", (IPacketHandler)serviceProvider.GetService(packetHandler.Value)); } } if (!PacketSerialiserProvider.PacketSerialiser.CanReadName && packetHandlers.GetPacketHandlers().Count > 1) { throw new Exception("A PacketSerialiser which cannot identify a packet can only support up to one packet type"); } return(serviceProvider); }