.NET6 MiniAPI中如何实现依赖注入的复杂配置流程?

2026-03-30 14:151阅读0评论SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计780个文字,预计阅读时间需要4分钟。

.NET6 MiniAPI中如何实现依赖注入的复杂配置流程?

在面向对象编程中,遵循依赖倒置原则(Dependency Inversion Principle)。这意味着高层模块不应直接依赖低层模块,两者都应依赖于抽象。换句话说,要依赖于抽象,而不是具体的实现。如下所示,在完成时,应该避免直接依赖具体实现。

  在OOP里有依赖倒置原则 (The Dependency Inversion Principle),意思是 高层模块不应该依赖于底层模块,二者都应该依赖于抽象。换句话说,依赖于抽象,不要依赖于具体实现。

  如下图,在完成订单后要调用快送模块,这时就依赖快递模块的接口,而不是具体的快递模块。

  依赖关系注入 (Dependency Injection简称DI),是一种软件的设计模式,用来实现依赖之间的控制反转。asp.net core框架天生内置了这种技术。注入的地方称之为容器(与dcoker无关)或服务容器。
注入的时候可以分三种注入形态:

  • Transient
  • Scoped
  • Singleton

  从字面意思也能了解,三种形态是访问的范围不同,通过Demo来看一下吧。

  demo定义了TransientService,ScopedService,SingletonServie三个服务和它们对应的接口,分别在Service中实现了一个打印时间的Call方法。

  三种Service和他们的实现

public interface ITransientService
{
string Call();
}
public class TransientService : ITransientService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"TransientService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

public interface IScopedService
{
string Call();
}
public class ScopedService : IScopedService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"ScopedService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

public interface ISingletonService
{
string Call();
}
public class SingletonService : ISingletonService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"TSingletonService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

  把三种服务和它们的接口注入到服务容器中, 为了使演示更清晰,增加了一个app.Use的方法,根据调用服务的url来判断是否是对应服务,然后调用Call方法,最后再调用Map到的方法,也就是说每次调用会有两个Call调用。

var builder = WebApplication.CreateBuilder();

builder.Services.AddScoped<IScopedService, ScopedService>();
builder.Services.AddTransient<ITransientService, TransientService>();
builder.Services.AddSingleton<ISingletonService, SingletonService>();

var app = builder.Build();

app.Use(async (context, next) =>
{
if (context.Request.Path.HasValue)
{
switch (context.Request.Path.Value)
{
case string s when s.Contains("transient"):
var transientService = context.RequestServices.GetService<ITransientService>();
Console.WriteLine($"--------------{transientService?.Call()}");
break;
case string s when s.Contains("scoped"):
var scopedService = context.RequestServices.GetService<IScopedService>();
Console.WriteLine($"--------------{scopedService?.Call()}");
break;
case string s when s.Contains("singleton"):
var singletonService = context.RequestServices.GetService<ISingletonService>();
Console.WriteLine($"--------------{singletonService?.Call()}");
break;
}
}
await next.Invoke();
});

app.MapGet("/transient", (ITransientService transientService) => transientService.Call());
app.MapGet("/scoped", (IScopedService scopedService) => scopedService.Call());
app.MapGet("/singleton", (ISingletonService singletonService) => singletonService.Call());

app.Run();

看一下结果吧:

.NET6 MiniAPI中如何实现依赖注入的复杂配置流程?

ITransientService结果:两次调用皆不同

IScopedService结果:两次调用相同(一个www.1234xp.com/dafeng.html 处的文章,转载请说明出处】

本文共计780个文字,预计阅读时间需要4分钟。

.NET6 MiniAPI中如何实现依赖注入的复杂配置流程?

在面向对象编程中,遵循依赖倒置原则(Dependency Inversion Principle)。这意味着高层模块不应直接依赖低层模块,两者都应依赖于抽象。换句话说,要依赖于抽象,而不是具体的实现。如下所示,在完成时,应该避免直接依赖具体实现。

  在OOP里有依赖倒置原则 (The Dependency Inversion Principle),意思是 高层模块不应该依赖于底层模块,二者都应该依赖于抽象。换句话说,依赖于抽象,不要依赖于具体实现。

  如下图,在完成订单后要调用快送模块,这时就依赖快递模块的接口,而不是具体的快递模块。

  依赖关系注入 (Dependency Injection简称DI),是一种软件的设计模式,用来实现依赖之间的控制反转。asp.net core框架天生内置了这种技术。注入的地方称之为容器(与dcoker无关)或服务容器。
注入的时候可以分三种注入形态:

  • Transient
  • Scoped
  • Singleton

  从字面意思也能了解,三种形态是访问的范围不同,通过Demo来看一下吧。

  demo定义了TransientService,ScopedService,SingletonServie三个服务和它们对应的接口,分别在Service中实现了一个打印时间的Call方法。

  三种Service和他们的实现

public interface ITransientService
{
string Call();
}
public class TransientService : ITransientService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"TransientService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

public interface IScopedService
{
string Call();
}
public class ScopedService : IScopedService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"ScopedService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

public interface ISingletonService
{
string Call();
}
public class SingletonService : ISingletonService
{
public DateTime Time { get; init; } = DateTime.Now;
public string Call()
{
return $"TSingletonService {Time.ToString("yyyy-MM-dd HH:mm:ss.fffffff")} test……";
}
}

  把三种服务和它们的接口注入到服务容器中, 为了使演示更清晰,增加了一个app.Use的方法,根据调用服务的url来判断是否是对应服务,然后调用Call方法,最后再调用Map到的方法,也就是说每次调用会有两个Call调用。

var builder = WebApplication.CreateBuilder();

builder.Services.AddScoped<IScopedService, ScopedService>();
builder.Services.AddTransient<ITransientService, TransientService>();
builder.Services.AddSingleton<ISingletonService, SingletonService>();

var app = builder.Build();

app.Use(async (context, next) =>
{
if (context.Request.Path.HasValue)
{
switch (context.Request.Path.Value)
{
case string s when s.Contains("transient"):
var transientService = context.RequestServices.GetService<ITransientService>();
Console.WriteLine($"--------------{transientService?.Call()}");
break;
case string s when s.Contains("scoped"):
var scopedService = context.RequestServices.GetService<IScopedService>();
Console.WriteLine($"--------------{scopedService?.Call()}");
break;
case string s when s.Contains("singleton"):
var singletonService = context.RequestServices.GetService<ISingletonService>();
Console.WriteLine($"--------------{singletonService?.Call()}");
break;
}
}
await next.Invoke();
});

app.MapGet("/transient", (ITransientService transientService) => transientService.Call());
app.MapGet("/scoped", (IScopedService scopedService) => scopedService.Call());
app.MapGet("/singleton", (ISingletonService singletonService) => singletonService.Call());

app.Run();

看一下结果吧:

.NET6 MiniAPI中如何实现依赖注入的复杂配置流程?

ITransientService结果:两次调用皆不同

IScopedService结果:两次调用相同(一个www.1234xp.com/dafeng.html 处的文章,转载请说明出处】