मेरी एएसपीनेट वेबएपीआई परियोजना में सेवाओं, कोर और डेटा एक्सेस के लिए कई असेंबली शामिल हैं। परियोजना में मेरे डी कंटेनर के रूप में निनजेक्ट का उपयोग करने के प्रयास में, मैंने NuGet से Ninject.Web.Common पैकेज जोड़ा। फिर, मैं के रूप में IDependencyResolver लागू:निंजा 3 - क्या BeginBlock() asp.net वेबैपी में InRequestScope ओवरराइड करता है?
public class NinjectDependencyResolver : NinjectDependencyScope, IDependencyResolver
{
readonly IKernel kernel;
public NinjectDependencyResolver(IKernel kernel) : base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectDependencyScope(this.kernel.BeginBlock());
}
}
public class NinjectDependencyScope : IDependencyScope
{
IResolutionRoot resolver;
public NinjectDependencyScope(IResolutionRoot resolver)
{
this.resolver = resolver;
}
public object GetService(System.Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
var resolved = this.resolver.Get(serviceType);
return resolved;
}
public System.Collections.Generic.IEnumerable<object> GetServices(System.Type serviceType)
{
if (resolver == null)
throw new ObjectDisposedException("this", "This scope has been disposed");
return this.resolver.GetAll(serviceType);
}
public void Dispose()
{
IDisposable disposable = resolver as IDisposable;
if (disposable != null)
disposable.Dispose();
resolver = null;
}
}
यहाँ मेरी Ninject.Web.Common.cs है।
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();
kernel.Bind<Func<IKernel>>().ToMethod(ctx =>() => new Bootstrapper().Kernel);
kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;
}
/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
kernel.Bind(x =>
x.FromAssembliesMatching("WebApiTest.DataAccess.dll")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(config => config.InRequestScope()));
kernel.Bind(x =>
x.FromAssembliesMatching("WebApiTest.*.dll")
.SelectAllClasses()
.BindAllInterfaces()
.Configure(config => config.InTransientScope()));
}
}
मेरा प्रश्न NinjectDependencyResolver में कोड के बारे में है -> BeginScope() विधि: लौट नई NinjectDependencyScope (this.kernel.BeginBlock());
मैं अपने भंडार (WebApiTest.DataAccess.dll में कार्यान्वित) करना चाहता हूं, अनुरोध किया जाना चाहिए। मैं नाते कोहारी से post में आया था। मुझे एहसास है कि पोस्ट पुराना है लेकिन सक्रियण खंडों के बारे में विवरण मुझे आश्चर्यचकित करता है कि यह अभी भी वर्तमान कार्यान्वयन है।
सक्रियण ब्लॉक के माध्यम से, निनजे 2 में दायरे को संभालने का एक अंतिम तरीका है। ब्लॉक> बाध्यकारी पर घोषित दायरे को ओवरराइड करने का एक तरीका है, और इसके बजाय सक्रिय रूप से ब्लॉक के साथ सक्रिय उदाहरणों को जोड़ता है। ...
तो, मेरे भंडारों का वास्तविक दायरा क्या है?
इसके अलावा, मेरे लिए लग रहा है कि BeginBlock() का उपयोग वैकल्पिक है, लेकिन जब मैं इसे हटाने, नियंत्रक करने के लिए पहली कॉल सफल होता है लेकिन बाद में किसी भी कॉल अपवाद:
Ninject घटक ICache ऐसा कोई कर्नेल के घटक कंटेनर
क्यों घटक में घटक पंजीकृत किया गया है ??
मैं उन का उपयोग कर रहा हूं। आप का क्या तात्पर्य है? –
आप नहीं हैं। कार्यान्वयन को देखो। हमने उन्हें इस धागे से बाहर बनाया: https://github.com/ninject/Ninject.Web.WebApi/pull/1 –
इयान, धन्यवाद। तुरंत आपके उत्तर में लिंक नहीं मिला। क्या मेरी समझ सही है कि मेरे कार्यान्वयन के साथ, BeginBlock() सिंगलटन के दायरे को ओवरराइट करता है? –