private static void Run_UsingUnityDI(string[] args) { using (var container = new Unity.UnityContainer().EnableDiagnostic()) { string environment = Settings.Environment(); string notificationRecipientList = Settings.NotificationRecipientList(); string sendEmailGateway = Settings.SendEmailGateway(); // -- httpclientfactory Microsoft.Extensions.DependencyInjection.ServiceCollection serviceCollection = new Microsoft.Extensions.DependencyInjection.ServiceCollection(); serviceCollection.AddHttpClient("Sender", (c) => { c.BaseAddress = new Uri(sendEmailGateway); }); serviceCollection.BuildServiceProvider(container); //var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>(); IHttpClientFactory clientFactory = container.Resolve <IHttpClientFactory>(); HttpClient httpClient = clientFactory.CreateClient("Sender"); container.RegisterSingleton <IConfig, AppConfig>( new InjectionProperty("Environment", environment) , new InjectionProperty("NotificationRecipientList", notificationRecipientList) , new InjectionProperty("SendEmailGateway", sendEmailGateway) ); container.RegisterSingleton <ILogger, Logger>(); //Sender(IConfig config, IHttpClientFactory httpClientFactory, ILogger logger) /* * container.RegisterSingleton<ISender, Sender>( * new InjectionConstructor( * new ResolvedParameter(typeof(IConfig)) * , new ResolvedParameter(typeof(IHttpClientFactory)) * , new ResolvedParameter(typeof(ILogger)) * ) * );*/ container.RegisterSingleton <ISender, Sender>(); container.RegisterType(typeof(RemoteAppenderSink)); var sink = container.Resolve <RemoteAppenderSink>(); //sink.EventsReached += (s, a) => AddLog(a.LoggingEvents); RemotingConfiguration.Configure("log4netListener.exe.config", false); RemotingServices.Marshal(sink, "RemoteAppenderLoggingSink"); } }
private static async Task Run(string[] args) { using (var container = new Unity.UnityContainer().EnableDiagnostic()) { container.RegisterSingleton <ILogger, Logger>(); Application.Run(container.Resolve <Form1>()); } }
static UnityContainer() { InternalUnityContainer container = new InternalUnityContainer(); container.RegisterSingleton <IConfigProvider>( ConfigProviderNames.UserSecrets, new InjectionFactory(c => new UserSecretsConfigProvider())); globalUnityContainer = container; }
static void Main(string[] args) { var container = new Unity.UnityContainer(); container.RegisterSingleton <IStringHelper, StringHelper>(); var newUsers = container.Resolve <Users>(); Console.WriteLine(newUsers.FormatUserName("cnf")); // Unity.Injection. Console.ReadKey(); }