我很久以前建造了一个IoC容器,几个月前看了这个节目http://www.dnrtv.com/default.aspx?showNum=126,并做了一个使用它的小样本,并且工作正常.
现在我在我正在开始的项目中使用unity作为依赖注入容器,并且还在之前的小型MVC网站中尝试了NInject.
但我现在问自己,这些容器比我从节目中创建的容器有什么好处,我只能看到它从配置文件中加载,我可以在我的网站上做,是否还有其他我缺少的东西?我只想要一个人告诉我你必须使用这些容器因为1,2,3个原因比你的好.
.net dependency-injection ninject ioc-container unity-container
我已经看到了许多使用ASP.NET MVC配置Ninject的不同方法,但是随着MVC框架的每个版本的发布,实现似乎都有所改变.我正在尝试将RavenDB会话注入我的存储库.这就是我所拥有的几乎可以工作的东西.
public class MvcApplication : NinjectHttpApplication
{
...
protected override void OnApplicationStarted()
{
base.OnApplicationStarted();
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}
protected override IKernel CreateKernel()
{
return new StandardKernel(new MyNinjectModule());
}
public static IDocumentSession CurrentSession
{
get { return (IDocumentSession)HttpContext.Current.Items[RavenSessionKey]; }
}
...
}
public class MyNinjectModule : NinjectModule
{
public override void Load()
{
Bind<IUserRepository>().To<UserRepository>();
Bind<IDocumentSession>().ToConstant(MvcApplication.CurrentSession);
}
}
Run Code Online (Sandbox Code Playgroud)
当它尝试解析IDocumentSession时,我收到以下错误.
Error activating IDocumentSession using binding from IDocumentSession to constant value
Provider returned null.
Activation path:
3) Injection of dependency IDocumentSession into parameter …Run Code Online (Sandbox Code Playgroud) 我正在使用带有WCF扩展的Ninject 2.2.在我的大多数服务中,存储库都会快速实例化/释放.但是,我的一项服务执行长时间运行(2-3分钟).如果我观察w3wp进程,我可以看到使用SQL建立的TCP/IP连接,我可以在SQL上运行sp_who2并查看连接.
完成这些操作后,连接将保持打开状态5-10分钟.
当我多次运行该操作时,我没有看到生成新的连接但是我将运行该应用程序的多个实例,并且我发现之前的性能下降,同时发送了那些长时间运行的操作并且在几分钟后自行修复.
这个垃圾收集可以成为问题的一部分吗?如何解决?
这是我的Ninject绑定:
Bind<ISomeRepository>().To<SomeRepository>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
这是我的WCF绑定:
<binding name="xxx" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
bypassProxyOnLocal="false" transactionFlow="false"
hostNameComparisonMode="StrongWildcard" maxBufferPoolSize="524288"
maxReceivedMessageSize="99999999" messageEncoding="Text"
textEncoding="utf-8" useDefaultWebProxy="false" allowCookies="false">
<readerQuotas maxDepth="90" maxStringContentLength="99999"
maxArrayLength="99999999" maxBytesPerRead="4096"
maxNameTableCharCount="16384" />
<reliableSession ordered="true" inactivityTimeout="00:10:00"
enabled="true" />
<security mode="..."/>
</binding>
<service name="SomeService"
behaviorConfiguration="abcd">
<endpoint name="BasicEndPoint" address="http://localhost/SomeService.svc"
binding="wsHttpBinding" bindingConfiguration="xxx"
contract="ISomeJobService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
Run Code Online (Sandbox Code Playgroud)
我意识到一个选择是将服务使用的存储库数量减少到一个,但我真的在寻找一个临时解决方案.
ObjectContext实例已在InRequestScope中处理!
我在网上尝试了几个小时试图解决问题.
ObjectContext实例已被释放,不能再用于需要连接的操作.
我发现了几个文章和帖子中包含了同样的问题这个,这个,这个和这个
我尝试了所有方法,但总是发生错误.
上下文
public class BindSolutionContext : DbContext
{
public DbSet<Project> Projects { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<ProjectImage> ProjectImages { get; set; }
public BindSolutionContext()
: base("name=Data")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<BindSolutionContext>());
}
}
Run Code Online (Sandbox Code Playgroud)
Ninject
kernel.Bind<BindSolutionContext>().ToSelf().InRequestScope();
kernel.Bind<IProjectRepository>().To<ProjectRepository>().InRequestScope();
kernel.Bind<IUserRepository>().To<UserRepository>().InRequestScope();
kernel.Bind<IRoleRepository>().To<RoleRepository>().InRequestScope();
kernel.Bind<IAddressRepository>().To<AddressRepository>().InRequestScope();
kernel.Bind<IProjectImageRepository>().To<ProjectImageRepository>().InRequestScope();
Run Code Online (Sandbox Code Playgroud)
知识库
public class ProjectRepository : IProjectRepository
{
private readonly …Run Code Online (Sandbox Code Playgroud) dependency-injection ninject custom-membershipprovider asp.net-mvc-3 dbcontext
阅读Lucene,似乎建议在所有请求中使用相同的IndexSearcher实例.
如果我有一个使用ninject注入的搜索类
public interface IPatientSearch
{
void DoSearch(ref SearchDTO _search);
//...
}
Run Code Online (Sandbox Code Playgroud)
是否存在使用InSingletonScope绑定它的任何问题,这将确保在所有请求中共享相同的实例?
Bind<IPatientSearch>().To<PatientSearch>().InSingletonScope();
Run Code Online (Sandbox Code Playgroud)
我是否遗漏了使用这种方法的任何明显缺陷?
我今天开始了一个新项目并且去了nuget以获得ninject.
我下载了Ninject MVC3插件,该插件应该下载我需要的所有内容.
然后,我从一个不同的项目中获取了一些旧代码,并注意到InRequestScope似乎已经消失了.
它被拿走了还是我错过了什么?
编辑
我想我在"Ninject.Web.Common"下找到了它
我在安装Ninject.Web(v3)的情况下尝试启动WebForms应用程序时出现以下错误.
"静态容器已经有了与之关联的内核!"
我在Global.asax中实现Ninject继承自NinjectHttpApplication并覆盖CreateKernel(),我的模块采用以下形式
public class NinjectWebModule: NinjectModule
{
public override void Load()
{
Bind<IBlah>().To<Blah>();
}
}
Run Code Online (Sandbox Code Playgroud)
我基本上遵循了包含的设置建议如何在asp.net Web窗体上实现Ninject或DI?
痛苦的黄色屏幕说
[NotSupportedException: The static container already has a kernel associated with it!]
Ninject.Web.KernelContainer.set_Kernel(IKernel value) in c:\Projects\Ninject\ninject.web\src\Ninject.Web\KernelContainer.cs:38
Ninject.Web.NinjectWebHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web\src\Ninject.Web\NinjectWebHttpApplicationPlugin.cs:62
Ninject.Web.Common.Bootstrapper.<Initialize>b__0(INinjectHttpApplicationPlugin c) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map(IEnumerable`1 series, Action`1 action) in c:\Projects\Ninject\ninject\src\Ninject\Infrastructure\Language\ExtensionsForIEnumerableOfT.cs:32
Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:52
Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:80
[HttpException (0x80004005): The static container already has a kernel associated with it!]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9859725
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState …Run Code Online (Sandbox Code Playgroud) 我一直试图让(Ninject 3+)的Ninject.Extensions.Conventions工作,没有运气.我把它归结为一个找到的样本控制台应用程序,我甚至无法做到这一点.这就是我所拥有的:
class Program
{
static void Main(string[] args)
{
var kernel = new StandardKernel();
kernel.Bind(x => x
.FromThisAssembly()
.SelectAllClasses()
.BindAllInterfaces());
var output = kernel.Get<IConsoleOutput>();
output.HelloWorld();
var service = kernel.Get<Service>();
service.OutputToConsole();
Console.ReadLine();
}
public interface IConsoleOutput
{
void HelloWorld();
}
public class ConsoleOutput : IConsoleOutput
{
public void HelloWorld()
{
Console.WriteLine("Hello world!");
}
}
public class Service
{
private readonly IConsoleOutput _output;
public Service(IConsoleOutput output)
{
_output = output;
}
public void OutputToConsole()
{
_output.HelloWorld();
}
}
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了各种组合的 FromAssembliesMatching …
为控制器编写集成测试可能很有用.如果控制器的依赖关系由Ninject管理,则不清楚如何实例化控制器.
请注意,它不是单元测试 - 我想测试一个包含所有依赖项的完整控制器.我正在使用MVC 4,NInject 3和MbUnit.
我想将IServiceProvider绑定到Ninject IKernel实现.有什么意义
Bind<IKernel>().ToConstant(this).InTransientScope();
Run Code Online (Sandbox Code Playgroud)
从Ninject 来源绑定?
这是Ninject如何将IKernel绑定到KernelBase实现的方式.我无法理解这一点.ToConstant绑定类型设置范围隐式为Singleton.而具有ToConstant绑定类型的TransientScope对我没有任何意义.
ninject ×10
c# ×4
.net ×2
asp.net-mvc ×2
dbcontext ×1
kernel ×1
lucene.net ×1
ninject.web ×1
nuget ×1
ravendb ×1
singleton ×1
wcf ×1