Quantcast
Channel: Active questions tagged nuget-package - Stack Overflow
Viewing all articles
Browse latest Browse all 3067

How to get all dependencies of required Service with IServiceCollector extension

$
0
0

My Modules :

    public class CoreModule : ICoreModule    {        public void Load(IServiceCollection collection)        {            collection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();            collection.AddScoped<INotyfService, NotyfService>();        }    }

My Service Collection Extension :

    public static class ServiceCollectionExtensions    {        public static IServiceCollection AddDependencyResolvers(this IServiceCollection serviceCollection, ICoreModule[] modules)        {            foreach (var module in modules)            {                module.Load(serviceCollection);            }            return ServiceTool.Create(serviceCollection);        }    }

I Tried to get notyfService instance but it require more than 1 service.My Aspect Class :

public class NotificationAspectAttribute : MethodInterceptions{    private readonly INotyfService _notyf;    public NotificationAspectAttribute()    {        _notyf = ServiceTool.ServiceProvider.GetService<INotyfService>();    }    protected override async Task OnAfterAsync(IInvocation invocation)    {        try        {            if (invocation.ReturnValue is SuccessResult successResult)çalışmıyor            {                _notyf.Success(successResult.Message);            }            else if (invocation.ReturnValue is ErrorResult errorResult)            {                _notyf.Error(errorResult.Message);            }        }        catch (Exception)        {            _notyf.Error("An unexpected error occurred.");            throw;        }    }}

Program.cs :

builder.Services.AddNotyf(config =>{    config.DurationInSeconds = 5;    config.IsDismissable = true;    config.Position = NotyfPosition.BottomRight;    config.HasRippleEffect = true;});builder.Services.AddDependencyResolvers(new ICoreModule[]{    new CoreModule()});

I Tried this way :

    public class CoreModule : ICoreModule    {        public void Load(IServiceCollection collection)        {            collection.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();            collection.AddNotyf(config =>            {                config.DurationInSeconds = 5;                config.IsDismissable = true;                config.Position = NotyfPosition.BottomRight;                config.HasRippleEffect = true;            });            collection.AddScoped<INotyfService, NotyfService>();        }    }}

My NotyfService injection doesnt enough because with injection method it inject more than Service class.

Notyf Dependency Injection Class :

namespace AspNetCoreHero.ToastNotification{    public static class ServiceCollectionExtensions    {        public static void AddToastify(this IServiceCollection services, Action<ToastifyConfig> toastifyConfiguration)        {            ToastifyConfig toastifyConfig = new ToastifyConfig();            toastifyConfiguration(toastifyConfig);            ToastifyEntity implementationInstance = new ToastifyEntity(toastifyConfig.DurationInSeconds, toastifyConfig.Gravity, toastifyConfig.Position);            if (services == null)            {                throw new ArgumentNullException("services");            }            services.AddFrameworkServices();            services.AddSingleton<ITempDataService, TempDataService>();            services.AddSingleton<IToastNotificationContainer<ToastifyNotification>, TempDataToastNotificationContainer<ToastifyNotification>>();            services.AddScoped<IToastifyService, ToastifyService>();            services.AddSingleton(implementationInstance);        }        private static void AddFrameworkServices(this IServiceCollection services)        {            if (services.FirstOrDefault((ServiceDescriptor d) => d.ServiceType == typeof(ITempDataProvider)) == null)            {                services.AddSingleton<ITempDataProvider, CookieTempDataProvider>();            }            if (services.FirstOrDefault((ServiceDescriptor d) => d.ServiceType == typeof(IHttpContextAccessor)) == null)            {                services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();            }        }        public static void AddNotyf(this IServiceCollection services, Action<NotyfConfig> configure)        {            NotyfConfig notyfConfig = new NotyfConfig();            configure(notyfConfig);            NotyfEntity implementationInstance = new NotyfEntity(notyfConfig.DurationInSeconds, notyfConfig.Position, notyfConfig.IsDismissable);            if (services == null)            {                throw new ArgumentNullException("services");            }            services.AddFrameworkServices();            services.AddSingleton<ITempDataService, TempDataService>();            services.AddSingleton<IMessageContainerFactory, MessageContainerFactory>();            services.AddScoped<INotyfService, NotyfService>();            services.AddScoped<NotyfMiddleware>();            services.AddSingleton(implementationInstance);        }    }}

So my question is how can i get all required NotyfService instances with service tool and use it like constructor injection ?


Viewing all articles
Browse latest Browse all 3067

Trending Articles