예제 #1
0
 public void Register(ServiceRegistrationInfo info)
 {
     if (!_actor.IsStopped)
     {
         Action <IDirectoryClient> consumer = x => x.Register(info);
         if (_mailbox.IsPreallocated)
         {
             _mailbox.Send(_actor, consumer, null, RegisterRepresentation1);
         }
         else
         {
             _mailbox.Send(new LocalMessage <IDirectoryClient>(_actor, consumer, RegisterRepresentation1));
         }
     }
     else
     {
         _actor.DeadLetters?.FailedDelivery(new DeadLetter(_actor, RegisterRepresentation1));
     }
 }
예제 #2
0
        public void TestRegisterDiscoverMutiple()
        {
            _directory.Actor.Start();
            _directory.Actor.Use(new TestAttributesClient());
            _directory.Actor.AssignLeadership();

            var location1 = new Location("test-host1", 1234);
            var info1     = new ServiceRegistrationInfo("test-service1", new List <Location> {
                location1
            });

            _client1.Actor.Register(info1);

            var location2 = new Location("test-host2", 1234);
            var info2     = new ServiceRegistrationInfo("test-service2", new List <Location> {
                location2
            });

            _client2.Actor.Register(info2);

            var location3 = new Location("test-host3", 1234);
            var info3     = new ServiceRegistrationInfo("test-service3", new List <Location> {
                location3
            });

            _client3.Actor.Register(info3);

            Pause();

            foreach (var interest in _interests)
            {
                Assert.NotNull(interest.ServicesSeen);
                Assert.Contains("test-service1", interest.ServicesSeen);
                Assert.Contains("test-service2", interest.ServicesSeen);
                Assert.Contains("test-service3", interest.ServicesSeen);
                Assert.NotEmpty(interest.DiscoveredServices);
                Assert.Contains(info1, interest.DiscoveredServices);
                Assert.Contains(info2, interest.DiscoveredServices);
                Assert.Contains(info3, interest.DiscoveredServices);
            }
        }
예제 #3
0
        public void TestShouldNotInformInterest()
        {
            _directory.Actor.Start();
            _directory.Actor.Use(new TestAttributesClient());

            // directory NOT assigned leadership
            _directory.Actor.RelinquishLeadership(); // actually never had leadership, but be explicit and prove no harm

            var location1 = new Location("test-host1", 1234);
            var info1     = new ServiceRegistrationInfo("test-service1", new List <Location> {
                location1
            });

            _client1.Actor.Register(info1);

            Pause();

            Assert.Empty(_interest1.ServicesSeen);
            Assert.DoesNotContain("test-service", _interest1.ServicesSeen);
            Assert.Empty(_interest1.DiscoveredServices);
            Assert.DoesNotContain(info1, _interest1.DiscoveredServices);
        }
예제 #4
0
        private void Validate(ServiceRegistrationInfo model)
        {
            Contract.Requires(model.Factory.ImplementationType != null);
            Contract.Ensures(model.Factory.ImplementationType != null);

            Maybe <TransactionalClassMetaInfo> meta;
            List <string> problematicMethods;

            if (model.ServiceType == null ||
                model.ServiceType.IsInterface ||
                !(meta = _MetaStore.GetMetaFromType(model.Factory.ImplementationType)).HasValue ||
                (problematicMethods = (from method in meta.Value.TransactionalMethods
                                       where !method.IsVirtual
                                       select method.Name).ToList()).Count == 0)
            {
                return;
            }

            throw new AutoTxFacilityException(string.Format("The class {0} wants to use transaction interception, " +
                                                            "however the methods must be marked as virtual in order to do so. Please correct " +
                                                            "the following methods: {1}", model.Factory.ImplementationType.FullName,
                                                            string.Join(", ", problematicMethods.ToArray())));
        }
예제 #5
0
        public static ServiceRegistrationInfo CreateServiceInfo(Service service)
        {
            var info = new ServiceRegistrationInfo(service);

            return(info);
        }
예제 #6
0
 public bool RegisterService(ServiceRegistrationInfo service)
 {
     return(client.RegisterService(service));
 }
예제 #7
0
 public async Task <bool> RegisterService([FromBody] ServiceRegistrationInfo service)
 {
     return(await _client.RegisterService(service));
 }
예제 #8
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
            });
        }
예제 #9
0
        public void TestAlteredLeadership()
        {
            _directory.Actor.Start();
            _directory.Actor.Use(new TestAttributesClient());

            // START directory assigned leadership
            _directory.Actor.AssignLeadership();

            var location1 = new Location("test-host1", 1234);
            var info1     = new ServiceRegistrationInfo("test-service1", new List <Location> {
                location1
            });

            _client1.Actor.Register(info1);

            var location2 = new Location("test-host2", 1234);
            var info2     = new ServiceRegistrationInfo("test-service2", new List <Location> {
                location2
            });

            _client2.Actor.Register(info2);

            var location3 = new Location("test-host3", 1234);
            var info3     = new ServiceRegistrationInfo("test-service3", new List <Location> {
                location3
            });

            _client3.Actor.Register(info3);

            Pause();

            foreach (var interest in _interests)
            {
                var discoveredServices = interest.DiscoveredServices.ToList();
                Assert.NotEmpty(interest.ServicesSeen);
                Assert.Contains("test-service1", interest.ServicesSeen);
                Assert.Contains("test-service2", interest.ServicesSeen);
                Assert.Contains("test-service3", interest.ServicesSeen);
                Assert.NotEmpty(discoveredServices);
                Assert.Contains(info1, discoveredServices);
                Assert.Contains(info2, discoveredServices);
                Assert.Contains(info3, discoveredServices);
            }

            // ALTER directory relinquished leadership
            _directory.Actor.RelinquishLeadership();
            Pause();

            foreach (var interest in _interests)
            {
                interest.ServicesSeen.Clear();
                interest.DiscoveredServices.Clear();
            }

            Pause();

            foreach (var interest in _interests)
            {
                Assert.Empty(interest.ServicesSeen);
                Assert.DoesNotContain("test-service1", interest.ServicesSeen);
                Assert.DoesNotContain("test-service2", interest.ServicesSeen);
                Assert.DoesNotContain("test-service3", interest.ServicesSeen);
                Assert.Empty(interest.DiscoveredServices);
                Assert.DoesNotContain(info1, interest.DiscoveredServices);
                Assert.DoesNotContain(info2, interest.DiscoveredServices);
                Assert.DoesNotContain(info3, interest.DiscoveredServices);
            }

            // ALTER directory assigned leadership
            _directory.Actor.AssignLeadership();
            Pause();

            foreach (var interest in _interests)
            {
                interest.ServicesSeen.Clear();
                interest.DiscoveredServices.Clear();
            }

            Pause();

            foreach (var interest in _interests)
            {
                Assert.NotEmpty(interest.ServicesSeen);
                Assert.Contains("test-service1", interest.ServicesSeen);
                Assert.Contains("test-service2", interest.ServicesSeen);
                Assert.Contains("test-service3", interest.ServicesSeen);
                Assert.NotEmpty(interest.DiscoveredServices);
                Assert.Contains(info1, interest.DiscoveredServices);
                Assert.Contains(info2, interest.DiscoveredServices);
                Assert.Contains(info3, interest.DiscoveredServices);
            }
        }
예제 #10
0
    //====================================
    // DirectoryClient
    //====================================

    public void Register(ServiceRegistrationInfo info)
    {
        var converted = Model.Message.RegisterService.As(Name.Of(info.Name), Location.ToAddresses(info.Locations));

        _registerService = RawMessage.From(0, 0, converted.ToString());
    }