示例#1
0
        public void AppScopeTest()
        {
            var builder = new ContainerBuilder();

            builder.RegisterType <TestClass1>().AsSelf().SingleInstance();
            builder.RegisterType <TestClass2>().AsSelf().InstancePerLifetimeScope();
            IContainer cont = builder.Build();

            using (var scope = new AppScope(cont))
            {
                object r1 = scope.Resolve(typeof(TestClass1));
                var    r2 = scope.Resolve <TestClass1>();
                Assert.AreSame(r1, r2);
                Assert.AreSame(cont, scope.Scope);

                var t1 = scope.Resolve <TestClass2>();
                using (AppScope inner = scope.BeginScope())
                {
                    var t2 = inner.Resolve <TestClass2>();
                    Assert.AreNotSame(t1, t2);

                    var resolvedScope = inner.Resolve <IAppScope>();
                    Assert.AreSame(inner, resolvedScope);
                }
            }
        }
示例#2
0
        public void AppCoreBuilderTest()
        {
            AppCoreBuilder.Create(new ContainerBuilder()).Build().Dispose();

            using (
                AppCore core =
                    AppCoreBuilder.Create()
                    .AddModule(new TestModule())
                    .AddModule <TestModule1>()
                    .Configure(b => b.RegisterType <TestClass1>().AsSelf().SingleInstance())
                    .Build())
            {
                using (AppScope scope = core.BeginScope())
                {
                    object r1 = scope.Resolve(typeof(TestClass1));
                    var    r2 = scope.Resolve <TestClass1>();
                    Assert.AreSame(r1, r2);


                    var t1 = scope.Resolve <TestClass2>();
                    using (AppScope inner = scope.BeginScope())
                    {
                        var t2 = inner.Resolve <TestClass2>();
                        Assert.AreNotSame(t1, t2);
                    }
                }
            }
        }
        public void RefrenceArrayTests()
        {
            Guid id;

            using (var scope = _scope.BeginScope())
            {
                var data = new List <LeafDataObject>();
                for (int i = 0; i < 100; i++)
                {
                    data.Add(new LeafDataObject()
                    {
                        Number = i
                    });
                }

                var rep = scope.Resolve <IRepositoryWithGuid <LeafDataObject> >();
                rep.StoreBulk(data);

                rep.SaveChanges();
                var rootRep = scope.Resolve <IRepositoryWithGuid <RootDataObject> >();
                var root    = new RootDataObject()
                {
                    Data = data.Select(d => new Refrence <LeafDataObject>
                    {
                        Id = d.Id
                    }).ToList(),
                    Name = "data"
                };
                rootRep.Store(root);

                rootRep.SaveChanges();
                id = root.Guid;
            }
            using (var scope = _scope.BeginScope())
            {
                var rootRep  = scope.Resolve <IRepositoryWithGuid <RootDataObject> >();
                var rootData = rootRep.Find(id);

                Assert.AreNotEqual(rootData.Data.Count, 0);
                foreach (var dataRef in rootData.Data)
                {
                    Assert.NotNull(dataRef);
                    Assert.NotNull(dataRef.Object);
                    Assert.GreaterOrEqual(dataRef.Object.Number, 0);
                }
            }
        }
示例#4
0
        public void AppScopeTest()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<TestClass1>().AsSelf().SingleInstance();
            builder.RegisterType<TestClass2>().AsSelf().InstancePerLifetimeScope();
            IContainer cont = builder.Build();

            using (var scope = new AppScope(cont))
            {
                object r1 = scope.Resolve(typeof (TestClass1));
                var r2 = scope.Resolve<TestClass1>();
                Assert.AreSame(r1, r2);
                Assert.AreSame(cont, scope.Scope);

                var t1 = scope.Resolve<TestClass2>();
                using (AppScope inner = scope.BeginScope())
                {
                    var t2 = inner.Resolve<TestClass2>();
                    Assert.AreNotSame(t1, t2);

                    var resolvedScope = inner.Resolve<IAppScope>();
                    Assert.AreSame(inner,resolvedScope);
                }
            }
        }
        public void Start()
        {
            var services = _knownServices.ToArray();

            Log.InfoFormat("Запуск WCF-хоста для {0} сервисов", services.Length);
            var hostName = _config.GetAppSettingString("Zen/Hostname");

            if (string.IsNullOrEmpty(hostName))
            {
                hostName = "localhost";
            }

            var port = _config.GetAppSettingString("Zen/Port");

            if (string.IsNullOrEmpty(port))
            {
                port = "8080";
            }

            foreach (var knownService in services)
            {
                Log.DebugFormat("Запуск хостов для типа: {0}", knownService.GetType().Name);
                IWebService service = knownService;
                var         scope   = AppScope.BeginScope(b => b.Register(ctx => this).As <WebserviceHostApplication>());
                _hostScopes.Add(scope);
                _hostThreads.Add(new Thread(() =>
                {
                    var uriStr  = string.Format("http://{0}:{1}/{2}", hostName, port, service.GetWebserviceName());
                    Uri address = new Uri(uriStr);

                    foreach (
                        var webService in
                        service.GetType()
                        .GetInterfaces()
                        .Where(i =>
                               i.GetCustomAttributes(typeof(ServiceContractAttribute), true).Any() &&
                               i != typeof(IWebService)))
                    {
                        try
                        {
                            Log.DebugFormat("Запуск хостов для типа: {0} по контракту {1}", service.GetType().Name,
                                            webService.Name);
                            ServiceHost host = new ServiceHost(service.GetType(), address);
                            host.AddServiceEndpoint(webService, new BasicHttpBinding(), string.Empty);

                            host.AddDependencyInjectionBehavior(webService, scope.Scope);

                            host.Description.Behaviors.Add(new ServiceMetadataBehavior
                            {
                                HttpGetEnabled = true,
                                HttpGetUrl     = address
                            });
                            _hosts.Add(host);
                            host.Open();
                            var sb = new StringBuilder();
                            foreach (var ep in host.Description.Endpoints)
                            {
                                sb.AppendFormat(
                                    "Для контракта: {1} сервиса {2} производится прослушивание по адресу {0}",
                                    ep.Address.Uri.AbsoluteUri,
                                    ep.Contract.Name,
                                    service.GetType().Name)
                                .AppendLine();
                            }
                            Log.Info(sb.ToString());

                            var enumService = scope.Resolve <EnumeratorWebservice>();
                            if (enumService != null)
                            {
                                enumService.RegisterService(service);
                            }
                        }
                        catch (Exception ex)
                        {
                            Log.Error(string.Format("Ошибка инициализации веб-сервиса {1}:{0}", webService.Name,
                                                    service.GetType().Name), ex);
                        }
                    }
                }));
            }

            foreach (var hostThread in _hostThreads)
            {
                hostThread.Start();
            }
        }