使用Ninject和Windows服务的任何好例子?我不确定我需要什么扩展.另外,不确定作文根应该是什么?在那里使用Ninject与Windows服务的任何好例子?
我试图用上述技术构建一些基础项目.我希望获得最大的灵活性和可测试性,因此我尝试使用模式将其作为未来项目的基础.然而,它似乎有些不对劲,我真的需要帮助.所以我有两个问题:
我目前的代码有什么问题吗?我正确应用了模式?是否有任何建议或建议可以引导我朝着正确的方向前进?
为什么这段代码实际连接到数据库,创建它,但是即使我执行了更正操作,也不支持插入?(有关此错误的详细信息,请查看帖子的结尾)修复
我相信这也可以帮助别人,因为我没有找到足够的信息来正确地做出一些事情.我很确定很多人都试图以正确的方式做到这一点,并且如果我正在做的事情是正确的,我不确定.
我有两个实体:评论和评论
评论
public class Comment
{
[Key]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Author { get; set; }
public virtual string Body { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
评论
public class Review
{
[Key]
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual string Author { get; set; }
public virtual string Body { …Run Code Online (Sandbox Code Playgroud) 我在使用Ninject的InSingletonScope与Web Api RC绑定时遇到了一些困难.无论我如何创建绑定,看起来Web Api可能正在处理范围/生命周期而不是Ninject.
我尝试了一些关于连接Ninject的变种.最常见的是与此处的答案相同: ASP.NET Web API与ninject的绑定
我也试过这个版本:http: //www.peterprovost.org/blog/2012/06/19/adding-ninject-to-web-api/
在两者中,我实际上创建了一个开箱即用的Web Api项目,然后添加Ninject包,如任一帖子中所述.最后,我正在为StackOverflow版本添加Resolver和Scope类,例如:
public class NinjectDependencyScope : IDependencyScope
{
private IResolutionRoot resolver;
internal NinjectDependencyScope(IResolutionRoot resolver)
{
Contract.Assert(resolver != null);
this.resolver = resolver;
}
public void Dispose()
{
IDisposable 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 …Run Code Online (Sandbox Code Playgroud) 我正在寻找一种方法将我的连接注入我的存储库.我尝试注入SqlConnection使用IDBConnection,但是在NInject尝试停用连接时遇到了一些问题,从未调用过该事件.我无法弄清楚如何将连接字符串注入我的存储库.
有人可以给我一个建议吗?
这是跟踪:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Type 'ProjectName.Web.Api.Controllers.ContinentsController' does not have a default constructor
</ExceptionMessage>
<ExceptionType>System.ArgumentException</ExceptionType>
<StackTrace>
at System.Linq.Expressions.Expression.New(Type type)
at System.Web.Http.Internal.TypeActivator.Create[TBase](Type instanceType)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.GetInstanceOrActivator(HttpRequestMessage request, Type controllerType, Func`1& activator)
at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
</StackTrace>
</Error>
Run Code Online (Sandbox Code Playgroud)
我觉得这很奇怪,public class UsersController : ApiController { ... }工作得很好.我比较了2个控制器,所有设置和结构都相似.
我正在使用Ninject我的系统设置类似于Jamie Kurtz Asp.Net Mvc 4和Web Api:从开始到完成构建REST服务.
从堆栈跟踪,是否有人能够发现问题以及如何解决它?谢谢!
按照要求.
ContinentsController
[LoggingNHibernateSession]
public class ContinentsController : ApiController
{
private readonly ISession _session;
private readonly IContinentMapper _continentMapper;
private readonly …Run Code Online (Sandbox Code Playgroud) c# ninject inversion-of-control asp.net-mvc-4 asp.net-web-api
当我有一个没有默认构造函数的类,即使用依赖注入来传递其依赖项时,可以Newtonsoft.Json创建这样的对象吗?
例如:
public class SomeFoo
{
private readonly IFooDependency _dependency;
public SomeFoo(IFooDependency dependency){
if(dependency == null)
throw new ArgumentNullException("dependency");
_dependency = dependency;
}
public string Data { get; set; }
public int MoreData { get; set; }
public void DoFoo(){
Data = _dependency.GetFooData();
MoreData = _dependency.GetMoreFooDate();
}
}
Run Code Online (Sandbox Code Playgroud)
在序列化过程中,我只关心存储Data和MoreData(以及对象的类型,但暂时不要让事情复杂化).现在,为了反序列化,我可以调用类似的东西
var obj = JsonConvert.DeserializeObject<SomeFoo>(jsonText);
Run Code Online (Sandbox Code Playgroud)
如何让JsonConvert了解我的DI容器?
(注意:解决方法是在我的类中始终使用默认构造函数,并在那里调用Service Locator以获取我需要的任何依赖项.我只是在寻找一些更简洁的解决方案而不用这些构造函数来规划我的类).
我有一个Ninject的问题,我以前没有见过,也不知道如何修复.我正在使用MVC NuGet包进行MVC 5.
在我的NinjectWebCommon.cs中,有以下方法:
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
Run Code Online (Sandbox Code Playgroud)
但是,当尝试执行最后一行时,它会失败,抱怨"序列不包含任何元素".我以前使用Unity进行DI但已更改为Ninject.
有任何想法吗?
先谢谢,M
我对IoC框架很陌生,所以请原谅术语.
所以我所拥有的是一个带有Nininject MVC参考的MVC项目.我的项目中有其他类libarys,例如Domain层,我希望能够在那里使用Ninject框架,但我的所有绑定都在MVC项目NinjectWebCommon.cs的App_Start文件夹下:
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IHardwareService>().To<WindowsHardwareService>();
kernel.Bind<IStatusApi>().To<StatusApiController>();
}
Run Code Online (Sandbox Code Playgroud)
目前在我的类库中我使用构造函数注入,但有时我不得不对依赖项进行硬编码:
var service = new WindowsHardwareService();
Run Code Online (Sandbox Code Playgroud)
当我希望能够做到以下几点时:
IKernel kernel = new StandardKernel(.....);
var context = kernel.Get<IHardwareService>();
Run Code Online (Sandbox Code Playgroud)
我没有做以下因为我没有任何模块?我读过的所有文档主要针对的是常规Ninject库而不是MVC版本.
我需要做什么,以及如何将常规Ninject库与MVC版本一起使用?
更新
这是我尝试过的:
这样做的目的是使每个项目都可以加载模块并获得当前注入的接口.
App_Start/NinjectWebCommon.cs(在MVC项目中)
private static void RegisterServices(IKernel kernel)
{
var modules = new IoCModules();
var newKernal = modules.GetKernel();
kernel = newKernal;
}
Run Code Online (Sandbox Code Playgroud)
IoCModules.cs(在Project.Ioc项目中)
public class IoCModules
{
public IKernel GetKernel()
{
var modules = new CoreModule();
return modules.Kernel;
}
}
Run Code Online (Sandbox Code Playgroud)
CoreModule.cs(在Project.IoC.Modules项目中)< - …
我正在使用Ninject和与nuget一起安装的MVC3扩展.我的内核设置代码位于App_Start/NinjectMVC3.cs文件中.一切都在控制器中运行良好,但我无法弄清楚如何(正确)绑定Global.asax.cs MvcApplication代码中的接口.
我最终使用了一个hack(创建一个返回bootstrap.kernel的公共NinjectMVC3.GetKernel()方法).但是,这将被弃用,必须有一个正确的方法来做到这一点,我没有看到.
这是我的代码:
public class LogFilterAttribute : ActionFilterAttribute
{
private IReportingService ReportingService { get; set; }
public LogFilterAttribute( IReportingService reportingService )
{
this.ReportingService = reportingService;
}
...
}
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterGlobalFilters( GlobalFilterCollection filters )
{
filters.Add( new HandleErrorAttribute() );
filters.Add( new LogFilterAttribute() );
}
...
protected void Application_Start()
{
...
RegisterGlobalFilters( GlobalFilters.Filters );
// NOTE hack:
var kernel = NinjectMVC3.GetKernel();
var logger = kernel.Get<ILogger>();
var bw = new BackgroundWork(logger);
Application["BackgroundWork"] = bw; …Run Code Online (Sandbox Code Playgroud) 这个问题与Ninject没有特别的关系.这更像是一个通用的编码问题,但是我在这里发布它是为了防止在Ninject中处理问题的方法比我想做的更好.
我想知道是否可以从Global.asax中的实例全局访问Ninject标准内核.
这是代码:
public class MvcApplication : NinjectHttpApplication
{
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
// MVC global registration, routing and filtering code goes here...
}
protected override IKernel CreateKernel()
{
return Container;
}
private static IKernel Container
{
get
{
IKernel kernel = new StandardKernel();
kernel.Load(new ServiceModule(), new RepositoryModule());
return kernel;
}
}
}
Run Code Online (Sandbox Code Playgroud)
如果我有一些类,例如,没有与控制器接口的外观类,我想开始一个依赖链,我的理解是我应该使用:
_className = kernel.Get<IClassName>();
Run Code Online (Sandbox Code Playgroud)
但是,我知道这样做的唯一方法是创建Ninject标准内核的新实例,但是如果我理解正确,那么创建Ninject内核的新实例并不是一个好主意,因为这基本上是创建的第二个内核.
那么,是否有可能从我的应用程序中的任何位置访问在Application Start中在Global.asax中实例化的现有内核,或者是否有更好的方法来完成此操作?
问候,
弗雷德城堡