コード例 #1
0
    private static HostingApplication CreateApplication(IHttpContextFactory httpContextFactory = null, bool useHttpContextAccessor = false,
                                                        ActivitySource activitySource          = null)
    {
        var services = new ServiceCollection();

        services.AddOptions();
        if (useHttpContextAccessor)
        {
            services.AddHttpContextAccessor();
        }

        httpContextFactory ??= new DefaultHttpContextFactory(services.BuildServiceProvider());

        var hostingApplication = new HostingApplication(
            ctx => Task.CompletedTask,
            NullLogger.Instance,
            new DiagnosticListener("Microsoft.AspNetCore"),
            activitySource ?? new ActivitySource("Microsoft.AspNetCore"),
            DistributedContextPropagator.CreateDefaultPropagator(),
            httpContextFactory);

        return(hostingApplication);
    }
コード例 #2
0
    private static HostingApplication CreateApplication(out FeatureCollection features,
                                                        DiagnosticListener diagnosticListener = null, ActivitySource activitySource = null, ILogger logger = null, Action <DefaultHttpContext> configure = null)
    {
        var httpContextFactory = new Mock <IHttpContextFactory>();

        features = new FeatureCollection();
        features.Set <IHttpRequestFeature>(new HttpRequestFeature());
        var context = new DefaultHttpContext(features);

        configure?.Invoke(context);
        httpContextFactory.Setup(s => s.Create(It.IsAny <IFeatureCollection>())).Returns(context);
        httpContextFactory.Setup(s => s.Dispose(It.IsAny <HttpContext>()));

        var hostingApplication = new HostingApplication(
            ctx => Task.CompletedTask,
            logger ?? new NullScopeLogger(),
            diagnosticListener ?? new NoopDiagnosticListener(),
            activitySource ?? new ActivitySource("Microsoft.AspNetCore"),
            DistributedContextPropagator.CreateDefaultPropagator(),
            httpContextFactory.Object);

        return(hostingApplication);
    }
コード例 #3
0
 public void TestBuiltInPropagatorsAreCached()
 {
     Assert.Same(DistributedContextPropagator.CreateDefaultPropagator(), DistributedContextPropagator.CreateDefaultPropagator());
     Assert.Same(DistributedContextPropagator.CreateNoOutputPropagator(), DistributedContextPropagator.CreateNoOutputPropagator());
     Assert.Same(DistributedContextPropagator.CreatePassThroughPropagator(), DistributedContextPropagator.CreatePassThroughPropagator());
 }
コード例 #4
0
        public void TestAllPropagators()
        {
            RemoteExecutor.Invoke(() => {
                Assert.NotNull(DistributedContextPropagator.Current);

                //
                // Default Propagator
                //

                Assert.Same(DistributedContextPropagator.CreateDefaultPropagator(), DistributedContextPropagator.Current);

                TestDefaultPropagatorUsingW3CActivity(
                    DistributedContextPropagator.Current,
                    "Legacy1=true",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("     LegacyKey1     ", "    LegacyValue1    ")
                });

                TestDefaultPropagatorUsingHierarchicalActivity(
                    DistributedContextPropagator.Current,
                    "Legacy2=true",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("LegacyKey2", "LegacyValue2")
                });

                TestFields(DistributedContextPropagator.Current);

                //
                // NoOutput Propagator
                //

                DistributedContextPropagator.Current = DistributedContextPropagator.CreateNoOutputPropagator();
                Assert.NotNull(DistributedContextPropagator.Current);
                TestNoOutputPropagatorUsingHierarchicalActivity(
                    DistributedContextPropagator.Current,
                    "ActivityState=1",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("B1", "V1"), new KeyValuePair <string, string>(" B2 ", " V2 ")
                });

                TestNoOutputPropagatorUsingHierarchicalActivity(
                    DistributedContextPropagator.Current,
                    "ActivityState=2",
                    null);

                TestNoOutputPropagatorUsingW3CActivity(
                    DistributedContextPropagator.Current,
                    "ActivityState=1",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>(" B3 ", " V3"), new KeyValuePair <string, string>(" B4 ", " V4 "), new KeyValuePair <string, string>("B5", "V5")
                });

                TestNoOutputPropagatorUsingW3CActivity(
                    DistributedContextPropagator.Current,
                    "ActivityState=2",
                    null);

                TestFields(DistributedContextPropagator.Current);

                //
                // Pass Through Propagator
                //

                DistributedContextPropagator.Current = DistributedContextPropagator.CreatePassThroughPropagator();
                Assert.NotNull(DistributedContextPropagator.Current);
                TestPassThroughPropagatorUsingHierarchicalActivityWithParentChain(
                    DistributedContextPropagator.Current,
                    "PassThrough=true",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("PassThroughKey1", "PassThroughValue1"), new KeyValuePair <string, string>("PassThroughKey2", "PassThroughValue2")
                });

                TestPassThroughPropagatorUsingHierarchicalActivityWithParentId(
                    DistributedContextPropagator.Current,
                    "PassThrough1=true",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("PassThroughKey3", "PassThroughValue3"), new KeyValuePair <string, string>(" PassThroughKey4 ", " PassThroughValue4 ")
                });

                TestPassThroughPropagatorUsingW3CActivity(
                    DistributedContextPropagator.Current,
                    "PassThrough2=1",
                    new List <KeyValuePair <string, string> >()
                {
                    new KeyValuePair <string, string>("     PassThroughKey4     ", "    PassThroughValue4    ")
                });

                TestPassThroughPropagatorWithNullCurrent(DistributedContextPropagator.Current);

                TestFields(DistributedContextPropagator.Current);

                //
                // Test Current
                //

                Assert.Throws <ArgumentNullException>(() => DistributedContextPropagator.Current = null);
            }).Dispose();
        }