gpt4 book ai didi

c# - 无法使用 Ninject 将依赖项注入(inject)从 Angular 服务调用的 ASP.NET Web API Controller

转载 作者:行者123 更新时间:2023-11-30 14:56:29 24 4
gpt4 key购买 nike

我正在将 Ninject 与 ASP.NET MVC 4 一起使用。我正在使用存储库并希望进行构造函数注入(inject)以将存储库传递给其中一个 Controller 。

这是实现 StatTracker 接口(interface)的上下文对象 (EntityFramework):

public class StatTrackerRepository : IStatTrackerRepository
{
private GolfStatTrackerEntities _ctx;

public StatTrackerRepository(GolfStatTrackerEntities ctx)
{
_ctx = ctx;
}

public IQueryable<Facility> GetFacilites()
{
return _ctx.Facilities;
}
}

这是我的存储库界面:

public interface IStatTrackerRepository
{
IQueryable<Facility> GetFacilites();
}

然后调用我的家庭 Controller :

public class HomeController : Controller
{
public IStatTrackerRepository _repo { get; set; }

public HomeController(IStatTrackerRepository repo)
{
_repo = repo;
}

public ActionResult Index()
{
var facilities = _repo.GetFacilites().ToList();
return View(facilities);
}
}

页面加载正确,但是,一旦加载页面,它会立即使用调用 $http 方法的 angularjs Controller :

function facilityIndexController($scope, $http) {
$scope.data = [];
$http({ method: 'GET', url: '/api/facility' }).
success(function(result) {
angular.copy(result.data, $scope.data);
}).error(function() {
alert("Could not load facilities");
});
}

...调用以下 API Controller :

public class FacilityController : ApiController
{
public IStatTrackerRepository _repo { get; set; }
public GolfStatTrackerEntities ctx { get; set; }

//public FacilityController()
//{
// _repo = new StatTrackerRepository(ctx);
//}

public FacilityController(IStatTrackerRepository repo)
{
_repo = repo;
}

public IEnumerable<Facility> Get()
{
var facilities = _repo.GetFacilites().ToList();
return facilities;
}
}

....它属于 Angular $http 调用的错误函数,因为 FacilityController(IStatTrackerRepository repo) 从未运行过。

我曾尝试使用无参数构造函数为 FacilityController() 实例化 StatTrackerRepository(ctx),但是,当我这样做时,我得到了一个 NullReferenceException。

我的 Ninject 配置如下:

private static IKernel CreateKernel()
{
var kernel = new StandardKernel();
try
{
kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();

RegisterServices(kernel);
//GlobalConfiguration.Configuration.DependencyResolver =
// new NinjectResolver(kernel);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}


private static void RegisterServices(IKernel kernel)
{
kernel.Bind<GolfStatTrackerEntities>().To<GolfStatTrackerEntities>().InRequestScope();
kernel.Bind<IStatTrackerRepository>().To<StatTrackerRepository>().InRequestScope();
}

我不确定这是否是 Ninject 的问题,或者我实现 Ninject 的方式是否存在问题。注入(inject)似乎在 Home View 的初始加载上起作用,但是,当它使用 Angular 调用 API 时,与 Ninject 断开连接。

请帮忙。

最佳答案

我们最终在我们的一个旧项目中使用了类似的配置,并意识到我们需要向我们的 MVC/WebApi 应用程序添加更多的基础结构代码:

NinjectDependencyScope.cs

using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Web.Http.Dependencies;
using Ninject;
using Ninject.Syntax;

namespace YourAppNameSpace
{
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot _resolver;

internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
_resolver = resolver;
}

public void Dispose()
{
var disposable = _resolver as IDisposable;
if (disposable != null)
{
disposable.Dispose();
}

_resolver = null;
}

public object GetService(Type serviceType)
{
if (_resolver == null)
{
throw new ObjectDisposedException("this", "This scope has already been disposed");
}

return _resolver.TryGet(serviceType);
}

public IEnumerable<object> GetServices(Type serviceType)
{
if (_resolver == null)
{
throw new ObjectDisposedException("this", "This scope has already been disposed");
}

return _resolver.GetAll(serviceType);
}
}
}

NinjectDependencyResolver.cs

using System.Web.Http.Dependencies;
using Ninject;

namespace YourAppNameSpace
{
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
private readonly IKernel _kernel;

public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
_kernel = kernel;
}

public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(_kernel.BeginBlock());
}
}
}

然后我们将以下内容添加到 NinjectWebCommon.cs 文件中:

public static void RegisterNinject(HttpConfiguration configuration)
{
// Set Web API Resolver
configuration.DependencyResolver = new NinjectDependencyResolver(Bootstrapper.Kernel);

}

然后是Global.asax.cs文件,Application_Start方法:

NinjectWebCommon.RegisterNinject(GlobalConfiguration.Configuration);

关于c# - 无法使用 Ninject 将依赖项注入(inject)从 Angular 服务调用的 ASP.NET Web API Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22788538/

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