Chào bạn Tìm kiếm | Bài viết mới | Thành viên | Đăng ký | Đăng nhập |
E-Groups Forum E.G 1.0 » Lập trình websites Asp.Net mô hình mvc » Thảo luận - chia sẻ » Ninject in ASP.NET MVC4 - support
Ninject in ASP.NET MVC4 - support  
Chủ đề trước · Chủ đề tiếp theo
egroups
28/10/2014 10:03:21 SA
Lơ tơ mơ

Cấp bậc: Thành viên mới

Tham gia: 30/1/2013
Bài viết: 93
Đến từ: TpHCM

Đánh giá: [0]
{Bình chọn}
Được Cảm ơn: 1 lần
So after much screwing around I finally got Ninject wired in and compiling in my MVC4 application. The problem I was running into is the IDependencyScope interface no longer exists from what I can tell and the System.Web.Http.Dependencies namespace was done away with.

So, my problem now is I have everything wired in and upon running the application I get:

 Sequence contains no elements

 [InvalidOperationException: Sequence contains no elements]
 System.Linq.Enumerable.Single(IEnumerable`1 source) +379
 Ninject.Web.Mvc.NinjectMvcHttpApplicationPlugin.Start() in c:\Projects\Ninject\ninject.web.mvc\mvc3\src\Ninject.Web.Mvc\NinjectMvcHttpApplicationPlugin.cs:53
 Ninject.Web.Common.Bootstrapper.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:31
 Ninject.Web.Common.Bootstrapper.Initialize(Func`1 createKernelCallback) in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\Bootstrapper.cs:53
 Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:81

Which I haven't been able to track down or even begin to fathom where it is coming from.

My standard Ninject methods inside the Global.asax.cs look as follows:

protected override IKernel CreateKernel()
 {
 var kernel = new StandardKernel();
 kernel.Load(Assembly.GetExecutingAssembly());
 kernel.Bind().To();

 GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
 return kernel;
 }

 protected override void OnApplicationStarted()
 {
 base.OnApplicationStarted();
 AreaRegistration.RegisterAllAreas();
 RegisterGlobalFilters(GlobalFilters.Filters);
 RegisterRoutes(RouteTable.Routes);
 BundleTable.Bundles.RegisterTemplateBundles();
 }

And my custom resolver:

public class NinjectDependencyResolver : IDependencyResolver

{
 private readonly IKernel _kernel;

 public NinjectDependencyResolver(IKernel kernel)
 {
 _kernel = kernel;
 }

 public object GetService(Type serviceType)
 {
 return _kernel.TryGet(serviceType);
 }

 public IEnumerable GetServices(Type serviceType)
 {
 try
 {
 return _kernel.GetAll(serviceType);
 }
 catch (Exception)
 {
 return new List();
 }
 }

 public void Dispose()
 {
 // When BeginScope returns 'this', the Dispose method must be a no-op.
 }
}

Any insight here would be greatly appreciated. I've spent far too much time already trying to get any DI framework wired into the latest MVC4 RC running on .NET 4.5 and have now just reached my tolerance level for things just not working at all..

Edit #1 A little further research digging around in github the ExtensionsForIEnumerableOfT.cs doesn't help much:

https://github.com/ninject/ninject/blob/master/src/Ninject/Infrastructure/Language/ExtensionsForIEnumerableOfT.cs

And possibly if I had wrote it myself I would begin to understand this but Bootstrapper.cs doesn't help too much either.

https://github.com/ninject/Ninject.Web.Common/blob/master/src/Ninject.Web.Common/Bootstrapper.cs

Hoping these details will make it easier for any of you who might have more experience with Ninject.

Edit #2 The error encountered is specifically in NinjectMvcHttpApplicationPlugin.cs:

The offending line is:

ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType().Single());

Which lives in the following method:

public void Start()

{
 ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType().Single());
 DependencyResolver.SetResolver(this.CreateDependencyResolver());
 RemoveDefaultAttributeFilterProvider();
}

The ModelValidatorProviders collection contains 2 elements: {System.Web.Mvc.DataErrorInfoModelValidatorProvider} {System.Web.Mvc.ClientDataTypeModelValidatorProvider}

And it's trying to remove a single instance of:

System.Web.Mvc.DataAnnotationsModelValidatorProvider

Which apparently isn't loaded up in the ModelValidationProviders.Providers collection. Any ideas from here?

Resolution to Above Exception And Onto The Next

To resolve the issue in the ModelValidatorProviders I had to manually add an object it was expecting. So now my CreateKernel method looks like:

protected override IKernel CreateKernel()
{
 var kernel = new StandardKernel();
 kernel.Load(Assembly.GetExecutingAssembly());

 kernel.Bind().To();
 kernel.Unbind();

 GlobalConfiguration.Configuration.ServiceResolver.SetResolver(new NinjectDependencyResolver(kernel));
 ModelValidatorProviders.Providers.Add(new DataAnnotationsModelValidatorProvider());
 FilterProviders.Providers.Add(new FilterAttributeFilterProvider());
 return kernel;
}

Now it runs and gets into the actual guts of Ninject but still has an issue, one that makes no sense yet again:

Exception Details: Ninject.ActivationException: Error activating IntPtr

No matching bindings are available, and the type is not self-bindable.
Activation path:
 3) Injection of dependency IntPtr into parameter method of constructor of type Func{IKernel}
 2) Injection of dependency Func{IKernel} into parameter lazyKernel of constructor of type HttpApplicationInitializationHttpModule
 1) Request for IHttpModule

Suggestions:
 1) Ensure that you have defined a binding for IntPtr.
 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel.
 3) Ensure you have not accidentally created more than one kernel.
 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name.
 5) If you are using automatic module loading, ensure the search path and filters are correct.

---------------------------------
Hỗ trợ lập trình asp .net mvc5 khóa 5 tháng đảm bảo sẽ code chuyên nghiệp. Liên hệ: 0166.795.666.5
{ Hãy nhập thông tin bình luận bài viết này }
 
egroups đã offline
 #1  
egroups
28/10/2014 10:04:05 SA
Lơ tơ mơ

Cấp bậc: Thành viên mới

Tham gia: 30/1/2013
Bài viết: 93
Đến từ: TpHCM

Đánh giá: [0]
{Bình chọn}
Được Cảm ơn: 1 lần
Ok after beating my head against the wall for far too long I figured out what was going on. The default project type for MVC4 running on .NET 4.5 had a reference to the original RC version of System.Web.Http instead of the updated version.

Namespaces were missing, objects didn't exist, life was not good.

Steps for resolution:

  1. Remove your reference to System.Web.Http in your MVC4 project
  2. Add Reference -> System.Web.Http
  3. Delete all work arounds you put in to get the old garbage version of System.Web.Http to work
  4. Reapply standard process to wire in Ninject.

    HOWEVER, the error of:

    Exception Details: Ninject.ActivationException: Error activating IntPtr No matching bindings are available, and the type is not self-bindable. Activation path: 3) Injection of dependency IntPtr into parameter method of constructor of type Func{IKernel} 2) Injection of dependency Func{IKernel} into parameter lazyKernel of constructor of type HttpApplicationInitializationHttpModule 1) Request for IHttpModule

    Suggestions: 1) Ensure that you have defined a binding for IntPtr. 2) If the binding was defined in a module, ensure that the module has been loaded into the kernel. 3) Ensure you have not accidentally created more than one kernel. 4) If you are using constructor arguments, ensure that the parameter name matches the constructors parameter name. 5) If you are using automatic module loading, ensure the search path and filters are correct.

Update This was solved by updating MVC from MVC4 Beta to MVC4 RC.


---------------------------------
Hỗ trợ lập trình asp .net mvc5 khóa 5 tháng đảm bảo sẽ code chuyên nghiệp. Liên hệ: 0166.795.666.5

{ Hãy nhập thông tin bình luận bài viết này }
 
egroups đã offline
 #2  
Thành viên đang xem
Có 1 người dùng đang xem (1 khách)
E-Groups Forum E.G 1.0 » Lập trình websites Asp.Net mô hình mvc » Thảo luận - chia sẻ » Ninject in ASP.NET MVC4 - support
Di chuyển nhanh:  
Có bài mới Có bài mới Không có bài mới Không có bài mới
Có bài mới (Đã khóa) Có bài mới (Đã khóa) Không có bài mới (Đã khóa) Không có bài mới (Đã khóa)
Thông báo Thông báo Chú ý Chú ý
Đã chuyển Đã chuyển Bình chọn Bình chọn
Bạn không thể gửi bài viết mới
Bạn không thể trả lời bài viết
Bạn không thể xóa bài viết của bạn
Bạn có thể chỉnh sửa bài viết của bạn
Bạn không thể tạo bình chọn
Bạn có thể bình chọn
Giờ hiện tại: 12:48 CH - GMT + 7
Powered by E-GROUPS Việt Nam phiên bản Mã Nguồn Mở Forum EG 1.0

Share full soure code web, Thiết kế web, Asp .Net MVC, Asp .Net MVC4, Asp .Net MVC5, Mã nguồn mở forum EG 1.0, Thương Mại Điện Tử, Diễn đàn SEO
, thiet ke web gia re, thiet ke web chuyen nghiep