Example #1
0
        public void CanDecorateAClassType()
        {
            var dummyObject = new DummyObject();

            var proxy = (DummyObject)classUnderTest.Decorate(typeof(DummyObject), dummyObject);

            proxy.SetValue(7);
            Assert.AreEqual(1, aspectStub.CallCount);
            proxy.GetValue();
            Assert.AreEqual(2, aspectStub.CallCount);
        }
Example #2
0
        private void DecorateProfilerForWcfClientProcessing()
        {
            const String assembly  = "System.ServiceModel";
            const String module    = "System.ServiceModel.dll";
            const String className = "System.ServiceModel.ChannelFactory";

            // void InitializeEndpoint(ServiceEndpoint endpoint)
            Decorator.Decorate(
                assembly, module,
                className + ".InitializeEndpoint",
                1,
                null,
                this.wcfClientProcessing.OnEndInitializeEndpoint1,
                null);

            // void InitializeEndpoint(Binding binding, EndpointAddress address)
            // void InitializeEndpoint(string configurationName, EndpointAddress address)
            Decorator.Decorate(
                assembly, module,
                className + ".InitializeEndpoint",
                2,
                this.wcfClientProcessing.OnStartInitializeEndpoint2,
                this.wcfClientProcessing.OnEndInitializeEndpoint2,
                null);

            // void InitializeEndpoint(string configurationName, EndpointAddress address, Configuration configuration)
            Decorator.Decorate(
                assembly, module,
                className + ".InitializeEndpoint",
                3,
                null,
                this.wcfClientProcessing.OnEndInitializeEndpoint3,
                null);
        }
Example #3
0
        public void ShouldThrowWhenDecoratingInvalidTarget()
        {
            var        proxyGenerator       = new ProxyGenerator();
            var        invalidateAllTargets = new ProxyGenerationOptions(new InvalidProxyHook());
            IDecorator classUnderTest       = new Decorator(proxyGenerator, invalidateAllTargets,
                                                            aspectStub);
            var dummyObject = new DummyObject();

            Assert.Throws <ArgumentException>(() =>
                                              classUnderTest.Decorate(typeof(DummyObject), dummyObject));
        }
 public override void TestInitialize()
 {
     Decorator.InitializeExtension(Environment.CurrentDirectory);
     Decorator.Decorate(
         "System",
         "System.dll",
         "System.Net.HttpWebRequest.GetResponse",
         0,
         this.OnBegin,
         this.OnEnd,
         this.OnException);
 }
Example #5
0
        /// <summary>
        /// Enables outgoing request instrumentation: inject generic correlation context in outgoing <see cref="System.Net.Http.HttpClient"/> or <see cref="System.Net.WebRequest"/> call
        /// </summary>
        /// <typeparam name="TContext">Type of correlation context</typeparam>
        /// <param name="configuration">Collection of <see cref="Common.IContextInjector{TContext,TRequest}"/> to inject context into outgoing request</param>
        public static void Enable <TContext>(Configuration <TContext, WebRequest, WebResponse> configuration) where TContext : ICorrelationContext <TContext>
        {
            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (configuration.ContextInjectors == null)
            {
                throw new ArgumentNullException(nameof(configuration.ContextInjectors));
            }

            if (!Decorator.IsHostEnabled())
            {
                throw new InvalidOperationException("Profiler is not available on the host");
            }

            var extesionBaseDirectory = string.IsNullOrWhiteSpace(AppDomain.CurrentDomain.RelativeSearchPath)
                ? AppDomain.CurrentDomain.BaseDirectory
                : AppDomain.CurrentDomain.RelativeSearchPath;

            Decorator.InitializeExtension(extesionBaseDirectory);

            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.BeginGetResponse",
                2,
                (thisObj, callback, state) => OnBeginGetResponse(thisObj, configuration),
                null,
                null);

            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.EndGetResponse",
                1,
                null,
                (context, returnValue, thisObj, asyncResult) => OnEndGetResponse(context, returnValue, thisObj, asyncResult, configuration),
                null);

            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.GetResponse",
                0,
                thisObj => OnBeginGetResponse(thisObj, configuration),
                (context, returnValue, thisObj) => OnEndGetResponse(context, returnValue, thisObj, null, configuration),
                null);
        }
