Tag: ninject

将依赖项注入DelegatingHandler

我是dependency injection的新手,但对Ninject和Ninject.Extensions.Logging感到很满意,只要我需要它就可以[Inject]我的ILogger 。 然而,一些DelegatingHandler正在破坏所有的乐趣。 public class HttpsHandler : DelegatingHandler { [Inject] public ILogger Logger { get; set; } protected override Task SendAsync( HttpRequestMessage request, CancellationToken cancellationToken) { if (!string.Equals(request.RequestUri.Scheme, “https”, StringComparison.OrdinalIgnoreCase)) { Logger.Info(String.Format(“{0}: is using HTTP”, request.RemoteAddress()); return Task.Factory.StartNew( () => new HttpResponseMessage(HttpStatusCode.BadRequest) { Content = new StringContent(“HTTPS Required”) }); } return base.SendAsync(request, cancellationToken); } […]

如何在Ninject中使用Provider

我有以下代码 public class Something { [Inject] public Configuration config {get;set;} //singleton [Inject] public Provider windowsProvider { get; set; } //NOT singleton public void Search(string text) { WindowHandler handler = windowsProvider.Create(xxxxxx); //use the new handler that was created } } 但似乎提供商采用了IContext,我放了xxxxxx。 不应该使用IContext从我引导并从内核创建Something.cs时使用。 Provider上的no参数Create方法在哪里??? (我来自Guice的土地观点,它将被编码如上)。 所以问题是我该如何正确地做到这一点? 谢谢,迪恩

使用绑定到常量并使用Ninject绑定到范围中的类型

创建单个对象到接口的绑定的哪种方式更可取,何时以及为什么: Kernel.Bind().ToConstant(new Foo()); 要么 Kernel.Bind().To(typeof(Foo)).InSingletonScope(); 或者,如果两种方式都不正确并且更好地避免,那么应该使用什么呢?

基于约定的构造函数字符串参数与Ninject的绑定

我在我的项目中使用Ninject作为IoC容器。 我有以下课程: public class SomeRepository:ISomeRepository { public SomeRepository(string someDatabaseConnectionString) { // some code here.. } } 在我的应用程序设置文件中,我有一个名为“someDatabase”的连接字符串。 默认情况下,应该添加以下配置,以便将此连接字符串注入构造函数: kernel.Bind() .To() .WithConstructorArgument(“someDatabaseConnectionString”, connString); 但我想实现这种字符串的传统绑定。 名称以“ConnectionString”结尾的字符串类型的所有构造函数参数的值应从应用程序的connectionStrings配置部分获取并自动注入。 我也想为appSettings部分实现类似的约定。 这种方法在Mark Seeman的“Primitive Dependencies”一文(“基元的约定”一节)中有更详细的描述。 在示例中使用Castle Windsor容器。 是否可以使用Ninject实现此类约定,最好的方法是什么? 我已经尝试过ninject.extensions.conventions但似乎它没有这样的function,是吗?

WebAPI 2.1中的Ninject错误 – 确保控制器具有无参数的公共构造函数

我在WebAPI项目中安装了以下软件包及其依赖项: Ninject.Web.WebApi Ninject.Web.WebApi.OwinHost 我这纯粹是作为web-api项目运行的。 没有MVC。 当我运行我的应用程序并向AccountController的Register操作发送POST时,我收到以下错误: { “message”:”An error has occurred.”, “exceptionMessage”:”An error occurred when trying to create a controller of type ‘AccountController’. Make sure that the controller has a parameterless public constructor.”, “exceptionType”:”System.InvalidOperationException”, “stackTrace”:” at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__0.MoveNext()”, “innerException”:{ “message”:”An error […]

无法加载文件或程序集“System.Web.Mvc,Version = 3.0.0.0”或其依赖项之一

我在包管理器控制台中使用以下命令在MVC项目中添加Ninject: Install-Package Ninject -version 3.0.1.10 Install-Package Ninject.Web.Common -version 3.0.0.7 Install-Package Ninject.MVC3 -Version 3.0.0.6 当我运行应用程序时,我得到这样的错误: 无法加载文件或程序集’System.Web.Mvc,Version = 3.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35’或其依赖项之一。 定位的程序集的清单定义与程序集引用不匹配。 (HRESULTexception:0x80131040)

Ninject和DataContext处理

我正在使用Ninject从内核中检索我的DataContext,我想知道Ninject是否自动处理DataContext,或者他如何处理dispose()行为。 根据自己的经验,我知道处理datacontext非常重要,每当你创建DataContext的直接对象时(如:new DataContext()),你应该使用using()块。 我的问题是:当我从内核检索我的DataContext时,我是否仍然必须使用using()块? 或者Ninject是否为我解决了这个问题?

Ninject绑定,接口接口

我想按照以下方式做点什么: kernel.Bind().To().InSingletonScope(); kernel.Bind().To().InSingletonScope(); kernel.Bind().To(); kernel.Bind().To(); 所以我可以这样做: public class Boot { public Boot(IBootTask[] bootTasks) { foreach(var task in bootTasks){task.Execute();} } } 但我似乎无法将接口绑定到接口,任何人都知道解决这个问题的方法吗?

Log ninject已解决的依赖项应用程序启动

我们已经开始使用Ninject版本2作为我们的Io​​C容器以及通过命名约定解析的扩展。 我们还使用log4net进行日志记录。 我想要的是让Ninject记录它找到的所有依赖项以及解决它们的方法,最好是在应用程序启动时。 我找到了日志记录扩展,但找不到有关如何使用它来获取此文档或示例。 编辑: 因为这里请求的是使用log4net在启动时记录默认绑定的类 public class DefaultBindingGeneratorWithLogging:IBindingGenerator {private readonly IKernel kernel; /// /// Initializes a new instance of the class. /// /// The kernel. public DefaultBindingGeneratorWithLogging(IKernel kernel) { this.kernel = kernel; } /// /// Creates the bindings for a type. /// /// The type for which the bindings are created. /// The binding […]

Ninject基于参数类型的条件绑定

我正在使用工厂返回数据库: Bind() .ToFactory(); public interface IDataSenderFactory { IDataSender CreateDataSender(Connection connection); } 我有两种不同的datasender实现(WCF和远程处理),它们采用不同的类型: public abstract class Connection { public string ServerName { get; set; } } public class WcfConnection : Connection { // specificProperties etc. } public class RemotingConnection : Connection { // specificProperties etc. } 我试图使用Ninject根据从参数传递的Connection类型绑定这些特定类型的数据集。 我尝试了下面的失败: Bind() .To() .When(a => a.Parameters.Single(b => b.Name == […]