gpt4 book ai didi

c# - 在何处使用依赖注入(inject)注册上下文

转载 作者:太空宇宙 更新时间:2023-11-03 20:48:59 25 4
gpt4 key购买 nike

根据 this教程我应该:

使用依赖注入(inject)注册你的上下文

教程描述了我应该找到方法ConfigureServices() 并将建议的代码放入其中。

这是我的 startup.cs :

using Microsoft.Owin;
using Owin;

[assembly: OwinStartupAttribute(typeof(MyProject.Startup))]
namespace MyProject
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
}
}

所以我不知道该把代码放在哪里。

由于项目不兼容.net core 2.1,需要将Project->Property改为.Net Framework 4.6.1并安装包 Microsoft.EntityFrameworkCore.SqlServerMicrosoft.EntityFrameworkCore.Tools

我尝试将依赖注入(inject)添加到 global.asax.cs文件如下:

protected void Application_Start()
{
var services = new ServiceCollection();
ConfigureServices(services);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

private void ConfigureServices(IServiceCollection services)
{
var connection = @"Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;ConnectRetryCount=0";
services.AddDbContext<BloggingContext>(options => options.UseSqlServer(connection));
}

我成功完成了这一步并创建了 Controller 并且它可以工作,但是我没有选择正确的上下文 (BloggingContext),所以它创建了第二个数据库。所以,我需要用 BloggingContext 创建一个 Controller ,你知道怎么做吗?

最佳答案

显示的启动版本和教程相互冲突。

如果这是 Asp.Net Core MVC 应用程序,那么您可以自己添加该方法。 Startup 类是约定的一部分。

public partial class Startup {
//...

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

引用 App startup in ASP.NET Core

The ConfigureServices method

The ConfigureServices method is:

  • Optional.
  • Called by the host before the Configure method to configure the app's services.
  • Where configuration options are set by convention.

但是,如果您尝试在 Asp.Net MVC 5+ (这是 GitHub 项目的目标)中使用 .Net Core 技术,那么您将需要修改您的方法以适应使用.Net 核心依赖注入(inject)与非核心平台。

首先,您需要一个 IDependencyResolver(该版本的 Asp.Net MVC 使用的 DI 框架),以及一种用您自己的解析器替换默认解析器的方法。

public sealed class CoreDependencyResolver : System.Web.Mvc.IDependencyResolver {
private readonly System.Web.Mvc.IDependencyResolver mvcInnerResolver;
private readonly IServiceProvider serviceProvider;

public CoreDependencyResolver(IServiceProvider serviceProvider, System.Web.Mvc.IDependencyResolver dependencyResolver) {
this.serviceProvider = serviceProvider;
mvcInnerResolver = dependencyResolver;
}

public object GetService(Type serviceType) {
object result = this.serviceProvider.GetService(serviceType);
if (result == null && mvcInnerResolver != null)
result = mvcInnerResolver.GetService(serviceType);
return result;
}

public IEnumerable<object> GetServices(Type serviceType) {
IEnumerable<object> result = this.serviceProvider.GetServices(serviceType);
if (result == null && mvcInnerResolver != null)
result = mvcInnerResolver.GetServices(serviceType);
return result ?? new object[0];
}
}

自定义解析器就位后,您现在可以配置应用程序以使用它。

以您当前的示例为起点(评论意见)

protected void Application_Start() {
var services = new ServiceCollection();
ConfigureServices(services);
//build service provider
IServiceProvider provider = services.BuildServiceProvider();
//Get the current resolver used by MVC
var current = DependencyResolver.Current;
//use that and the provider to create your custom resolver
var resolver = new CoreDependencyResolver(provider, current);
//now set the MVC framework to use the resolver that wraps the service provider
//that was created from .Net Core Dependency Injection framework.
DependencyResolver.SetResolver(resolver);
//...

AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}

private void ConfigureServices(IServiceCollection services) {
//... omitted for brevity (register dependencies as normal)
}

关于c# - 在何处使用依赖注入(inject)注册上下文,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57413075/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com