Ocorre o seguinte erro no método RegisterServices da classe NinjectWebCommon, quando acesso o link de cadastro de produtos:
An exception of type 'System.TypeInitializationException' occurred in LojaWeb.dll but was not handled in user code
Additional information: O inicializador de tipo de 'LojaWeb.Infra.NHibernateHelper' acionou uma exceção.
Classe NinjectWebCommon:
[assembly: WebActivator.PreApplicationStartMethod(typeof(LojaWeb.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(LojaWeb.App_Start.NinjectWebCommon), "Stop")]
namespace LojaWeb.App_Start
{
using System;
using System.Web;
using Microsoft.Web.Infrastructure.DynamicModuleHelper;
using Ninject;
using Ninject.Web.Common;
using NHibernate;
using LojaWeb.Infra;
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);
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<ISession>().ToMethod(
x => NHibernateHelper.AbreSession()).InRequestScope();
}
}
}
Classe NHibernateHelper:
using NHibernate;
using NHibernate.Cfg;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
namespace LojaWeb.Infra
{
public class NHibernateHelper
{
private static ISessionFactory factory = RecuperaConfiguracao().BuildSessionFactory();
public static Configuration RecuperaConfiguracao()
{
Configuration cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(Assembly.GetExecutingAssembly());
return cfg;
}
public static ISession AbreSession()
{
return factory.OpenSession();
}
}
}