Example #6
0
        public void Run(object sender, EventArgs e)
        {
            Screen s          = (Screen)sender;
            Grid   backupGrid = s.grid;

            if (s.previousGrid == null)
            {
                s.previousGrid = backupGrid;
            }

            s.grid = LawStratege.Apply(s.grid, Screen.conf.LawStrategy);

            if (s.grid.Equals(backupGrid) || s.previousGrid.Equals(s.grid))
            {
                s.Over = true;
            }

            s.previousGrid = backupGrid;
            s.matrice      = Decorator.Decorate(s.grid, Screen.conf.pixelSize);
        }
Example #7
0
 public override void DecorateProperty(object obj, PropertyInfo propertyInfo)
 {
     if (propertyInfo.CanWrite)
     {
         Type   returnType = propertyInfo.GetSetMethod().GetParameters()[0].ParameterType;
         Object instance   = Decorator.InitializeType <Object>(returnType, null);
         Decorator.Decorate <Object>(instance);
         propertyInfo.GetSetMethod().Invoke(obj, new Object[1] {
             instance
         });
     }
     else if (propertyInfo.CanRead)
     {
         Type   returnType = propertyInfo.GetGetMethod().ReturnType;
         Object instance   = Decorator.InitializeType <Object>(returnType, null);
         Decorator.Decorate <Object>(instance);
         obj.GetType().GetMethod("set___" + propertyInfo.Name).Invoke(obj, new Object[1] {
             instance
         });
     }
 }
Example #8
0
 public override Task Run(IService <TOutput> service)
 => _client.Run(Decorator.Decorate(service));
Example #9
0
 public DirectServiceChannel(Action <T> action)
     : base(new Service <object>())
 {
     _input  = Decorator.Decorate(new Service <T>(action));
     _action = action;
 }
Example #10
0
        internal static void DecorateProfilerForSql(ref ProfilerSqlProcessing sqlCallbacks)
        {
            // Decorate Sql ExecuteNonQuery
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.ExecuteNonQuery",
                0,
                sqlCallbacks.OnBeginForSync,
                sqlCallbacks.OnEndForSync,
                sqlCallbacks.OnExceptionForSync);

            // Decorate Sql ExecuteReader
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.ExecuteReader",
                2,
                sqlCallbacks.OnBeginForExecuteReader,
                sqlCallbacks.OnEndForExecuteReader,
                sqlCallbacks.OnExceptionForExecuteReader);

            // Decorate Sql ExecuteReader (we instrument 2 overloads of ExecuteReader because there are cases when methods get inlined or tail call optimized)
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.ExecuteReader",
                0,
                sqlCallbacks.OnBeginForSync,
                sqlCallbacks.OnEndForSync,
                sqlCallbacks.OnExceptionForSync);

            // Decorate Sql ExecuteScalar
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.ExecuteScalar",
                0,
                sqlCallbacks.OnBeginForSync,
                sqlCallbacks.OnEndForSync,
                sqlCallbacks.OnExceptionForSync);

            // Decorate Sql ExecuteXmlReader
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.ExecuteXmlReader",
                0,
                sqlCallbacks.OnBeginForSync,
                sqlCallbacks.OnEndForSync,
                sqlCallbacks.OnExceptionForSync);

            // Decorates Sql BeginExecuteNonQueryInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.BeginExecuteNonQueryInternal",
                4,
                sqlCallbacks.OnBeginForBeginExecuteNonQueryInternal,
                null,
                null);

            // Decorates Sql EndExecuteNonQueryInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.EndExecuteNonQueryInternal",
                1,
                null,
                sqlCallbacks.OnEndForSqlAsync,
                sqlCallbacks.OnExceptionForSqlAsync);

            // Decorates Sql BeginExecuteReaderInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.BeginExecuteReaderInternal",
                5,
                sqlCallbacks.OnBeginForBeginExecuteReaderInternal,
                null,
                null);

            // Decorates Sql EndExecuteReaderInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.EndExecuteReaderInternal",
                1,
                null,
                sqlCallbacks.OnEndForSqlAsync,
                sqlCallbacks.OnExceptionForSqlAsync);

            // Decorates Sql BeginExecuteXmlReaderInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.BeginExecuteXmlReaderInternal",
                4,
                sqlCallbacks.OnBeginForBeginExecuteXmlReaderInternal,
                null,
                null);

            // Decorates Sql EndExecuteXmlReaderInternal
            Decorator.Decorate(
                "System.Data",
                "System.Data.dll",
                "System.Data.SqlClient.SqlCommand.EndExecuteXmlReaderInternal",
                1,
                null,
                sqlCallbacks.OnEndForSqlAsync,
                sqlCallbacks.OnExceptionForSqlAsync);
        }
