Exemple #1
0
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            int callCount = 0;

            return(scope =>
            {
                // Resolve the result before we increment the call count, this ensures that singletons
                // won't cause any side effects during the compilation of the resolve function.
                var result = RuntimeResolver.Resolve(callSite, scope);

                if (Interlocked.Increment(ref callCount) == 2)
                {
                    // Don't capture the ExecutionContext when forking to build the compiled version of the
                    // resolve function
                    ThreadPool.UnsafeQueueUserWorkItem(state =>
                    {
                        try
                        {
                            base.RealizeService(callSite);
                        }
                        catch
                        {
                            // Swallow the exception, we should log this via the event source in a non-patched release
                        }
                    },
                                                       null);
                }

                return result;
            });
        }
Exemple #2
0
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            int callCount = 0;

            return(scope =>
            {
                // Resolve the result before we increment the call count, this ensures that singletons
                // won't cause any side effects during the compilation of the resolve function.
                var result = RuntimeResolver.Resolve(callSite, scope);

                if (Interlocked.Increment(ref callCount) == 2)
                {
                    // Don't capture the ExecutionContext when forking to build the compiled version of the
                    // resolve function
                    _ = ThreadPool.UnsafeQueueUserWorkItem(_ =>
                    {
                        try
                        {
                            base.RealizeService(callSite);
                        }
                        catch (Exception ex)
                        {
                            DependencyInjectionEventSource.Log.ServiceRealizationFailed(ex);
                        }
                    },
                                                           null);
                }

                return result;
            });
        }
 /// <summary>
 /// Creates the runtime resolver.
 /// </summary>
 /// <returns>The runtime resolver.</returns>
 /// <param name="type">Type.</param>
 private IResolver CreateRuntimeResolver(Type type)
 {
     if ((Serializator.Options & SerializatorOption.AllowRuntimeResolvers) != 0)
     {
         return(RuntimeResolver.ConfigurateRuntimeResolver(type, Serializator));
     }
     return(ExceptionHandler("Not Allow RuntimeResolvers"));
 }
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            return(scope =>
            {
                Func <ServiceProviderEngineScope, object> realizedService = p => RuntimeResolver.Resolve(callSite, p);

                RealizedServices[callSite.ServiceType] = realizedService;
                return realizedService(scope);
            });
        }
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            int callCount = 0;

            return(scope =>
            {
                if (Interlocked.Increment(ref callCount) == 2)
                {
                    Task.Run(() => base.RealizeService(callSite));
                }
                return RuntimeResolver.Resolve(callSite, scope);
            });
        }
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite) // 这个返回的委托方法执行时会创建服务实例,并将其返回出来。这里具体创建实例是通过调用基类中的字段 RuntimeResolver (其中的 Resolve 方法)创建的
        {
            var callCount = 0;

            return(scope =>
            {
                if (Interlocked.Increment(ref callCount) == 2)
                {
                    Task.Run(() => base.RealizeService(callSite));
                }
                return RuntimeResolver.Resolve(callSite, scope);
            });
        }
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            int callCount = 0;

            return(scope =>
            {
                // We want to directly use the callsite value if it's set and the scope is the root scope.
                // We've already called into the RuntimeResolver and pre-computed any singletons or root scope
                // Avoid the compilation for singletons (or promoted singletons)
                if (scope.IsRootScope && callSite.Value != null)
                {
                    return callSite.Value;
                }

                // Resolve the result before we increment the call count, this ensures that singletons
                // won't cause any side effects during the compilation of the resolve function.
                var result = RuntimeResolver.Resolve(callSite, scope);

                if (Interlocked.Increment(ref callCount) == 2)
                {
                    // This second check is to avoid the race where we end up kicking off a background thread
                    // if multiple calls to GetService race and resolve the values for singletons before the initial check above.
                    if (scope.IsRootScope && callSite.Value != null)
                    {
                        return callSite.Value;
                    }

                    // Don't capture the ExecutionContext when forking to build the compiled version of the
                    // resolve function
                    _ = ThreadPool.UnsafeQueueUserWorkItem(_ =>
                    {
                        try
                        {
                            base.RealizeService(callSite);
                        }
                        catch (Exception ex)
                        {
                            DependencyInjectionEventSource.Log.ServiceRealizationFailed(ex);
                        }
                    },
                                                           null);
                }

                return result;
            });
        }
        protected override Func <ServiceProviderEngineScope, object> RealizeService(ServiceCallSite callSite)
        {
            var callCount = 0;

            return(scope =>
            {
                if (Interlocked.Increment(ref callCount) == 2)
                {
#if NET40
                    Task.Factory.StartNew(() => base.RealizeService(callSite));
#else
                    Task.Run(() => base.RealizeService(callSite));
#endif
                }

                return RuntimeResolver.Resolve(callSite, scope);
            });
        }
        protected void RegisterCommand(Type type, bool hidden = true)
        {
            CCommand command = ClassUtils.CreateInstance <CCommand>(type);

            if (command == null)
            {
                throw new ArgumentException("Can't create class instance: " + type.FullName);
            }

            String commandName = type.Name;

            if (commandName.StartsWith("Cmd_"))
            {
                commandName = commandName.Substring("Cmd_".Length);
            }

            command.Name     = commandName;
            command.IsHidden = hidden;
            RuntimeResolver.ResolveOptions(command);

            CRegistery.Register(command);
        }
        private void ResolveSingletonServices(IEnumerable <ServiceDescriptor> serviceDescriptors)
        {
            var callSite = CallSiteFactory.CreateCallSite(typeof(IServiceProvider), new CallSiteChain());

            if (callSite != null)
            {
                RuntimeResolver.Resolve(callSite, Root);
            }

            callSite = CallSiteFactory.CreateCallSite(typeof(IServiceScopeFactory), new CallSiteChain());
            if (callSite != null)
            {
                RuntimeResolver.Resolve(callSite, Root);
            }

            foreach (var descriptor in serviceDescriptors.Where(sd => sd.Lifetime == ServiceLifetime.Singleton && !sd.ServiceType.IsGenericTypeDefinition))
            {
                callSite = CallSiteFactory.CreateCallSite(descriptor.ServiceType, new CallSiteChain());
                if (callSite != null)
                {
                    RuntimeResolver.Resolve(callSite, Root);
                }
            }
        }
Exemple #11
0
 internal static void ResolveOptions(CCommand command, Type commandType)
 {
     RuntimeResolver.ResolveOptions(command, commandType);
 }
Exemple #12
0
 internal static void ResolveOptions(CCommand command)
 {
     RuntimeResolver.ResolveOptions(command);
 }