Example #1
0
        static void Main(string[] args)
        {
            //ServiceHostManager manager = new ServiceHostManager();
            //manager.Run();

            ServiceHostBootstrapper bootstrapper = new ServiceHostBootstrapper();

            bootstrapper.Run();

            DatabaseInitializer.Initialize();

            //ServiceHostManager manager = bootstrapper._container.GetExportedValue<IServiceHostManager>() as ServiceHostManager;

            IServiceLocator    serviceLocator = bootstrapper._container.GetExportedValue <IServiceLocator>();
            ServiceHostManager manager        = serviceLocator.GetInstance <IServiceHostManager>() as ServiceHostManager;

            manager._container = bootstrapper._container;
            manager.LookupServices();

            int basePort = 50000;

            foreach (var host in manager.Services)
            {
                if (!host.UpdateAddressPort(basePort))
                {
                    Console.WriteLine("Hosting Service: " + host.Meta.Name + " UpdateAddressPort " + basePort.ToString() + " failure.");
                }

                //foreach (var address in service.Description.Endpoints)
                //{
                //    Console.WriteLine("Hosting Service: " + service.Meta.Name + " at " + address.Address.Uri);
                //}

                Debug.Assert(host.Description.Endpoints.Count == 1);

                host.Open();

                OperationContext operationContext = OperationContext.Current;
                InstanceContext  instanceContext  = operationContext.InstanceContext;
                ILighterService  service          = instanceContext.GetServiceInstance() as ILighterService;
                service.Initialize();

                var address = host.Description.Endpoints[0];
                Console.WriteLine("Hosting Service: " + host.Meta.Name + " at " + address.Address.Uri);

                //basePort++;
            }

            Console.ReadKey();

            foreach (var service in manager.Services)
            {
                service.Close();
            }
        }
Example #2
0
        private void StartupServices()
        {
            try
            {
                LastMessage = "搜索可提供的服务...";
                RaisePropertyChanged("LastMessage");

                _bootstrapper = new ServiceHostBootstrapper();
                _bootstrapper.Run();

                //DatabaseInitializer.Initialize();

                IServiceLocator serviceLocator = _bootstrapper._container.GetExportedValue <IServiceLocator>();
                _manager            = serviceLocator.GetInstance <IServiceHostManager>() as ServiceHostManager;
                _manager._container = _bootstrapper._container;
                _manager.LookupServices();

                LastMessage = "找到服务" + _manager.Services.Count <ExportServiceHost>().ToString() + "个";
                RaisePropertyChanged("LastMessage");

                _lighterServerContext = _bootstrapper._container.GetExportedValue <ILighterServerContext>();
                Users = _lighterServerContext.LoginedAccounts;

                foreach (ExportServiceHost host in _manager.Services)
                {
                    if (!host.UpdateAddressPort(ServerPort))
                    {
                        LastMessage = "Hosting Service: " + host.Meta.Name + " UpdateAddressPort " + ServerPort.ToString() + " failure.";
                        RaisePropertyChanged("LastMessage");
                    }

                    Debug.Assert(host.Description.Endpoints.Count == 1);
                    var address = host.Description.Endpoints[0];

                    host.Open();
                    host.Opened += delegate
                    {
                        LastMessage = "服务已启动: " + host.Meta.Name + " at " + address.Address.Uri;
                        RaisePropertyChanged("LastMessage");
                    };

                    //foreach (var address in host.Description.Endpoints)
                    {
                        LastMessage = "正在初始化服务 " + host.Meta.Name + " ... ";
                        RaisePropertyChanged("LastMessage");

                        //OperationContext operationContext = OperationContext.Current;
                        //InstanceContext instanceContext = operationContext.InstanceContext;
                        //ILighterService service = instanceContext.GetServiceInstance() as ILighterService;
                        //service.Initialize();

                        ServiceInfo info = new ServiceInfo(host.Meta.Description, address.Address.Uri);
                        Services.Add(info);
                        RaisePropertyChanged("Services");
                    }
                }

                LastMessage = "服务已提供";
                RaisePropertyChanged("LastMessage");

                IEventAggregator _eventAggregator = _manager._container.GetExportedValue <IEventAggregator>();
                _eventAggregator.GetEvent <AccountLoginEvent>().Subscribe(AccountLogined);
                _eventAggregator.GetEvent <AccountLogoutEvent>().Subscribe(AccountLogouted);
            }
            catch (Exception e)
            {
                LastMessage = ExceptionMessage.GetExceptionMessage(e);
                RaisePropertyChanged("LastMessage");
            }
        }