Exemplo n.º 1
0
        public object Build(ServiceId serviceId, string[] additionalInterfaces)
        {
            var registration = _serviceRegistry.AllRegistrations
                               .SingleOrDefault(r => r.ServiceName == serviceId.ServiceName);

            Type proxyType;

            if (registration?.ImplementationType != null)
            {
                proxyType = _proxyTypeBuilder.Build(registration.ImplementationType);
            }
            else
            {
                var interfacesTypes = new List <Type>(additionalInterfaces?.Length ?? 1);

                if (registration?.ServiceType != null)
                {
                    interfacesTypes.Add(registration.ServiceType);
                }

                if (additionalInterfaces != null)
                {
                    foreach (var typeFullName in additionalInterfaces)
                    {
#warning Use better type resolver?
                        var type = Type.GetType(typeFullName, throwOnError: false);
                        if (type != null)
                        {
                            interfacesTypes.Add(type);
                        }
                    }
                }

                proxyType = _proxyTypeBuilder.Build(interfacesTypes);
            }

            var allInterfaces = proxyType
                                .GetInterfaces()
                                .Where(i => i != typeof(IProxy))
                                .Select(i => i.AssemblyQualifiedName);

            if (additionalInterfaces != null)
            {
                allInterfaces = allInterfaces.Union(additionalInterfaces).Distinct();
            }

#warning Needs ability to inject services with different Service IDs (parent Service ID?) as a part of service mesh framework.
            var proxy = (IProxy)_appServiceIocContainer.Resolve(proxyType);
            proxy.Executor = _proxyMethodExecutor;
            proxy.Context  = new ServiceProxyContext
            {
                Service = new ServiceDescriptor
                {
                    Id         = serviceId,
                    Interfaces = allInterfaces.ToArray()
                }
            };
            return(proxy);
        }
Exemplo n.º 2
0
        private object CreateServiceProxy(IServiceDefinition serviceDefinition, ServiceId serviceId, string[] additionalInterfaces)
        {
            Type proxyType;

            if (serviceDefinition.Implementation != null)
            {
                proxyType = _proxyTypeBuilder.Build(serviceDefinition.Implementation);
            }
            else
            {
                var interfacesTypes = new HashSet <Type>();

                if (serviceDefinition.Interfaces?.Length > 0)
                {
                    foreach (var interfaceType in serviceDefinition.Interfaces)
                    {
                        interfacesTypes.Add(interfaceType);
                    }
                }

                if (additionalInterfaces != null)
                {
                    foreach (var typeFullName in additionalInterfaces)
                    {
#warning Use better type resolver?
                        var type = Type.GetType(typeFullName, throwOnError: false);
                        if (type != null)
                        {
                            interfacesTypes.Add(type);
                        }
                    }
                }

                proxyType = _proxyTypeBuilder.Build(interfacesTypes);
            }

            var allInterfaces = proxyType
                                .GetInterfaces()
                                .Where(i => i != typeof(IProxy))
                                .Select(i => i.AssemblyQualifiedName);

            if (additionalInterfaces != null)
            {
                allInterfaces = allInterfaces.Union(additionalInterfaces).Distinct();
            }

            var serviceProxyContext = new ServiceProxyContext
            {
                Definition = serviceDefinition,

                Descriptor = new ServiceDescriptor
                {
                    Id         = serviceId,
                    Interfaces = allInterfaces.ToArray()
                }
            };

            var buildingContext = ServiceProxyBuildingContext.EnterScope(serviceProxyContext);
            try
            {
                var proxy = (IProxy)ActivateServiceProxyInstance(proxyType);
                proxy.Executor = _proxyMethodExecutor;
                proxy.Context  = serviceProxyContext;
                return(proxy);
            }
            finally
            {
                buildingContext.ExitScope();
            }
        }