Exemplo n.º 1
0
        private T CreateAndExportService <T>()
        {
            var implementationGenerator = Container.Locate <IServiceImplementationGenerator>();

            var implementationRequest = new ImplementationRequest
            {
                DefaultSerializer   = Serializer,
                ExposeDefaultMethod = ExposeDefaultMethod.PostOnly,
                ClientProvider      = new RpcTestClientProvider(ProcessRequest),
                InterfaceType       = typeof(T)
            };

            var implementationType =
                implementationGenerator.GenerateImplementationForInterface(implementationRequest);

            Container.Configure(c =>
            {
                var strategy = new CompiledExportStrategy(implementationType, Container, c.OwningScope.StrategyCompiler.DefaultStrategyExpressionBuilder);

                strategy.AddExportAs(typeof(T));

                c.AddActivationStrategy(strategy);
            });

            return(Container.Locate <T>());
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public IEnumerable <IActivationStrategy> ProvideExports(IInjectionScope scope, IActivationExpressionRequest request)
        {
            var fullName = request.ActivationType.FullName;

            if ((request.ActivationType.GetTypeInfo().IsInterface ||
                 request.ActivationType.GetTypeInfo().IsAbstract) &&
                _namespaceConfig.Namespaces.Any(proxyNamespace => fullName.StartsWith(proxyNamespace)))
            {
                if (_namespaceConfig.Serializer == null)
                {
                    _namespaceConfig.Serializer = scope.LocateOrDefault <IClientSerializer>();

#if NETCOREAPP3_1
                    _namespaceConfig.Serializer = new JsonClientSerializer();
#endif

                    if (_namespaceConfig.Serializer == null)
                    {
                        throw new Exception("IClientSerializer implementation is not exported");
                    }
                }

                var implementationGenerator = scope.Locate <IServiceImplementationGenerator>();

                var implementationRequest = new ImplementationRequest
                {
                    DefaultSerializer       = _namespaceConfig.Serializer,
                    ExposeDefaultMethod     = ExposeDefaultMethod.PostOnly,
                    ClientProvider          = _clientProvider,
                    InterfaceType           = request.ActivationType,
                    NamingConventionService = _namespaceConfig.NamingConvention
                };

                var implementationType =
                    implementationGenerator.GenerateImplementationForInterface(implementationRequest);

                var strategy = new CompiledExportStrategy(implementationType, scope,
                                                          request.Services.LifestyleExpressionBuilder);

                strategy.AddExportAs(request.ActivationType);

                return(new IActivationStrategy[] { strategy });
            }

            return(Array.Empty <IActivationStrategy>());
        }
Exemplo n.º 3
0
        /// <summary>Provide exports for a missing type</summary>
        /// <param name="scope">scope to provide value</param>
        /// <param name="request">request</param>
        /// <returns>set of activation strategies</returns>
        public IEnumerable <IActivationStrategy> ProvideExports(IInjectionScope scope, IActivationExpressionRequest request)
        {
            var fullName = request.ActivationType.FullName;

            if ((request.ActivationType.GetTypeInfo().IsInterface ||
                 request.ActivationType.GetTypeInfo().IsAbstract) &&
                _proxyNamespaces.Any(proxyNamespace => fullName.StartsWith(proxyNamespace)))
            {
                var proxyGenerator = scope.Locate <IProxyGenerator>();

                var proxyType = proxyGenerator.GenerateProxyType(request.ActivationType, _callByName);

                var strategy =
                    new CompiledExportStrategy(proxyType, scope, request.Services.LifestyleExpressionBuilder);

                strategy.AddExportAs(request.ActivationType);

                yield return(strategy);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Provide exports for a missing type
        /// </summary>
        /// <param name="scope">scope to provide value</param>
        /// <param name="request">request</param>
        /// <returns>set of activation strategies</returns>
        public virtual IEnumerable <IActivationStrategy> ProvideExports(IInjectionScope scope, IActivationExpressionRequest request)
        {
            var requestedType = request.ActivationType;

            if (requestedType.IsConstructedGenericType)
            {
                var genericType = requestedType.GetGenericTypeDefinition();

                if (genericType == typeof(ImmutableLinkedList <>))
                {
                    yield return(new ImmutableLinkListStrategy(scope));

                    yield break;
                }

                if (genericType == typeof(ImmutableArray <>))
                {
                    yield return(new ImmutableArrayStrategy(scope));

                    yield break;
                }

                if (genericType == typeof(IReadOnlyCollection <>) ||
                    genericType == typeof(IReadOnlyList <>) ||
                    genericType == typeof(ReadOnlyCollection <>))
                {
                    yield return(new ReadOnlyCollectionStrategy(scope));

                    yield break;
                }

                if (genericType == typeof(IList <>) ||
                    genericType == typeof(ICollection <>) ||
                    genericType == typeof(List <>))
                {
                    yield return(new ListEnumerableStrategy(scope));

                    yield break;
                }

                if (genericType == typeof(KeyedLocateDelegate <,>))
                {
                    yield return(new KeyedLocateDelegateStrategy(scope));

                    yield break;
                }


                if (genericType.FullName == "System.Collections.Immutable.ImmutableList`1" ||
                    genericType.FullName == "System.Collections.Immutable.ImmutableArray`1" ||
                    genericType.FullName == "System.Collections.Immutable.ImmutableQueue`1" ||
                    genericType.FullName == "System.Collections.Immutable.ImmutableStack`1" ||
                    genericType.FullName == "System.Collections.Immutable.ImmutableSortedSet`1" ||
                    genericType.FullName == "System.Collections.Immutable.ImmutableHashSet`1")
                {
                    yield return(new ImmutableCollectionStrategy(genericType, scope));

                    yield break;
                }
            }

            if (requestedType.GetTypeInfo().IsInterface ||
                requestedType.GetTypeInfo().IsAbstract)
            {
                yield break;
            }

            if (typeof(MulticastDelegate).GetTypeInfo().IsAssignableFrom(requestedType.GetTypeInfo()))
            {
                var method = requestedType.GetTypeInfo().GetDeclaredMethod("Invoke");

                if (method.ReturnType != typeof(void) &&
                    scope.CanLocate(method.ReturnType))
                {
                    var parameterCount = method.GetParameters().Length;

                    switch (parameterCount)
                    {
                    case 0:
                        yield return(new DelegateNoArgWrapperStrategy(requestedType, scope));

                        break;

                    case 1:
                        yield return(new DelegateOneArgWrapperStrategy(requestedType, scope));

                        break;

                    case 2:
                        yield return(new DelegateTwoArgWrapperStrategy(requestedType, scope));

                        break;

                    case 3:
                        yield return(new DelegateThreeArgWrapperStrategy(requestedType, scope));

                        break;

                    case 4:
                        yield return(new DelegateFourArgWrapperStrategy(requestedType, scope));

                        break;

                    case 5:
                        yield return(new DelegateFiveArgWrapperStrategy(requestedType, scope));

                        break;
                    }
                }
            }
            else if (ShouldCreateConcreteStrategy(request))
            {
                var strategy =
                    new CompiledExportStrategy(requestedType, scope, request.Services.LifestyleExpressionBuilder).ProcessAttributeForStrategy();

                strategy.Lifestyle = scope.ScopeConfiguration.AutoRegistrationLifestylePicker?.Invoke(requestedType);

                yield return(strategy);
            }
        }