Example #1
0
        public IFabricConnector Select(ServiceId serviceId)
        {
            var serviceRegistration = _serviceRegistry.AllRegistrations
                                      .SingleOrDefault(r => r.ServiceName == serviceId.ServiceName);

            if (serviceRegistration == null)
            {
                _serviceRegistryUpdaterViaDiscovery.UpdateAsync(CancellationToken.None).Wait();
                serviceRegistration = _serviceRegistry.AllRegistrations
                                      .SingleOrDefault(r => r.ServiceName == serviceId.ServiceName);

                if (serviceRegistration == null)
                {
                    throw new InvalidOperationException($"Service '{serviceId.ServiceName}' is not registered.");
                }
            }

            if (!serviceRegistration.IsExternal)
            {
                var connector = _currentFabric.Instance.GetConnector(serviceId);
                if (connector == null)
                {
                    throw new InvalidOperationException(
                              $"The fabric does not have a self-connector for service '{serviceId.ServiceName}'.");
                }
                return(connector);
            }

            var connectorType          = serviceRegistration.ConnectorType;
            var connectorConfiguration = serviceRegistration.ConnectorConfiguration;

            var fabricConnectorFactory = _fabricConnectorFactorySelector.Select(connectorType);
            var fabricConnector        = fabricConnectorFactory.Create(serviceId, connectorConfiguration);

            return(fabricConnector);
        }
Example #2
0
        public async Task <BootstrapResult> BootstrapAsync(CancellationToken ct)
        {
            //_serviceRegistry.Register(
            //    new ServiceRegistrationInfo
            //    {
            //        Name = nameof(IntrinsicRoutines),
            //        QualifiedServiceTypeName = typeof(IntrinsicRoutines).AssemblyQualifiedName,
            //        QualifiedImplementationTypeName = typeof(IntrinsicRoutines).AssemblyQualifiedName,
            //        IsSingleton = true
            //    });

            //foreach (var serviceType in _appServiceDiscoveryFromCodeMarkup.DiscoverServiceTypes())
            //{
            //    ServiceRegistrationInfo registrationInfo;
            //    try
            //    {
            //        registrationInfo = _appServiceRegistrationInfoExtractor.Extract(serviceType);
            //    }
            //    catch
            //    {
            //        continue;
            //    }
            //    // TODO: fill in 'fabric' config?
            //    _serviceRegistry.Register(registrationInfo);
            //}

            //foreach (var registrationInfo in _appServiceDiscoveryFromRuntimeCollection.Services)
            //{
            //    // TODO: fill in 'fabric' config?
            //    _serviceRegistry.Register(registrationInfo);
            //}

            var appIocContainer = _appIocContainerProviders
                                  .Select(p => p.GetAppIocContainer())
                                  .FirstOrDefault(c => c != null);

            if (appIocContainer != null)
            {
                foreach (var bindingInfo in appIocContainer.DiscoverServices())
                {
                    var registrationInfo = new ServiceRegistrationInfo
                    {
                        QualifiedServiceTypeName        = bindingInfo.ServiceType.AssemblyQualifiedName,
                        QualifiedImplementationTypeName = bindingInfo.ImplementationType?.AssemblyQualifiedName,
                        IsExternal  = bindingInfo.IsExternal,
                        IsSingleton = true
                    };
                    // TODO: fill in 'fabric' config?
                    _serviceRegistry.Register(registrationInfo);
                }
            }

            if (appIocContainer == null)
            {
                appIocContainer = new BasicAppServiceIocContainer();
            }
            _appIocContainerHolder.Container = appIocContainer;

            foreach (var serviceRegistration in _serviceRegistry.AllRegistrations)
            {
                if (serviceRegistration.IsSingleton)
                {
                    var implementationType = serviceRegistration.ImplementationType;

                    if (implementationType == null && appIocContainer.TryGetImplementationType(
                            serviceRegistration.ServiceType, out implementationType) == true)
                    {
                        // TODO: properly update registration
                        ((ServiceRegistration)serviceRegistration).ImplementationType = implementationType;
                    }

                    if (!serviceRegistration.IsExternal && implementationType == null)
                    {
                        throw new InvalidOperationException(
                                  $"Could not find implementation type for service '{serviceRegistration.ServiceType}'.");
                    }

                    Func <object> proxyFactory = () =>
                    {
                        var serviceId = new ServiceId
                        {
                            ServiceName = serviceRegistration.ServiceName
                        };
                        var proxy = _serviceProxyBuilder.Build(serviceId);
                        return(proxy);
                    };

                    appIocContainer.RebindService(
                        serviceRegistration.ServiceType,
                        proxyFactory);

                    if (implementationType != null)
                    {
                        appIocContainer.RebindService(
                            implementationType,
                            proxyFactory);
                    }
                }
            }

            if (_fabric != null)
            {
                await _fabric.InitializeAsync(ct);
            }

            await _serviceRegistryUpdaterViaDiscovery.UpdateAsync(ct);

            if (_fabric != null && _servicePublishers.Length > 0)
            {
                var servicesToRegister = new List <ServiceRegistrationInfo>();

                foreach (var serviceRegistration in _serviceRegistry.AllRegistrations.Where(r => !r.IsExternal))
                {
                    var serviceId = new ServiceId {
                        ServiceName = serviceRegistration.ServiceName
                    };
                    if (_fabric.GetConnector(serviceId) is
                        IFabricConnectorWithConfiguration connectorWithConfiguration)
                    {
                        servicesToRegister.Add(new ServiceRegistrationInfo
                        {
                            Name = serviceRegistration.ServiceName,
                            QualifiedServiceTypeName = serviceRegistration.ServiceType.FullName,
                            IsSingleton            = serviceRegistration.IsSingleton,
                            IsExternal             = true,
                            ConnectorType          = connectorWithConfiguration.ConnectorType,
                            ConnectorConfiguration = connectorWithConfiguration.Configuration
                        });
                    }
                }

                if (servicesToRegister.Count > 0)
                {
                    foreach (var publisher in _servicePublishers)
                    {
                        await publisher.PublishAsync(servicesToRegister, ct);
                    }
                }
            }

            if (_fabric != null)
            {
                await _fabric.StartAsync(ct);
            }

            return(new BootstrapResult
            {
                Fabric = _fabric,
                AppIocContainer = _appIocContainerHolder.Container
            });
        }