我有一个旧的ASP.NET Web应用程序,它使用.asmx Web服务。作为重写的一部分,我需要使用DI将新编写的模块注入Web服务中。
我已经在项目中添加了以下ninject依赖项,如this resource中所述,并且该应用程序正常运行了一段时间。
Ninject模块:
public static class NinjectWebCommon
{
private static readonly Bootstrapper bootstrapper = new Bootstrapper();
/// <summary>
/// Starts the application
/// </summary>
public static void Start()
{
DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
bootstrapper.Initialize(CreateKernel);
}
/// <summary>
/// Stops the application.
/// </summary>
public static void Stop()
{
bootstrapper.ShutDown();
}
/// <summary>
/// Creates the kernel that will manage your application.
/// </summary>
/// <returns>The created kernel.</returns>
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);
return kernel;
}
catch
{
kernel.Dispose();
throw;
}
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind<IClientContextFactory>().To<ClientContextFactory>();
kernel.Bind<IContextStore>().To<ClientContextStore>();
kernel.Bind<ITemplateFiller, HashedTemplateFiller>();
// Map custom interfaces to business implementations
kernel.Bind<IUserView>().To<UserView>();
kernel.Bind<IBranchView>().To<BranchView>();
kernel.Bind<IEntity<UserDetailsDto>>().To<UserRepository>();
kernel.Bind<IEntity<Branch>>().To<BranchRepository>();
kernel.Bind<IUserConfiguration>().To<UserConfigurations.UserConfiguration>();
kernel.Bind<IMapper>().ToMethod(AutoMapper).InSingletonScope();
kernel.Bind<IMember>().To<Admin.Service.Member.Member> ();
// in-memory DB
kernel.Bind<IConnection>().To<SqlliteConnection>();
kernel.Bind<IInMemoryContextFactory>().To<InMemoryContextFactory>();
}
}
然后是我的网络服务:
public class GridService: WebServiceBase
{
[Inject]
public IClientContextFactory contextFactory { get; set; }
[Inject]
public IMapper mapper { get; set; }
[Inject]
public IUserView userView { get; set; }
}
当我导航到使用Ninject注入的页面时,也许在某些依赖项更新或某些原因(我不知道)之后,Ninject引发了以下错误:
> Object reference not set to an instance of an object.
at Ninject.Planning.Bindings.BindingConfiguration.GetProvider(IContext context)
at Ninject.Planning.Bindings.Binding.GetProvider(IContext context)
at Ninject.Activation.Context.ResolveInternal(Object scope)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.ResolveInternal(Object scope)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target)
at Ninject.Activation.Providers.StandardProvider.<>c__DisplayClass4.<Create>b__2(ITarget target)
at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
at Ninject.Activation.Providers.StandardProvider.Create(IContext context)
at Ninject.Activation.Context.ResolveInternal(Object scope)
at Ninject.Activation.Context.Resolve()
at Ninject.KernelBase.<>c__DisplayClass15.<Resolve>b__f(IBinding binding)
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
at System.Linq.Enumerable.SingleOrDefault[TSource](IEnumerable`1 source)
at Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent)
at Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent)
at Ninject.Activation.Strategies.PropertyInjectionStrategy.GetValue(IContext context, ITarget target, IEnumerable`1 allPropertyValues)
at Ninject.Activation.Strategies.PropertyInjectionStrategy.Activate(IContext context, InstanceReference reference)
at Ninject.Activation.Pipeline.<>c__DisplayClass2.<Activate>b__0(IActivationStrategy s)
at Ninject.Infrastructure.Language.ExtensionsForIEnumerableOfT.Map[T](IEnumerable`1 series, Action`1 action)
at Ninject.Activation.Pipeline.Activate(IContext context, InstanceReference reference)
at Ninject.KernelBase.Inject(Object instance, IParameter[] parameters)
at Ninject.Web.KernelContainer.Inject(Object instance)
at Ninject.Web.NinjectHttpModule.OnPreRequestHandlerExecute(Object sender, EventArgs e)
at System.Web.HttpApplication.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
我不知道根本原因是什么。我尝试将Ninject升级到不支持Ninject.Web
的v3.3.4,并且我的Web服务不再可以从WebServiceBase
继承。否则,我的依赖关系将无法正确解决。
该问题的解决方案是什么?