예제 #1
0
        internal async Task <NuGetService> StartService(ServiceDefinition service)
        {
            // Create a full service name
            var name = new ServiceName(Description.InstanceName, service.Name);

            // Initialize the serice, create the necessary IoC components and construct the instance.
            ServicePlatformEventSource.Log.ServiceInitializing(name);
            ILifetimeScope scope = null;
            NuGetService   instance;

            try
            {
                // Resolve the service
                instance = _container.ResolveNamed <NuGetService>(
                    service.Name,
                    new NamedParameter("name", name));

                // Construct a scope with the service
                scope = _container.BeginLifetimeScope(builder =>
                {
                    builder.RegisterInstance(instance)
                    .As <NuGetService>()
                    .As(service.Type);

                    // Add the container itself to the container
                    builder.Register(c => scope)
                    .As <ILifetimeScope>()
                    .SingleInstance();

                    // Add components provided by the service
                    instance.RegisterComponents(builder);
                });
            }
            catch (Exception ex)
            {
                ServicePlatformEventSource.Log.ServiceInitializationFailed(name, ex);
                throw;
            }

            // Because of the "throw" in the catch block, we won't arrive here unless successful
            ServicePlatformEventSource.Log.ServiceInitialized(name);

            // Start the service and return it if the start succeeds.
            ServicePlatformEventSource.Log.ServiceStarting(name);
            bool result = false;

            try
            {
                Starting(instance);
                result = await instance.Start(scope);

                Started(instance);
            }
            catch (Exception ex)
            {
                ServicePlatformEventSource.Log.ServiceStartupFailed(name, ex);
                throw;
            }

            // Because of the "throw" in the catch block, we won't arrive here unless successful
            ServicePlatformEventSource.Log.ServiceStarted(name);

            if (result)
            {
                return(instance);
            }
            return(null);
        }
 public TestServiceWithComponents(ServiceName name, ServiceHost host) : base(name, host)
 {
 }