相关疑难解决方法(0)

使用ASP.NET Core DI解析实例

如何使用ASP.NET Core MVC内置依赖注入框架手动解析类型?

设置容器很容易:

public void ConfigureServices(IServiceCollection services)
{
    // ...

    services.AddTransient<ISomeService, SomeConcreteService>();
}
Run Code Online (Sandbox Code Playgroud)

但是如何在ISomeService不进行注射的情况下解决?例如,我想这样做:

ISomeService service = services.Resolve<ISomeService>();
Run Code Online (Sandbox Code Playgroud)

没有这样的方法IServiceCollection.

c# dependency-injection asp.net-core-mvc asp.net-core

245
推荐指数
6
解决办法
17万
查看次数

NET Core MVC的Spring Boot Autowired注释等效

问题提到了这一切。

在春季启动中,我能够使用AutoWired注释自动将依赖项注入控制器。

class SomeController extends Controller {
    @AutoWired
    private SomeDependency someDependency;
}
Run Code Online (Sandbox Code Playgroud)

对于我很好奇它是否具有此批注,当前的实现方式是将其作为参数添加到构造函数中

[Route("api/[controller]")]
public class SomeController : Controller
{
    private SomeContext _someContext;

    public SomeController(SomeContext someContext)
    {
        _someContext = someContext;
    }
}
Run Code Online (Sandbox Code Playgroud)

c# dependency-injection autowired asp.net-core-mvc

6
推荐指数
1
解决办法
3271
查看次数