Exemple #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>());
        }
Exemple #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>());
        }
Exemple #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);
            }
        }