public void Setup() { IEnumerable <ServiceDescriptor> serviceDescriptors = new ServiceDescriptor[0]; subject = new Mock <IServiceCollection>(); subject .Setup(instance => instance.GetEnumerator()) .Returns(serviceDescriptors.GetEnumerator()); ServiceExtensions.RegisterServices(subject.Object, new[] { typeof(ITransientInterface).Assembly }); }
public void Setup() { IEnumerable <ServiceDescriptor> serviceDescriptors = new ServiceDescriptor[0]; services = new Mock <IServiceCollection>(); services .Setup(instance => instance.GetEnumerator()) .Returns(serviceDescriptors.GetEnumerator()); ServiceDeclaration declaration = new ServiceDeclaration(typeof(ITransientInterface), typeof(TestImplementation), declartionScope); ServiceExtensions.AddService(services.Object, declaration); }
static void Main(string[] args) { var services = new ServiceCollection(); services.AddTransient <IXMLWriter, XMLWriter>(); services.AddTransient <IXMLWriter, XMLWriter2>(); services.AddTransient <IJSONWriter, JSONWriter>(); services.AddTransient <IJSONWriter, JSONWriter2>(); var provider = services.BuildServiceProvider(); Console.WriteLine("Dependency Injection Demo (2)"); Console.WriteLine("Mapping Interfaces to instance classes"); Console.WriteLine("--------------------------------------"); Console.WriteLine("Please, select message format (1):XML // (2):JSON"); var res = Console.ReadLine(); if (res == "1") { var XMLInstance = provider.GetService <IXMLWriter>(); XMLInstance.WriteXML(); } else { var JSONInstance = provider.GetService <IJSONWriter>(); JSONInstance.WriteJSON(); } /* Invoke registered services */ var registeredXMLServices = provider.GetServices <IXMLWriter>(); foreach (var svc in registeredXMLServices) { Console.WriteLine(svc.ToString()); svc.WriteXML(); } /* Information about services via ServiceDescriptor */ var myServiceArray = new ServiceDescriptor[services.Count]; //Copy the services into an array. services.CopyTo(myServiceArray, 0); IEnumerator myEnumerator = myServiceArray.GetEnumerator(); Console.WriteLine("The Implementation Types in the collection are:"); while (myEnumerator.MoveNext()) { var myService1 = (ServiceDescriptor)myEnumerator.Current; Console.WriteLine(myService1.ImplementationType); } /* Description of properties in the service collection */ foreach (var svc in services) { Console.WriteLine($"Type: {svc.ImplementationType} \n" + $"Lifetime: {svc.Lifetime} \n" + $"Service Type: {svc.ServiceType}"); } Console.ReadLine(); }