From e2de55e8ef7a9380d2537e8ab6cf7611bf8901fe Mon Sep 17 00:00:00 2001 From: Hadi Khazaee Asl Date: Tue, 24 Nov 2026 08:25:21 +0330 Subject: [PATCH] last works on Keyed Services ... --- Extensions/XDIKeyedServiceExtensions.cs | 68 +++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/Extensions/XDIKeyedServiceExtensions.cs b/Extensions/XDIKeyedServiceExtensions.cs index a05a3f3..aae16cb 100644 --- a/Extensions/XDIKeyedServiceExtensions.cs +++ b/Extensions/XDIKeyedServiceExtensions.cs @@ -151,6 +151,74 @@ namespace xCommons.Extensions lifetime)); } + /// + /// Register Specified Keyed Service ... + /// + /// + /// + /// + /// + /// + public static void AddXKeyedService( + this IServiceCollection services, + string key, + TService instance, + ServiceLifetime lifetime = ServiceLifetime.Scoped + ) + where TService : class + { + // + var erorr = new XError(); + + // + var isKeyValid = !key.IsNullOrEmpty(); + var isInstanceValid = !instance.IsNull(); + var isServicesValid = !services.IsNull(); + var isServiceLifetimeValid = Enum.IsDefined(typeof(ServiceLifetime), lifetime); + + // + if (!isKeyValid || + !isInstanceValid || + !isServicesValid || + !isServiceLifetimeValid + ) + { + // + if (!isKeyValid) + { + erorr.Message = $"Key not Provided ..."; + } + + // + if (!isInstanceValid) + { + erorr.Message = $"Instance not Provided ..."; + } + + // + if (!isServicesValid) + { + erorr.Message = $"Services not Provided ..."; + } + + // + if (!isServiceLifetimeValid) + { + erorr.Message = $"invalid Service Lifetime ..."; + } + + // + erorr.Id = (int)XException.InvalidArgs; + throw erorr.ToException(); + } + + // + services.Add(new ServiceDescriptor( + typeof(XKeyedService), + sp => new XKeyedService(key, instance), + lifetime)); + } + /// /// Retrieve Registered XKeyedService ... ///