2019年12月8日 星期日

ASP.NET Core 專案樣版的預設相依注入服務研究分析 1

ASP.NET Core 3.0 專案樣版的預設相依注入服務研究分析

在這篇文章中,將會使用 Visual Studio 2019 來使用預設的 專案樣板,使用 .NET Core 3.0 平台,依序建立起各個專案樣板中的 ASP.NET Core 的專案,接著,觀察這些專案中的預設相依性注入的服務有哪些。

ASP.NET Core 專案樣版的預設相依注入服務研究分析 1
ASP.NET Core 專案樣版的預設相依注入服務研究分析 2
ASP.NET Core 專案樣版的預設相依注入服務研究分析 3
ASP.NET Core 專案樣版的預設相依注入服務研究分析 4
ASP.NET Core 專案樣版的預設相依注入服務研究分析 5

為了要能夠了解到專案內的相依性注入容器內已經有哪些預設的服務已經注入完成,這裡需要取得 IServiceCollection 這個具體實作的物件,並且使用底下的方式,逐一取得相依性注入服務容器的各個已經註冊的型別對應關係。
foreach (var item in serviceDescriptors)
{
    var newObject = new ServiceRegistration()
    {
        Lifetime = item.Lifetime.ToString(),
        ServiceType = item.ServiceType.Name,
        ImplementationType = item.ImplementationType?.Name,
        ImplementationInstance = item.ImplementationInstance?.GetType().Name,
        ImplementationFactory = item.ImplementationFactory?.GetType().GenericTypeArguments[1].Name
        //ImplementationFactory = item.ImplementationFactory != null ? "Factory" : ""
    };
    list.Add(newObject);
}
其中,需要在 Startup 類別中宣告一個欄位 IServiceCollection Services,接著在 ConfigureServices 方法內,將傳入的 IServiceCollection 參數物件,設定給剛剛宣告好的 Services 欄位變數
最後,請在 Configure 方法內,將上述程式碼在這個方法內來執行,就可以取得現在相依性注入服務容器的各個型別對應註冊表了。
另外,在這篇文章中,也彙整了各種專案範本的 Program 與 Startup 類別的程式碼,大家可以參考與比對,在不同的專案範本中,這些程式碼的差異在哪裡呢?

空白的專案

  • 請先使用 Visual Studio 2019 在 [建立新專案]對話窗中,選擇建立一個 [ASP.NET Core Web 應用程式]
  • 在 [設定新的專案] 對話窗中,輸入適合的專案名稱
  • 在 [建立新的 ASP.NET Core Web 應用程式] 對話窗中,請選擇 [空白] 專案樣版
    可用於建立 ASP.NET Core 應用程式的空白專案範本,此範本不含任何內容
  • 點選右下方 [建立] 按鈕,完成此專案範本的建立工作
  • 現在可以開始執行這個專案,底下是這個專案的執行畫面截圖 
這個專案的 Program 類別的程式碼為
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
這個專案的 Startup 類別程式碼為
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
這裡是預設註冊的所有服務清單,總共有 71 個

All Services

