当前位置: 首页 > news >正文

网站升级改版需要多久苏州做网站推广的公司

网站升级改版需要多久,苏州做网站推广的公司,上海教育网站建设,北京哪里可以做网站本文内容 先决条件创建新的控制台应用程序添加接口添加默认实现添加需要 DI 的服务为 DI 注册服务结束语 本文介绍如何在 .NET 中使用依赖注入 (DI)。 借助 Microsoft 扩展#xff0c;可通过添加服务并在 IServiceCollection 中配置这些服务来管理 DI。 IHost 接口会公开 IS…本文内容 先决条件创建新的控制台应用程序添加接口添加默认实现添加需要 DI 的服务为 DI 注册服务结束语 本文介绍如何在 .NET 中使用依赖注入 (DI)。 借助 Microsoft 扩展可通过添加服务并在 IServiceCollection 中配置这些服务来管理 DI。 IHost 接口会公开 IServiceProvider 实例它充当所有已注册的服务的容器。 本文介绍如何执行下列操作 创建一个使用依赖注入的 .NET 控制台应用生成和配置通用主机编写多个接口及相应的实现为 DI 使用服务生存期和范围设定 1、先决条件 .NET Core 3.1 SDK 或更高版本。熟悉如何创建新的 .NET 应用程序以及如何安装 NuGet 包。 2、创建新的控制台应用程序 通过 dotnet new 命令或 IDE 的“新建项目”向导新建一个名为 ConsoleDI 的 .NET 控制台应用程序 Example 。 将 NuGet 包 Microsoft.Extensions.Hosting 添加到项目。 新的控制台应用项目文件应如下所示 Project SdkMicrosoft.NET.SdkPropertyGroupOutputTypeExe/OutputTypeTargetFrameworknet7.0/TargetFrameworkNullableenable/NullableImplicitUsingstrue/ImplicitUsingsRootNamespaceConsoleDI.Example/RootNamespace/PropertyGroupItemGroupPackageReference IncludeMicrosoft.Extensions.Hosting Version7.0.1 //ItemGroup/Project重要 在此示例中需要 NuGet 包 Microsoft.Extensions.Hosting 来生成和运行应用。 某些元包可能包含 Microsoft.Extensions.Hosting 包在这种情况下不需要显式包引用。 3、添加接口 在此示例应用中你将了解依赖项注入如何处理服务生存期。 你将创建多个表示不同服务生存期的接口。 将以下接口添加到项目根目录 IReportServiceLifetime.cs using Microsoft.Extensions.DependencyInjection;namespace ConsoleDI.Example;public interface IReportServiceLifetime {Guid Id { get; }ServiceLifetime Lifetime { get; } }IReportServiceLifetime 接口定义了以下项 表示服务的唯一标识符的 Guid Id 属性。表示服务生存期的 ServiceLifetime 属性。 IExampleTransientService.cs using Microsoft.Extensions.DependencyInjection;namespace ConsoleDI.Example;public interface IExampleTransientService : IReportServiceLifetime {ServiceLifetime IReportServiceLifetime.Lifetime ServiceLifetime.Transient; }IExampleScopedService.cs using Microsoft.Extensions.DependencyInjection;namespace ConsoleDI.Example;public interface IExampleScopedService : IReportServiceLifetime {ServiceLifetime IReportServiceLifetime.Lifetime ServiceLifetime.Scoped; }IExampleSingletonService.cs using Microsoft.Extensions.DependencyInjection;namespace ConsoleDI.Example;public interface IExampleSingletonService : IReportServiceLifetime {ServiceLifetime IReportServiceLifetime.Lifetime ServiceLifetime.Singleton; }IReportServiceLifetime 的所有子接口均使用默认值显式实现 IReportServiceLifetime.Lifetime。 例如IExampleTransientService 使用 ServiceLifetime.Transient 值显式实现 IReportServiceLifetime.Lifetime。 4、添加默认实现 该示例实现使用 Guid.NewGuid() 的结果初始化其 Id 属性。 将各种服务的下列默认实现类添加到项目根目录 ExampleTransientService.cs namespace ConsoleDI.Example;internal sealed class ExampleTransientService : IExampleTransientService {Guid IReportServiceLifetime.Id { get; } Guid.NewGuid(); }ExampleScopedService.cs namespace ConsoleDI.Example;internal sealed class ExampleScopedService : IExampleScopedService {Guid IReportServiceLifetime.Id { get; } Guid.NewGuid(); }ExampleSingletonService.cs namespace ConsoleDI.Example;internal sealed class ExampleSingletonService : IExampleSingletonService {Guid IReportServiceLifetime.Id { get; } Guid.NewGuid(); }每个实现都定义为 internal sealed 并实现其相应的接口。 例如ExampleSingletonService 会实现 IExampleSingletonService。 5、添加需要 DI 的服务 添加下列服务生存期报告器类它作为服务添加到控制台应用 ServiceLifetimeReporter.cs namespace ConsoleDI.Example;internal sealed class ServiceLifetimeReporter {private readonly IExampleTransientService _transientService;private readonly IExampleScopedService _scopedService;private readonly IExampleSingletonService _singletonService;public ServiceLifetimeReporter(IExampleTransientService transientService,IExampleScopedService scopedService,IExampleSingletonService singletonService) (_transientService, _scopedService, _singletonService) (transientService, scopedService, singletonService);public void ReportServiceLifetimeDetails(string lifetimeDetails){Console.WriteLine(lifetimeDetails);LogService(_transientService, Always different);LogService(_scopedService, Changes only with lifetime);LogService(_singletonService, Always the same);}private static void LogServiceT(T service, string message)where T : IReportServiceLifetime Console.WriteLine($ {typeof(T).Name}: {service.Id} ({message})); }ServiceLifetimeReporter 会定义一个构造函数该函数需要上述每一个服务接口即 IExampleTransientService、IExampleScopedService 和 IExampleSingletonService。 对象会公开一个方法使用者可通过该方法使用给定的 lifetimeDetails 参数报告服务。 被调用时ReportServiceLifetimeDetails 方法会使用服务生存期消息记录每个服务的唯一标识符。 日志消息有助于直观呈现服务生存期。 6、为 DI 注册服务 使用以下代码更新 Program.cs using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using ConsoleDI.Example;HostApplicationBuilder builder Host.CreateApplicationBuilder(args);builder.Services.AddTransientIExampleTransientService, ExampleTransientService(); builder.Services.AddScopedIExampleScopedService, ExampleScopedService(); builder.Services.AddSingletonIExampleSingletonService, ExampleSingletonService(); builder.Services.AddTransientServiceLifetimeReporter();using IHost host builder.Build();ExemplifyServiceLifetime(host.Services, Lifetime 1); ExemplifyServiceLifetime(host.Services, Lifetime 2);await host.RunAsync();static void ExemplifyServiceLifetime(IServiceProvider hostProvider, string lifetime) {using IServiceScope serviceScope hostProvider.CreateScope();IServiceProvider provider serviceScope.ServiceProvider;ServiceLifetimeReporter logger provider.GetRequiredServiceServiceLifetimeReporter();logger.ReportServiceLifetimeDetails(${lifetime}: Call 1 to provider.GetRequiredServiceServiceLifetimeReporter());Console.WriteLine(...);logger provider.GetRequiredServiceServiceLifetimeReporter();logger.ReportServiceLifetimeDetails(${lifetime}: Call 2 to provider.GetRequiredServiceServiceLifetimeReporter());Console.WriteLine(); }每个 services.Add{LIFETIME}{SERVICE} 扩展方法添加并可能配置服务。 我们建议应用遵循此约定。 将扩展方法置于 Microsoft.Extensions.DependencyInjection 命名空间中以封装服务注册的组。 还包括用于 DI 扩展方法的命名空间部分 Microsoft.Extensions.DependencyInjection 允许在不添加其他 using 块的情况下在 IntelliSense 中显示它们。在通常会调用这些扩展方法的 Program 或 Startup 类中避免出现过多的 using 语句。 应用会执行以下操作 使用默认活页夹设置创建一个 IHostBuilder 实例。配置服务并对其添加相应的服务生存期。调用 Build() 并分配 IHost 的实例。调用 ExemplifyScoping传入 IHost.Services。 7、结束语 在此示例应用中你创建了多个接口和相应的实现。 其中每个服务都唯一标识并与 ServiceLifetime 配对。 示例应用演示了如何针对接口注册服务实现以及如何在没有支持接口的情况下注册纯类。 然后示例应用演示了如何在运行时解析定义为构造函数参数的依赖项。 运行该应用时它会显示如下所示的输出 // Sample output: // Lifetime 1: Call 1 to provider.GetRequiredServiceServiceLifetimeReporter() // IExampleTransientService: d08a27fa-87d2-4a06-98d7-2773af886125 (Always different) // IExampleScopedService: 402c83c9-b4ed-4be1-b78c-86be1b1d908d (Changes only with lifetime) // IExampleSingletonService: a61f1ff4-0b14-4508-bd41-21d852484a7b (Always the same) // ... // Lifetime 1: Call 2 to provider.GetRequiredServiceServiceLifetimeReporter() // IExampleTransientService: b43d68fb-2c7b-4a9b-8f02-fc507c164326 (Always different) // IExampleScopedService: 402c83c9-b4ed-4be1-b78c-86be1b1d908d (Changes only with lifetime) // IExampleSingletonService: a61f1ff4-0b14-4508-bd41-21d852484a7b (Always the same) // // Lifetime 2: Call 1 to provider.GetRequiredServiceServiceLifetimeReporter() // IExampleTransientService: f3856b59-ab3f-4bbd-876f-7bab0013d392 (Always different) // IExampleScopedService: bba80089-1157-4041-936d-e96d81dd9d1c (Changes only with lifetime) // IExampleSingletonService: a61f1ff4-0b14-4508-bd41-21d852484a7b (Always the same) // ... // Lifetime 2: Call 2 to provider.GetRequiredServiceServiceLifetimeReporter() // IExampleTransientService: a8015c6a-08cd-4799-9ec3-2f2af9cbbfd2 (Always different) // IExampleScopedService: bba80089-1157-4041-936d-e96d81dd9d1c (Changes only with lifetime) // IExampleSingletonService: a61f1ff4-0b14-4508-bd41-21d852484a7b (Always the same)在应用输出中可看到 Transient 服务总是不同的每次检索服务时都会创建一个新实例。Scoped 服务只会随着新范围而改变但在一个范围中是相同的实例。Singleton 服务总是相同的新实例仅被创建一次。
http://www.w-s-a.com/news/866390/

