Example #1
0
        /// <summary>
        /// Special get which can be used in situations where constructor
        /// argument need to be provided to resolve. The caller must provide an
        /// anonymous object which acts as prototype for constructor arguments
        /// </summary>
        /// <typeparam name="T">The type to resolve.</typeparam>
        /// <param name="resolutionRoot">The resolution root.</param>
        /// <param name="prototype">The argument provider prototype</param>
        /// <param name="shouldInherit">if set to <c>true</c> the constructor parameters are inherited.</param>
        /// <returns>
        /// The resolved instance.
        /// </returns>
        /// <extensiondoc category="ResolutionRoot"/>
        public static T Get <T>(this IResolutionRoot resolutionRoot, object prototype, bool shouldInherit)
        {
            var arguments = new List <ConstructorArgument>();

            var dictionary = prototype.ToDict();

            var contextAware = dictionary.Where(kvp => kvp.Value is Func <IContext, object>).ToDictionary(
                kvp => kvp.Key, kvp => ((Func <IContext, object>)kvp.Value));

            var contextAndTargetAware = dictionary
                                        .Where(kvp => kvp.Value is Func <IContext, ITarget, object>)
                                        .ToDictionary(kvp => kvp.Key, kvp => ((Func <IContext, ITarget, object>)kvp.Value));

            foreach (KeyValuePair <string, object> keyValuePair in dictionary)
            {
                Func <IContext, object>          contextAwareFunction;
                Func <IContext, ITarget, object> contextAndTargetAwareFunction;

                if (contextAware.TryGetValue(keyValuePair.Key, out contextAwareFunction))
                {
                    arguments.Add(new ConstructorArgument(keyValuePair.Key, ctx => contextAwareFunction(ctx), shouldInherit));
                }
                else if (contextAndTargetAware.TryGetValue(keyValuePair.Key, out contextAndTargetAwareFunction))
                {
                    arguments.Add(new ConstructorArgument(keyValuePair.Key, (ctx, target) => contextAndTargetAwareFunction(ctx, target), shouldInherit));
                }
                else
                {
                    arguments.Add(new ConstructorArgument(keyValuePair.Key, keyValuePair.Value, shouldInherit));
                }
            }

            return(ResolutionExtensions.Get <T>(resolutionRoot, arguments.ToArray()));
        }
 protected override object DoGetInstance(Type serviceType, string key)
 {
     if (key == null)
     {
         return(ResolutionExtensions.Get((IResolutionRoot)this.Kernel, serviceType, new IParameter[0]));
     }
     else
     {
         return(ResolutionExtensions.Get((IResolutionRoot)this.Kernel, serviceType, key, new IParameter[0]));
     }
 }
Example #3
0
        /// <summary>
        /// Stores the bus instance
        /// </summary>
        public void SetBus(IBus bus)
        {
            _kernel.Bind <IBus>().ToConstant(bus);

            _kernel.Bind <IMessageContext>().ToMethod(c =>
            {
                var currentMessageContext = MessageContext.Current;
                if (currentMessageContext == null)
                {
                    throw new InvalidOperationException(
                        "Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?");
                }
                return(currentMessageContext);
            });

            ResolutionExtensions.Get <IBus>(_kernel);
        }
        public void Execute()
        {
            NinjectTaskScopeObject ninjectTaskScopeObject = new NinjectTaskScopeObject();

#if NET
            CallContext.SetData(NinjectExtensionInTaskScope.InTaskScopeName, ninjectTaskScopeObject);
#endif
            try
            {
                lock (lockObject)
                {
                    ResolutionExtensions.Get <ITask>(this.kernel, this.taskName, new IParameter[0]).Execute();
                }
            }
            finally
            {
                ninjectTaskScopeObject.Dispose();
#if NET
                CallContext.FreeNamedDataSlot(NinjectExtensionInTaskScope.InTaskScopeName);
#endif
            }
        }
        /// <summary>
        /// Stores the bus instance
        /// </summary>
        public void SetBus(IBus bus)
        {
            if (_kernel.CanResolve(typeof(IBus)))
            {
                throw new InvalidOperationException("Cannot register IBus because it has already been registered. If you want to host multiple Rebus instances in a single process, please use separate container instances for them.");
            }

            _kernel.Bind <IBus>().ToConstant(bus);

            _kernel.Bind <ISyncBus>().ToMethod(c => bus.Advanced.SyncBus);

            _kernel.Bind <IMessageContext>().ToMethod(c =>
            {
                var currentMessageContext = MessageContext.Current;
                if (currentMessageContext == null)
                {
                    throw new InvalidOperationException(
                        "Attempted to inject the current message context from MessageContext.Current, but it was null! Did you attempt to resolve IMessageContext from outside of a Rebus message handler?");
                }
                return(currentMessageContext);
            });

            ResolutionExtensions.Get <IBus>(_kernel);
        }
 public IBus ResolveBus()
 {
     return(ResolutionExtensions.Get <IBus>(_kernel));
 }
Example #7
0
 public static object TryGet(Type type, params IParameter[] parameters)
 {
     return(ResolutionExtensions.Get(Kernel, type, parameters));
 }
Example #8
0
 public static object TryGet(Type type)
 {
     return(ResolutionExtensions.Get(Kernel, type, Array.Empty <IParameter>()));
 }
Example #9
0
 public static T TryGet <T>(params IParameter[] parameters)
 {
     return(ResolutionExtensions.Get <T>(Kernel, parameters));
 }
Example #10
0
 public static T Get <T>()
 {
     return(ResolutionExtensions.Get <T>(Kernel, Array.Empty <IParameter>()));
 }