serviceCollection.Scan(t => t.FromCallingAssembly() .AddClasses(c =>…
- User
serviceCollection.Scan(t => t.FromCallingAssembly() .AddClasses(c => c.AssignableTo()) .AsImplementedInterfaces() .WithTransientLifetime());
public class RandomWeaponBoxConstructor : IBoxConstructor {}
im i right?
- Assistant
Yes, you are correct based on the "Scrutor" library syntax for scanning and registering services in .NET dependency injection. The code snippet you provided registers all classes from the calling assembly that are assignable to
IBoxConstructorwith a transient lifetime.Given that your
RandomWeaponBoxConstructorclass implements theIBoxConstructorinterface, it will be registered:csharppublic class RandomWeaponBoxConstructor : IBoxConstructor {}Here's a breakdown of what your code does:
FromCallingAssembly()- Scans the assembly that contains the call to this method.AddClasses(c => c.AssignableTo<IBoxConstructor>())- Finds all classes that can be assigned toIBoxConstructor.AsImplementedInterfaces()- Registers the found classes as their implemented interfaces.- - Sets the lifetime of these services to transient, meaning a new instance will be created each time it is requested.
- User
23:40:10 [EROR] (cssharp:Core) Error invoking callback System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.InvalidOperationException: No service for type 'PubgMod.Boxes.RandomBox.RandomWeaponBoxConstructor' has been registered. at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at PubgMod.BoxManagement.ConstructBox(CCSPlayerController controller, BoxInfo info) in H:\csharp\PubgMod\PubgMod\BoxManagement.cs:line 43 at PubgMod.BoxManagement.<>c__DisplayClass4_1.b__0(CCSPlayerController _, ChatMenuOption _) in H:\csharp\PubgMod\PubgMod\BoxManagement.cs:line 35 at CounterStrikeSharp.API.Modules.Menu.BaseMenuInstance.OnKeyPress(CCSPlayerController player, Int32 key) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Modules/Menu/BaseMenu.cs:line 132 at CounterStrikeSharp.API.Modules.Menu.MenuManager.OnKeyPress(CCSPlayerController player, Int32 key) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Modules/Menu/MenuManager.cs:line 72 at CounterStrikeSharp.API.Core.Application.<>c.b__16_0(CCSPlayerController player, CommandInfo info) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Application.cs:line 101 at CounterStrikeSharp.API.Core.Commands.CommandManager.HandleCommandInternal(Int32 playerSlot, IntPtr commandInfo) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/Commands/CommandManager.cs:line 159 at InvokeStub_Action1) at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) --- End of inner exception stack trace --- at System.Reflection.MethodBaseInvoker.InvokeWithFewArgs(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Delegate.DynamicInvokeImpl(Object[] args) at CounterStrikeSharp.API.Core.FunctionReference.b__18_0(fxScriptContext* context) in /home/runner/work/CounterStrikeSharp/CounterStrikeSharp/managed/CounterStrikeSharp.API/Core/FunctionReference.cs:line 100
- Assistant