相关文章:

  • 邢台专业做网站哪家好临沂网站建设中企动力
  • 建设网站是主营成本吗wordpress 后台
  • 猎头可以做单的网站企业网站建设
  • 建小程序需要网站吗在putty上怎样安装wordpress
  • 天津智能网站建设找哪家WordPress相册插件pro
  • 电脑网站页面怎么调大小济宁网站建设软件开发
  • 亿玛酷网站建设广州增城区最新消息
  • 企业网站视频栏目建设方案中企动力网站模板
  • 网站页面策划国外注册域名的网站
  • 百中搜如何做网站排名网站维护一年一般多少钱
  • 镇江地区做网站的公司wordpress说说加分类
  • 深圳高端网站设计免费的关键词优化软件
  • 视频网站公司沈阳网站建设服务
  • 网站全屏代码做网站必须用对方服务器
  • 网站速度慢wordpressssl正式申请后wordpress
  • 那个网站做玉石最专业西瓜创客少儿编程加盟
  • 备案时的网站建设方案书免费软件库
  • 惠州外贸网站建设网站模板 兼容ie8
  • 南京淄博网站建设方案php网站开发实训感想
  • 网站设计的含义只做恐怖片的网站
  • 网站改版方案ppt室内装修公司简介
  • 做色网站wordpress twenty ten
  • 马鞍山建设工程监督站建管处网站免费的海报模板网站
  • 类似百度的网站移动端的网站怎么做的
  • 网站开发需要什么文凭网站分析的优劣势
  • 海尔网站建设不足之处山东网站营销
  • 楚雄 网站建设广告设计一般人能学吗
  • 热搜榜排名前十山东seo多少钱
  • 衡水哪有建网站的吗企业信息系统英文
  • 有模板怎么建站wordpress媒体库图片路径