Example #11
0
        internal static void DecorateProfilerForHttp(ref ProfilerHttpProcessing httpCallbacks)
        {
            // Decorates Http GetResponse
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.GetResponse",
                0,
                httpCallbacks.OnBeginForGetResponse,
                httpCallbacks.OnEndForGetResponse,
                httpCallbacks.OnExceptionForGetResponse);

            // Decorates Http GetRequestStream
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.GetRequestStream",
                1,
                httpCallbacks.OnBeginForGetRequestStream,
                null,
                httpCallbacks.OnExceptionForGetRequestStream);

            // Decorates Http BeginGetResponse
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.BeginGetResponse",
                2,
                httpCallbacks.OnBeginForBeginGetResponse,
                null,
                null);

            // Decorates Http EndGetResponse
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.EndGetResponse",
                1,
                null,
                httpCallbacks.OnEndForEndGetResponse,
                httpCallbacks.OnExceptionForEndGetResponse);

            // Decorates Http BeginGetRequestStream
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.BeginGetRequestStream",
                2,
                httpCallbacks.OnBeginForBeginGetRequestStream,
                null,
                null);

            // Decorates Http EndGetRequestStream
            Decorator.Decorate(
                "System",
                "System.dll",
                "System.Net.HttpWebRequest.EndGetRequestStream",
                2,
                null,
                null,
                httpCallbacks.OnExceptionForEndGetRequestStream);
        }
Example #12
0
        public static void Main(string[] args)
        {
            var options = new Options();

            if (!Parser.Default.ParseArguments(args, options))
            {
                return;
            }
            if (options.InputFiles == null || options.InputFiles.Count == 0)
            {
                Console.Error.WriteLine("You must provide input files or directories.");
                Console.WriteLine(options.GetUsage());
                return;
            }

            var filesToAnalyze = new List <string>();

            foreach (var inputElement in options.InputFiles)
            {
                if (File.Exists(inputElement))
                {
                    filesToAnalyze.Add(inputElement);
                }
                else if (Directory.Exists(inputElement))
                {
                    filesToAnalyze.AddRange(LoadFilesFromDirectory(inputElement));
                }
                else
                {
                    Console.Error.WriteLine("File or directory {0} does not exist.", inputElement);
                    Console.WriteLine(options.GetUsage());
                    return;
                }
            }

            var outputDirectory = PrepareOutputDirectory(options.OutputDirectory);

            var serializer = new Serializer();

            using (var analysisFileStream = new FileStream(Path.Combine(outputDirectory.FullName, options.Analysis), FileMode.Create))
            {
                using (var serializerStream = serializer.ObtainOpenStreamSerializer(analysisFileStream))
                {
                    foreach (var inputFile in filesToAnalyze)
                    {
                        var assembly = AssemblyDefinition.ReadAssembly(inputFile);

                        var analyzer = new Analyzer();
                        analyzer.Analyze(assembly);

                        var blocks = analyzer.GetBlocks();
                        serializerStream.Serialize(blocks);

                        var decorator          = new Decorator(blocks);
                        var decoratedAssembly  = decorator.Decorate(assembly);
                        var assemblyTargetPath = Path.Combine(outputDirectory.FullName, inputFile);
                        EnsurePathExists(assemblyTargetPath);
                        decoratedAssembly.Write(assemblyTargetPath);

                        if (options.Console)
                        {
                            foreach (var block in blocks)
                            {
                                Console.WriteLine(block);
                            }
                        }
                    }
                }
            }
        }