Ejemplo n.º 1
0
        public TService Get(FrameworkType frameworkType)
        {
            if (!_isInitialized)
            {
                InitializeSimpleInjector(_dependency);
                InitializeWindsor(_dependency);
                InitializeMicrosoftDependencyInjection(_dependency);

                _isInitialized = true;
            }

            switch (frameworkType)
            {
            case FrameworkType.Native:
                return((TService)_aspectWeaver.Weave(new TImplementation(), typeof(TService),
                                                     typeof(TImplementation)));

            case FrameworkType.CastleWindsor:
                return(_windsorContainer.Resolve <TService>());

            case FrameworkType.SimpleInjector:
                return(_simpleInjectorContainer.GetInstance <TService>());

            case FrameworkType.MicrosoftDependencyInjection:
                return(_serviceProvider.GetService <TService>());

            default:
                throw new ArgumentOutOfRangeException(nameof(frameworkType), frameworkType, null);
            }
        }
Ejemplo n.º 2
0
        public void CreateWeavedUsingNativeWeaver()
        {
            var service = _weaver.Weave <IServiceForBenchmark, ServiceForBenchmark>(
                new ServiceForBenchmark());

            GC.KeepAlive(service);
        }
Ejemplo n.º 3
0
        protected override bool Process(AssemblyStitcherContext context)
        {
            if (AlreadyProcessed(context))
            {
                return(false);
            }


#if DEBUG
            _logging = new MultiLogging(new DefaultLogging(Logging), new FileLogging("MrAdvice.log"));
            _logging.WriteDebug("Start");
#else
            _logging = Logging;
#endif
            if (context.Module == null)
            {
                _logging.WriteError("Target assembly {0} could not be loaded", context.AssemblyPath);
                return(false);
            }

            try
            {
                // instances are created here
                // please also note poor man's dependency injection (which is enough for us here)
                var assemblyResolver = context.AssemblyResolver;
                var typeResolver     = new TypeResolver(context.Module)
                {
                    Logging = _logging, AssemblyResolver = assemblyResolver
                };
                var typeLoader   = new TypeLoader(() => LoadWeavedAssembly(context, assemblyResolver));
                var aspectWeaver = new AspectWeaver {
                    Logging = _logging, TypeResolver = typeResolver, TypeLoader = typeLoader
                };

                BlobberHelper.Setup();

                //Assembly.Load("System, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes");
                //Assembly.Load("System.Core, Version=2.0.5.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e, Retargetable=Yes");
                Assembly.Load("MrAdvice, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c0e7e6eab6f293d8");

                return(aspectWeaver.Weave(context.Module));
            }
            catch (Exception e)
            {
                _logging.WriteError("Internal error: {0}", e.ToString());
                for (var ie = e.InnerException; ie != null; ie = ie.InnerException)
                {
                    _logging.WriteError("Inner exception: {0}", e.ToString());
                }
            }
            return(false);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Processes the specified context.
        /// </summary>
        /// <param name="context">The context.</param>
        /// <returns></returns>
        /// <exception cref="InvalidOperationException">Ignore.</exception>
        protected override bool Process(AssemblyStitcherContext context)
        {
            BlobberHelper.Setup();

#if DEBUG
            _logging = new MultiLogging(new DefaultLogging(Logging), new FileLogging("MrAdvice.log"));
            _logging.WriteDebug("Start");
#else
            _logging = Logging;
#endif
            if (context.Module is null)
            {
                _logging.WriteError("Target assembly {0} could not be loaded", context.AssemblyPath);
                return(false);
            }

            try
            {
                // instances are created here
                // please also note poor man's dependency injection (which is enough for us here)
                var assemblyResolver = context.AssemblyResolver;
                var typeResolver     = new TypeResolver(context.Module)
                {
                    Logging = _logging, AssemblyResolver = assemblyResolver
                };
                var typeLoader   = new TypeLoader(() => LoadWeavedAssembly(context, assemblyResolver));
                var aspectWeaver = new AspectWeaver {
                    Logging = _logging, TypeResolver = typeResolver, TypeLoader = typeLoader
                };

                // second chance: someone had the marker file missing
                if (aspectWeaver.FindShortcutType(context.Module) is not null)
                {
                    return(false);
                }

                return(aspectWeaver.Weave(context.Module));
            }
            catch (Exception e)
            {
                _logging.WriteError("Internal error: {0}", e.ToString());
                for (var ie = e.InnerException; ie is not null; ie = ie.InnerException)
                {
                    _logging.WriteError("Inner exception: {0}", e.ToString());
                }
            }
            return(false);
        }
Ejemplo n.º 5
0
    /// <summary>
    /// Executes this instance.
    /// </summary>
    // ReSharper disable once UnusedMember.Global
    public void Execute()
    {
        // instances are created here
        // please also note poor man's dependency injection (which is enough for us here)
        Logger.LogInfo    = LogInfo;
        Logger.LogWarning = LogWarning;
        Logger.LogError   = LogError;
        var typeResolver = new TypeResolver {
            AssemblyResolver = AssemblyResolver
        };
        var typeLoader   = new TypeLoader(() => LoadWeavedAssembly());
        var aspectWeaver = new AspectWeaver {
            TypeResolver = typeResolver, TypeLoader = typeLoader
        };

        aspectWeaver.Weave(ModuleDefinition);
    }
Ejemplo n.º 6
0
        protected override bool Process(AssemblyStitcherContext context)
        {
            if (AlreadyProcessed(context))
            {
                return(false);
            }

#if DEBUG
            _logging = new MultiLogging(new DefaultLogging(Logging), new FileLogging("MrAdvice.log"));
            _logging.WriteDebug("Start");
#else
            _logging = Logging;
#endif
            try
            {
                // instances are created here
                // please also note poor man's dependency injection (which is enough for us here)
                var assemblyResolver = context.AssemblyResolver;
                var typeResolver     = new TypeResolver {
                    Logging = _logging, AssemblyResolver = assemblyResolver
                };
                var typeLoader   = new TypeLoader(() => LoadWeavedAssembly(context, assemblyResolver));
                var aspectWeaver = new AspectWeaver {
                    Logging = _logging, TypeResolver = typeResolver, TypeLoader = typeLoader
                };
                // TODO: use blobber's resolution (WTF?)
                AppDomain.CurrentDomain.AssemblyResolve += OnAssemblyResolve;
                BlobberHelper.Setup();

                return(aspectWeaver.Weave(context.Module));
            }
            catch (Exception e)
            {
                _logging.WriteError("Internal error: {0}", e.ToString());
                for (var ie = e.InnerException; ie != null; ie = ie.InnerException)
                {
                    _logging.WriteError("Inner exception: {0}", e.ToString());
                }
            }
            return(false);
        }
Ejemplo n.º 7
0
        protected override bool Process(AssemblyStitcherContext context)
        {
            BlobberHelper.Setup();

            if (AlreadyProcessed(context))
            {
                return(false);
            }

#if DEBUG
            _logging = new MultiLogging(new DefaultLogging(Logging), new FileLogging("MrAdvice.log"));
            _logging.WriteDebug("Start");
#else
            _logging = Logging;
#endif
            if (context.Module == null)
            {
                _logging.WriteError("Target assembly {0} could not be loaded", context.AssemblyPath);
                return(false);
            }

            try
            {
                try
                {
                    const string mrAdviceAssemblyName = "MrAdvice, Version=2.0.0.0, Culture=neutral, PublicKeyToken=c0e7e6eab6f293d8";
                    var          mrAdviceAssembly     = LoadEmbeddedAssembly(mrAdviceAssemblyName);
                    if (mrAdviceAssembly == null)
                    {
                        _logging.WriteError("Can't find/load embedded MrAdvice assembly (WTF?), exiting");
                        return(false);
                    }
                }
                catch (FileNotFoundException)
                {
                    _logging.WriteError("Can't load MrAdvice assembly (WTF?), exiting");
                    foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
                    {
                        _logging.Write("Assembly in AppDomain: {0}", assembly.GetName());
                    }
                    return(false);
                }

                // instances are created here
                // please also note poor man's dependency injection (which is enough for us here)
                var assemblyResolver = context.AssemblyResolver;
                var typeResolver     = new TypeResolver(context.Module)
                {
                    Logging = _logging, AssemblyResolver = assemblyResolver
                };
                var typeLoader   = new TypeLoader(() => LoadWeavedAssembly(context, assemblyResolver));
                var aspectWeaver = new AspectWeaver {
                    Logging = _logging, TypeResolver = typeResolver, TypeLoader = typeLoader
                };

                // second chance: someone had the marker file missing
                if (aspectWeaver.FindShortcutType(context.Module) != null)
                {
                    return(false);
                }

                return(aspectWeaver.Weave(context.Module));
            }
            catch (Exception e)
            {
                _logging.WriteError("Internal error: {0}", e.ToString());
                for (var ie = e.InnerException; ie != null; ie = ie.InnerException)
                {
                    _logging.WriteError("Inner exception: {0}", e.ToString());
                }
            }
            return(false);
        }