IndexTypeLifetimeImplementationTypeImplementationInstanceImplementationFactory
1Microsoft.Extensions.Hosting.IHostingEnvironmentSingletonHostingEnvironment
2Microsoft.Extensions.Hosting.IHostEnvironmentSingletonHostingEnvironment
3Microsoft.Extensions.Hosting.HostBuilderContextSingletonHostBuilderContext
4Microsoft.Extensions.Configuration.IConfigurationSingletonIConfiguration
5Microsoft.Extensions.Hosting.IApplicationLifetimeSingletonIApplicationLifetime
6Microsoft.Extensions.Hosting.IHostApplicationLifetimeSingletonApplicationLifetime
7Microsoft.Extensions.Hosting.IHostLifetimeSingletonConsoleLifetime
8Microsoft.Extensions.Hosting.IHostSingletonHost
9Microsoft.Extensions.Options.IOptions`1SingletonOptionsManager`1
10Microsoft.Extensions.Options.IOptionsSnapshot`1ScopedOptionsManager`1
11Microsoft.Extensions.Options.IOptionsMonitor`1SingletonOptionsMonitor`1
12Microsoft.Extensions.Options.IOptionsFactory`1TransientOptionsFactory`1
13Microsoft.Extensions.Options.IOptionsMonitorCache`1SingletonOptionsCache`1
14Microsoft.Extensions.Logging.ILoggerFactorySingletonLoggerFactory
15Microsoft.Extensions.Logging.ILogger`1SingletonLogger`1
16Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonDefaultLoggerLevelConfigureOptions
17Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
18Microsoft.Extensions.Logging.Configuration.ILoggerProviderConfigurationFactorySingletonLoggerProviderConfigurationFactory
19Microsoft.Extensions.Logging.Configuration.ILoggerProviderConfiguration`1SingletonLoggerProviderConfiguration`1
20Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerFilterConfigureOptions
21Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigurationChangeTokenSource`1
22Microsoft.Extensions.Logging.Configuration.LoggingConfigurationSingletonLoggingConfiguration
23Microsoft.Extensions.Logging.ILoggerProviderSingletonConsoleLoggerProvider
24Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerProviderConfigureOptions`2
25Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerProviderOptionsChangeTokenSource`2
26Microsoft.Extensions.Logging.ILoggerProviderSingletonDebugLoggerProvider
27Microsoft.Extensions.Logging.EventSource.LoggingEventSourceSingletonLoggingEventSource
28Microsoft.Extensions.Logging.ILoggerProviderSingletonEventSourceLoggerProvider
29Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonEventLogFiltersConfigureOptions
30Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonEventLogFiltersConfigureOptionsChangeSource
31Microsoft.Extensions.Logging.ILoggerProviderSingletonEventLogLoggerProvider
32Microsoft.AspNetCore.Hosting.IWebHostEnvironmentSingletonHostingEnvironment
33Microsoft.AspNetCore.Hosting.IHostingEnvironmentSingletonHostingEnvironment
34Microsoft.AspNetCore.Hosting.IApplicationLifetimeSingletonGenericWebHostApplicationLifetime
35Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Hosting.GenericWebHostServiceOptions, Microsoft.AspNetCore.Hosting, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
36System.Diagnostics.DiagnosticListenerSingletonDiagnosticListener
37System.Diagnostics.DiagnosticSourceSingletonDiagnosticListener
38Microsoft.AspNetCore.Http.IHttpContextFactorySingletonDefaultHttpContextFactory
39Microsoft.AspNetCore.Http.IMiddlewareFactoryScopedMiddlewareFactory
40Microsoft.AspNetCore.Hosting.Builder.IApplicationBuilderFactorySingletonApplicationBuilderFactory
41Microsoft.AspNetCore.Connections.IConnectionListenerFactorySingletonSocketTransportFactory
42Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions, Microsoft.AspNetCore.Server.Kestrel.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientKestrelServerOptionsSetup
43Microsoft.AspNetCore.Hosting.Server.IServerSingletonKestrelServer
44Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions, Microsoft.AspNetCore.Server.Kestrel.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
45Microsoft.Extensions.Options.IPostConfigureOptions`1[[Microsoft.AspNetCore.HostFiltering.HostFilteringOptions, Microsoft.AspNetCore.HostFiltering, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonPostConfigureOptions`1
46Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.AspNetCore.HostFiltering.HostFilteringOptions, Microsoft.AspNetCore.HostFiltering, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigurationChangeTokenSource`1
47Microsoft.AspNetCore.Hosting.IStartupFilterTransientHostFilteringStartupFilter
48Microsoft.AspNetCore.Routing.IInlineConstraintResolverTransientDefaultInlineConstraintResolver
49Microsoft.Extensions.ObjectPool.ObjectPoolProviderTransientDefaultObjectPoolProvider
50Microsoft.Extensions.ObjectPool.ObjectPool`1[[Microsoft.AspNetCore.Routing.UriBuildingContext, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonObjectPool`1
51Microsoft.AspNetCore.Routing.Tree.TreeRouteBuilderTransientTreeRouteBuilder
52Microsoft.AspNetCore.Routing.RoutingMarkerServiceSingletonRoutingMarkerService
53Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Routing.RouteOptions, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientConfigureRouteOptions
54Microsoft.AspNetCore.Routing.EndpointDataSourceSingletonEndpointDataSource
55Microsoft.AspNetCore.Routing.ParameterPolicyFactorySingletonDefaultParameterPolicyFactory
56Microsoft.AspNetCore.Routing.Matching.MatcherFactorySingletonDfaMatcherFactory
57Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilderTransientDfaMatcherBuilder
58Microsoft.AspNetCore.Routing.Internal.DfaGraphWriterSingletonDfaGraphWriter
59Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher+LifetimeTransientLifetime
60Microsoft.AspNetCore.Routing.Matching.EndpointMetadataComparerSingletonEndpointMetadataComparer
61Microsoft.AspNetCore.Routing.LinkGeneratorSingletonDefaultLinkGenerator
62Microsoft.AspNetCore.Routing.IEndpointAddressScheme`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]SingletonEndpointNameAddressScheme
63Microsoft.AspNetCore.Routing.IEndpointAddressScheme`1[[Microsoft.AspNetCore.Routing.RouteValuesAddress, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRouteValuesAddressScheme
64Microsoft.AspNetCore.Routing.LinkParserSingletonDefaultLinkParser
65Microsoft.AspNetCore.Routing.Matching.EndpointSelectorSingletonDefaultEndpointSelector
66Microsoft.AspNetCore.Routing.MatcherPolicySingletonHttpMethodMatcherPolicy
67Microsoft.AspNetCore.Routing.MatcherPolicySingletonHostMatcherPolicy
68Microsoft.AspNetCore.Routing.Template.TemplateBinderFactorySingletonDefaultTemplateBinderFactory
69Microsoft.AspNetCore.Routing.Patterns.RoutePatternTransformerSingletonDefaultRoutePatternTransformer
70Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Hosting.GenericWebHostServiceOptions, Microsoft.AspNetCore.Hosting, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
71Microsoft.Extensions.Hosting.IHostedServiceSingletonGenericWebHostService

API 專案

  • 請先使用 Visual Studio 2019 在 [建立新專案]對話窗中,選擇建立一個 [ASP.NET Core Web 應用程式]
  • 在 [設定新的專案] 對話窗中,輸入適合的專案名稱
  • 在 [建立新的 ASP.NET Core Web 應用程式] 對話窗中,請選擇 [API] 專案樣版
    用於建立 ASP.NET Core 應用程式的專案範本,富有 RESTful HTTP 服務的控制器範例。此範本也可用於 ASP.NET Core MVC 的檢視及控制器
  • 點選右下方 [建立] 按鈕,完成此專案範本的建立工作
  • 現在可以開始執行這個專案,底下是這個專案的執行畫面截圖 
這個專案的 Program 類別的程式碼為
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
這個專案的 Startup 類別程式碼為
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
這裡是預設註冊的所有服務清單,總共有 159 個

All Services

IndexTypeLifetimeImplementationTypeImplementationInstanceImplementationFactory
1Microsoft.Extensions.Hosting.IHostingEnvironmentSingletonHostingEnvironment
2Microsoft.Extensions.Hosting.IHostEnvironmentSingletonHostingEnvironment
3Microsoft.Extensions.Hosting.HostBuilderContextSingletonHostBuilderContext
4Microsoft.Extensions.Configuration.IConfigurationSingletonIConfiguration
5Microsoft.Extensions.Hosting.IApplicationLifetimeSingletonIApplicationLifetime
6Microsoft.Extensions.Hosting.IHostApplicationLifetimeSingletonApplicationLifetime
7Microsoft.Extensions.Hosting.IHostLifetimeSingletonConsoleLifetime
8Microsoft.Extensions.Hosting.IHostSingletonHost
9Microsoft.Extensions.Options.IOptions`1SingletonOptionsManager`1
10Microsoft.Extensions.Options.IOptionsSnapshot`1ScopedOptionsManager`1
11Microsoft.Extensions.Options.IOptionsMonitor`1SingletonOptionsMonitor`1
12Microsoft.Extensions.Options.IOptionsFactory`1TransientOptionsFactory`1
13Microsoft.Extensions.Options.IOptionsMonitorCache`1SingletonOptionsCache`1
14Microsoft.Extensions.Logging.ILoggerFactorySingletonLoggerFactory
15Microsoft.Extensions.Logging.ILogger`1SingletonLogger`1
16Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonDefaultLoggerLevelConfigureOptions
17Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
18Microsoft.Extensions.Logging.Configuration.ILoggerProviderConfigurationFactorySingletonLoggerProviderConfigurationFactory
19Microsoft.Extensions.Logging.Configuration.ILoggerProviderConfiguration`1SingletonLoggerProviderConfiguration`1
20Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerFilterConfigureOptions
21Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigurationChangeTokenSource`1
22Microsoft.Extensions.Logging.Configuration.LoggingConfigurationSingletonLoggingConfiguration
23Microsoft.Extensions.Logging.ILoggerProviderSingletonConsoleLoggerProvider
24Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerProviderConfigureOptions`2
25Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.Console.ConsoleLoggerOptions, Microsoft.Extensions.Logging.Console, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLoggerProviderOptionsChangeTokenSource`2
26Microsoft.Extensions.Logging.ILoggerProviderSingletonDebugLoggerProvider
27Microsoft.Extensions.Logging.EventSource.LoggingEventSourceSingletonLoggingEventSource
28Microsoft.Extensions.Logging.ILoggerProviderSingletonEventSourceLoggerProvider
29Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonEventLogFiltersConfigureOptions
30Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.Extensions.Logging.LoggerFilterOptions, Microsoft.Extensions.Logging, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonEventLogFiltersConfigureOptionsChangeSource
31Microsoft.Extensions.Logging.ILoggerProviderSingletonEventLogLoggerProvider
32Microsoft.AspNetCore.Hosting.IWebHostEnvironmentSingletonHostingEnvironment
33Microsoft.AspNetCore.Hosting.IHostingEnvironmentSingletonHostingEnvironment
34Microsoft.AspNetCore.Hosting.IApplicationLifetimeSingletonGenericWebHostApplicationLifetime
35Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Hosting.GenericWebHostServiceOptions, Microsoft.AspNetCore.Hosting, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
36System.Diagnostics.DiagnosticListenerSingletonDiagnosticListener
37System.Diagnostics.DiagnosticSourceSingletonDiagnosticListener
38Microsoft.AspNetCore.Http.IHttpContextFactorySingletonDefaultHttpContextFactory
39Microsoft.AspNetCore.Http.IMiddlewareFactoryScopedMiddlewareFactory
40Microsoft.AspNetCore.Hosting.Builder.IApplicationBuilderFactorySingletonApplicationBuilderFactory
41Microsoft.AspNetCore.Connections.IConnectionListenerFactorySingletonSocketTransportFactory
42Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions, Microsoft.AspNetCore.Server.Kestrel.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientKestrelServerOptionsSetup
43Microsoft.AspNetCore.Hosting.Server.IServerSingletonKestrelServer
44Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerOptions, Microsoft.AspNetCore.Server.Kestrel.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
45Microsoft.Extensions.Options.IPostConfigureOptions`1[[Microsoft.AspNetCore.HostFiltering.HostFilteringOptions, Microsoft.AspNetCore.HostFiltering, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonPostConfigureOptions`1
46Microsoft.Extensions.Options.IOptionsChangeTokenSource`1[[Microsoft.AspNetCore.HostFiltering.HostFilteringOptions, Microsoft.AspNetCore.HostFiltering, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigurationChangeTokenSource`1
47Microsoft.AspNetCore.Hosting.IStartupFilterTransientHostFilteringStartupFilter
48Microsoft.AspNetCore.Routing.IInlineConstraintResolverTransientDefaultInlineConstraintResolver
49Microsoft.Extensions.ObjectPool.ObjectPoolProviderTransientDefaultObjectPoolProvider
50Microsoft.Extensions.ObjectPool.ObjectPool`1[[Microsoft.AspNetCore.Routing.UriBuildingContext, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonObjectPool`1
51Microsoft.AspNetCore.Routing.Tree.TreeRouteBuilderTransientTreeRouteBuilder
52Microsoft.AspNetCore.Routing.RoutingMarkerServiceSingletonRoutingMarkerService
53Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Routing.RouteOptions, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientConfigureRouteOptions
54Microsoft.AspNetCore.Routing.EndpointDataSourceSingletonEndpointDataSource
55Microsoft.AspNetCore.Routing.ParameterPolicyFactorySingletonDefaultParameterPolicyFactory
56Microsoft.AspNetCore.Routing.Matching.MatcherFactorySingletonDfaMatcherFactory
57Microsoft.AspNetCore.Routing.Matching.DfaMatcherBuilderTransientDfaMatcherBuilder
58Microsoft.AspNetCore.Routing.Internal.DfaGraphWriterSingletonDfaGraphWriter
59Microsoft.AspNetCore.Routing.Matching.DataSourceDependentMatcher+LifetimeTransientLifetime
60Microsoft.AspNetCore.Routing.Matching.EndpointMetadataComparerSingletonEndpointMetadataComparer
61Microsoft.AspNetCore.Routing.LinkGeneratorSingletonDefaultLinkGenerator
62Microsoft.AspNetCore.Routing.IEndpointAddressScheme`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]SingletonEndpointNameAddressScheme
63Microsoft.AspNetCore.Routing.IEndpointAddressScheme`1[[Microsoft.AspNetCore.Routing.RouteValuesAddress, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRouteValuesAddressScheme
64Microsoft.AspNetCore.Routing.LinkParserSingletonDefaultLinkParser
65Microsoft.AspNetCore.Routing.Matching.EndpointSelectorSingletonDefaultEndpointSelector
66Microsoft.AspNetCore.Routing.MatcherPolicySingletonHttpMethodMatcherPolicy
67Microsoft.AspNetCore.Routing.MatcherPolicySingletonHostMatcherPolicy
68Microsoft.AspNetCore.Routing.Template.TemplateBinderFactorySingletonDefaultTemplateBinderFactory
69Microsoft.AspNetCore.Routing.Patterns.RoutePatternTransformerSingletonDefaultRoutePatternTransformer
70Microsoft.AspNetCore.Mvc.ApplicationParts.ApplicationPartManagerSingletonApplicationPartManager
71Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Mvc.MvcOptions, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientMvcCoreMvcOptionsSetup
72Microsoft.Extensions.Options.IPostConfigureOptions`1[[Microsoft.AspNetCore.Mvc.MvcOptions, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientMvcOptionsConfigureCompatibilityOptions
73Microsoft.Extensions.Options.IPostConfigureOptions`1[[Microsoft.AspNetCore.Mvc.MvcOptions, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientMvcCoreMvcOptionsSetup
74Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Mvc.ApiBehaviorOptions, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientApiBehaviorOptionsSetup
75Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Routing.RouteOptions, Microsoft.AspNetCore.Routing, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientMvcCoreRouteOptionsSetup
76Microsoft.AspNetCore.Mvc.ApplicationModels.ApplicationModelFactorySingletonApplicationModelFactory
77Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProviderTransientDefaultApplicationModelProvider
78Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProviderTransientApiBehaviorApplicationModelProvider
79Microsoft.AspNetCore.Mvc.Abstractions.IActionDescriptorProviderTransientControllerActionDescriptorProvider
80Microsoft.AspNetCore.Mvc.Infrastructure.IActionDescriptorCollectionProviderSingletonDefaultActionDescriptorCollectionProvider
81Microsoft.AspNetCore.Mvc.Infrastructure.IActionSelectorSingletonActionSelector
82Microsoft.AspNetCore.Mvc.ActionConstraints.ActionConstraintCacheSingletonActionConstraintCache
83Microsoft.AspNetCore.Mvc.ActionConstraints.IActionConstraintProviderTransientDefaultActionConstraintProvider
84Microsoft.AspNetCore.Routing.MatcherPolicySingletonConsumesMatcherPolicy
85Microsoft.AspNetCore.Routing.MatcherPolicySingletonActionConstraintMatcherPolicy
86Microsoft.AspNetCore.Mvc.Controllers.IControllerFactorySingletonDefaultControllerFactory
87Microsoft.AspNetCore.Mvc.Controllers.IControllerActivatorTransientDefaultControllerActivator
88Microsoft.AspNetCore.Mvc.Controllers.IControllerFactoryProviderSingletonControllerFactoryProvider
89Microsoft.AspNetCore.Mvc.Controllers.IControllerActivatorProviderSingletonControllerActivatorProvider
90Microsoft.AspNetCore.Mvc.Controllers.IControllerPropertyActivatorTransientDefaultControllerPropertyActivator
91Microsoft.AspNetCore.Mvc.Infrastructure.IActionInvokerFactorySingletonActionInvokerFactory
92Microsoft.AspNetCore.Mvc.Abstractions.IActionInvokerProviderTransientControllerActionInvokerProvider
93Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvokerCacheSingletonControllerActionInvokerCache
94Microsoft.AspNetCore.Mvc.Filters.IFilterProviderSingletonDefaultFilterProvider
95Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultTypeMapperSingletonActionResultTypeMapper
96Microsoft.AspNetCore.Mvc.Filters.RequestSizeLimitFilterTransientRequestSizeLimitFilter
97Microsoft.AspNetCore.Mvc.Filters.DisableRequestSizeLimitFilterTransientDisableRequestSizeLimitFilter
98Microsoft.AspNetCore.Mvc.Filters.RequestFormLimitsFilterTransientRequestFormLimitsFilter
99Microsoft.AspNetCore.Mvc.ModelBinding.IModelMetadataProviderSingletonDefaultModelMetadataProvider
100Microsoft.AspNetCore.Mvc.ModelBinding.Metadata.ICompositeMetadataDetailsProviderTransientICompositeMetadataDetailsProvider
101Microsoft.AspNetCore.Mvc.ModelBinding.IModelBinderFactorySingletonModelBinderFactory
102Microsoft.AspNetCore.Mvc.ModelBinding.Validation.IObjectModelValidatorSingletonIObjectModelValidator
103Microsoft.AspNetCore.Mvc.ModelBinding.Validation.ClientValidatorCacheSingletonClientValidatorCache
104Microsoft.AspNetCore.Mvc.ModelBinding.ParameterBinderSingletonParameterBinder
105Microsoft.Extensions.DependencyInjection.MvcMarkerServiceSingletonMvcMarkerService
106Microsoft.AspNetCore.Mvc.Infrastructure.ITypeActivatorCacheSingletonTypeActivatorCache
107Microsoft.AspNetCore.Mvc.Routing.IUrlHelperFactorySingletonUrlHelperFactory
108Microsoft.AspNetCore.Mvc.Infrastructure.IHttpRequestStreamReaderFactorySingletonMemoryPoolHttpRequestStreamReaderFactory
109Microsoft.AspNetCore.Mvc.Infrastructure.IHttpResponseStreamWriterFactorySingletonMemoryPoolHttpResponseStreamWriterFactory
110System.Buffers.ArrayPool`1[[System.Byte, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]SingletonTlsOverPerCoreLockedStacksArrayPool`1
111System.Buffers.ArrayPool`1[[System.Char, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]]SingletonTlsOverPerCoreLockedStacksArrayPool`1
112Microsoft.AspNetCore.Mvc.Infrastructure.OutputFormatterSelectorSingletonDefaultOutputFormatterSelector
113Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.ObjectResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonObjectResultExecutor
114Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.PhysicalFileResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonPhysicalFileResultExecutor
115Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.VirtualFileResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonVirtualFileResultExecutor
116Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.FileStreamResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonFileStreamResultExecutor
117Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.FileContentResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonFileContentResultExecutor
118Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.RedirectResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRedirectResultExecutor
119Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.LocalRedirectResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonLocalRedirectResultExecutor
120Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.RedirectToActionResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRedirectToActionResultExecutor
121Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.RedirectToRouteResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRedirectToRouteResultExecutor
122Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.RedirectToPageResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonRedirectToPageResultExecutor
123Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.ContentResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonContentResultExecutor
124Microsoft.AspNetCore.Mvc.Infrastructure.IActionResultExecutor`1[[Microsoft.AspNetCore.Mvc.JsonResult, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonSystemTextJsonResultExecutor
125Microsoft.AspNetCore.Mvc.Infrastructure.IClientErrorFactorySingletonProblemDetailsClientErrorFactory
126Microsoft.AspNetCore.Mvc.Infrastructure.ProblemDetailsFactorySingletonDefaultProblemDetailsFactory
127Microsoft.AspNetCore.Mvc.Routing.MvcRouteHandlerSingletonMvcRouteHandler
128Microsoft.AspNetCore.Mvc.Routing.MvcAttributeRouteHandlerTransientMvcAttributeRouteHandler
129Microsoft.AspNetCore.Mvc.Routing.ControllerActionEndpointDataSourceSingletonControllerActionEndpointDataSource
130Microsoft.AspNetCore.Mvc.Routing.ActionEndpointFactorySingletonActionEndpointFactory
131Microsoft.AspNetCore.Mvc.Routing.DynamicControllerEndpointSelectorSingletonDynamicControllerEndpointSelector
132Microsoft.AspNetCore.Routing.MatcherPolicySingletonDynamicControllerEndpointMatcherPolicy
133Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterConfigurationProviderSingletonMiddlewareFilterConfigurationProvider
134Microsoft.AspNetCore.Mvc.Filters.MiddlewareFilterBuilderSingletonMiddlewareFilterBuilder
135Microsoft.AspNetCore.Hosting.IStartupFilterSingletonMiddlewareFilterBuilderStartupFilter
136Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProviderSingletonApiDescriptionGroupCollectionProvider
137Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionProviderTransientDefaultApiDescriptionProvider
138Microsoft.AspNetCore.Authentication.IAuthenticationServiceScopedAuthenticationService
139Microsoft.AspNetCore.Authentication.IClaimsTransformationSingletonNoopClaimsTransformation
140Microsoft.AspNetCore.Authentication.IAuthenticationHandlerProviderScopedAuthenticationHandlerProvider
141Microsoft.AspNetCore.Authentication.IAuthenticationSchemeProviderSingletonAuthenticationSchemeProvider
142Microsoft.AspNetCore.Authorization.IAuthorizationServiceTransientDefaultAuthorizationService
143Microsoft.AspNetCore.Authorization.IAuthorizationPolicyProviderTransientDefaultAuthorizationPolicyProvider
144Microsoft.AspNetCore.Authorization.IAuthorizationHandlerProviderTransientDefaultAuthorizationHandlerProvider
145Microsoft.AspNetCore.Authorization.IAuthorizationEvaluatorTransientDefaultAuthorizationEvaluator
146Microsoft.AspNetCore.Authorization.IAuthorizationHandlerContextFactoryTransientDefaultAuthorizationHandlerContextFactory
147Microsoft.AspNetCore.Authorization.IAuthorizationHandlerTransientPassThroughAuthorizationHandler
148Microsoft.AspNetCore.Authorization.Policy.AuthorizationPolicyMarkerServiceSingletonAuthorizationPolicyMarkerService
149Microsoft.AspNetCore.Authorization.Policy.IPolicyEvaluatorTransientPolicyEvaluator
150Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProviderTransientAuthorizationApplicationModelProvider
151Microsoft.AspNetCore.Cors.Infrastructure.ICorsServiceTransientCorsService
152Microsoft.AspNetCore.Cors.Infrastructure.ICorsPolicyProviderTransientDefaultCorsPolicyProvider
153Microsoft.AspNetCore.Mvc.ApplicationModels.IApplicationModelProviderTransientCorsApplicationModelProvider
154Microsoft.AspNetCore.Mvc.Cors.CorsAuthorizationFilterTransientCorsAuthorizationFilter
155Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Mvc.MvcOptions, Microsoft.AspNetCore.Mvc.Core, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]TransientMvcDataAnnotationsMvcOptionsSetup
156Microsoft.AspNetCore.Mvc.DataAnnotations.IValidationAttributeAdapterProviderSingletonValidationAttributeAdapterProvider
157Microsoft.AspNetCore.Mvc.Formatters.FormatFilterSingletonFormatFilter
158Microsoft.Extensions.Options.IConfigureOptions`1[[Microsoft.AspNetCore.Hosting.GenericWebHostServiceOptions, Microsoft.AspNetCore.Hosting, Version=3.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60]]SingletonConfigureNamedOptions`1
159Microsoft.Extensions.Hosting.IHostedServiceSingletonGenericWebHostService






2019年12月7日 星期六

我的 Bootstrap 使用 VS Code 的學習工具

我的 Bootstrap 使用 VS Code 的學習工具

更多關於 Blazor 教學影片,可以參考 Blazor 教學影片播放清單 或者 Blazor 快速體驗教學影片撥放清單。也歡迎訂閱本 .NET / Blazor / Xamarin.Forms 影片頻道 。


今年 2019 年, ASP.NET Core 3.0 正式推出支援 Blazor Server ,所以,對於我而言,現在正是一個回歸到網頁開發的好時機,因為,當在進行網站開發的時候,退去的 JavaScript 程式語言,與其相關開發框架,jQuery, Angular , Reace, Vue 等等,對於身為一個 .NET 開發者而言,想要進軍網站開發應用程式之列,就僅剩下了 CSS / Bootstrap 了 。
因此,我決定使用底下工具來進行學習如何使用 Bootstrap 4.x 的工具。
接著,請點選左下角的齒輪圖示,選擇 [設定] 選項,接著,請使用 [使用者] 模式來進行設定
  • 請搜尋 Editor.FormatOnSave 這個文字,將其設定為啟用狀態
  • 請搜尋 Editor.FontSize 這個文字,設定適當的大小
  • 請搜尋 Editor.wordWrap 這個文字,設定為 on

2019年11月29日 星期五

.NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 5 在多執行緒下,不要全部都使用執行緒同步機制 來做到執行緒安全

.NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 5 在多執行緒下,不要全部都使用執行緒同步機制 來做到執行緒安全

  1. .NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 1 在單一執行緒下,同步執行加一與減一方法
  2. .NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 2 在多執行緒下,非同步執行加一與減一方法,造成執行緒不安全的現象
  3. .NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 3 在多執行緒下,使用 lock 關鍵字來做到執行緒安全
  4. .NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 4 在多執行緒下,使用 Interlocked 來做到執行緒安全
  5. .NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 5 在多執行緒下,不要全部都使用執行緒同步機制 來做到執行緒安全


從上一篇文章中,.NET C# 單執行緒 同步 多執行緒 非同步 執行緒同步 Synchronization 邏輯處理器數量 設計探討 : Part 4 在多執行緒下,使用 Interlocked 來做到執行緒安全 中,雖然將核心模式的執行緒同步機制,變更成為使用者模式的同步機制,得到的是執行緒安全的執行結果,不過,執行效能卻還是明顯的很大的落差。
在進行多執行緒非同步程式設計的時候,若多個執行緒要同時存取同一個共用物件的時候,會因為有競賽條件 Race Condition 的問題,而造成執行結果不正確的問題,這個時候需要導入執行緒同步機制來解決執行結果不確定或者不正確的問題;可是,不論導入的是核心模式或者使用者模式的執行緒同步機制,都會造成執行效能的影響;真造想要提升執行速度,唯一個解決方案就是不要使用執行緒同步機制,這樣就可以提升執行速度,而解決的其中一個方式就是透過了該執行緒委派方法內的區域變數 Local Variable 來解決此一問題,在這裡因為該區域變數存在於每個執行緒內的堆疊記憶體中,當要進行存取這些區域變數物件的時候,是不會有執行緒競賽條件的問題,每個執行緒都會存取自己本身的變數;在這個應用範例中,可以設計當整個迴圈都執行完畢之後,再把儲存在區域變數的執行結果,使用核心模式同步機制API或者使用者模式同步機制API,將結果更新到共享靜態變數中,這樣,就會降低整體使用執行緒同步機制的次數,當然,也會提升整體執行速度囉。
現在,來看看要如何這樣處理。

減少使用同步機制的次數

因為對於 counter++ 與 counter-- 這兩個運算式,是屬於 可部分完成的作業 (也就是非 Atomic Operation),因此,在這裡將會使用 Interlocked 所提供的靜態方法來做到同樣的需求。
C Sharp / C#
class AddSub
{
    public static int counter = 0;
    public void Adds(AddSubAction addSubAction = AddSubAction.NoLock)
    {
        int localCounter = 0;
        for (int i = 0; i < int.MaxValue; i++)
        {
                localCounter++;
        }
            Interlocked.Add(ref counter, localCounter);
    }
    public void Subs(AddSubAction addSubAction = AddSubAction.NoLock)
    {
        int localCounter = 0;
        for (int i = 0; i < int.MaxValue; i++)
        {
                localCounter--;
        }
            Interlocked.Add(ref counter, localCounter);
    }
}
在上面的程式碼中,將會把原先的 counter++ 運算式變更成為呼叫 localCounter++,其中, localCounter 這個變數是定義在這個方法內,並不會與其他的執行緒共享,每個執行緒僅能夠存去到自己本身的 localCounter 變數,因此,在這個大量次數的迴圈內,進行 localCounter++ 或者 localCounter-- 計算,並不會造成有執行緒競爭條件的問題產生。
當迴圈執行完畢之後,會將 localCounter 變數的值,是設定到共享靜態變數 counter 內,此時,將會透使用者模式提供的執行緒同步機制 Interlocked,將區域變數的數值,加總到共享靜態變數上,如此,在加一或者減一的方法內,就會存在著一次執行緒同步的呼叫。
現在,來看看指定不同邏輯處理器數量下的執行結果。
ThreadSynchronization yes NoLockByLocal 10000000
Counter=0, 4,923ms

ThreadSynchronization yes NoLockByLocal 10100000
Counter=0, 2,482ms

ThreadSynchronization yes NoLockByLocal 10101000
Counter=0, 2,593ms

ThreadSynchronization yes NoLockByLocal 11000000
Counter=0, 4,835ms

ThreadSynchronization yes NoLockByLocal 11100000
Counter=0, 3,102ms

ThreadSynchronization yes NoLockByLocal 11110000
Counter=0, 3,339ms

ThreadSynchronization yes NoLockByLocal 11111111
Counter=0, 3,148ms
從上面的執行結果可以看出,不論執行效能與執行結果的正確性,都是令人相當滿意的。
+

執行結果是正確的,Counter 這個共享靜態變數,經過多執行緒的加一與減一非同步計算的結果,得到的是 0,這是符合預期的;而且,不論執行幾次,使用多少數量的邏輯處理器,執行結果都是相同的,而且若是同時使用更多的執行緒來執行這兩個加一與減一的方法,執行結果也都是 Counter=0。
對於執行效能上,表現得也是相當的亮眼,除了 10000000 與 11000000 這兩種邏輯處理器數量模式,需要花費大約 5 秒的時間 (這個時間已經比起同步執行程式碼的 7.5 秒,提升了不少執行效能),對於採用其他邏輯處理器數量的模式下,大約可以維持 2.5秒 ~ 3.3 秒之間的處理速度;對於 10000000 與 11000000 這兩種邏輯處理器數量模式,其實都是指定在同一個 CPU Core 上來執行,因為這台電腦的 CPU 具備了 Hyper-Threading 技術,同一個 CPU Core 可以具備有兩個 邏輯處理器效果,但是,這並不代表這個 CPU Core 的可以處理效能具備有兩倍能力,畢竟 Hyper-Threading 技術僅能夠提升執行速度約 20~30%;而當指定了不同 CPU Core 的時候,就可以看到多執行緒程式的執行速度明顯的提升相當的多。