示例#1
0
        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");
            }
        }
示例#2
0
        private static async Task Run(string[] args)
        {
            using (var container = new Unity.UnityContainer().EnableDiagnostic())
            {
                container.RegisterSingleton <ILogger, Logger>();

                Application.Run(container.Resolve <Form1>());
            }
        }
示例#3
0
        static UnityContainer()
        {
            InternalUnityContainer container = new InternalUnityContainer();

            container.RegisterSingleton <IConfigProvider>(
                ConfigProviderNames.UserSecrets,
                new InjectionFactory(c => new UserSecretsConfigProvider()));

            globalUnityContainer = container;
        }
示例#4
0
        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();
        }