Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 温莎城堡酒店_C#_.net_Nhibernate_Castle Windsor_Windsor Nhfacility - Fatal编程技术网

C# 温莎城堡酒店

C# 温莎城堡酒店,c#,.net,nhibernate,castle-windsor,windsor-nhfacility,C#,.net,Nhibernate,Castle Windsor,Windsor Nhfacility,我正在迁移我的mvc web应用程序,以使用Nhibernate工具进行管理 Nhibernate会议 我遇到了这个奇怪的问题 当我通过以下方式在web.config/httpmodules下注册SessionWebModule时: <add name="NHibernateSessionWebModule" type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule, Castle.Facil

我正在迁移我的mvc web应用程序,以使用Nhibernate工具进行管理 Nhibernate会议

我遇到了这个奇怪的问题

当我通过以下方式在web.config/httpmodules下注册SessionWebModule时:

<add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />

系统无法延迟加载对象。我没有例外

如果我不将其添加到web.config,延迟加载可以正常工作。 就注册温莎而言,这一切似乎都还不错 集装箱和注册Nhibernate设施开始运行。亚硝酸铵 该设施能够提供会话管理器和Windsor容器 能够解析对象

当然,若并没有SessionWebModule,Nhibernate设施几乎是无用的 对于我来说,在这种情况下,它无法正确管理 会议。我看到datareader是allready开放类型异常 不好

我被困住了,急需解决这个问题,我会的 感谢您的帮助

我将在下面介绍我的配置和代码的一些部分 提供更多信息

Web.config:

<castle>
        <facilities>
            <facility id="nhibernatefacility" isWeb="false"
type="Castle.Facilities.NHibernateIntegration.NHibernateFacility,
Castle.Facilities.NHibernateIntegration">
                <factory id="nhibernate.factory">
                    <settings>
                        <item
key="connection.provider">NHibernate.Connection.DriverConnectionProvider</
item>
                        <item
key="connection.driver_class">NHibernate.Driver.SqlClientDriver</item>
                        <item key="connection.connection_string">Data
Source=zzz;Database=xxx;Trusted_Connection=True;</item>
                        <item
key="dialect">NHibernate.Dialect.MsSql2005Dialect</item>
                        <item
key="proxyfactory.factory_class">NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu</item>
                      </settings>
                    <assemblies>
                        <assembly>AppWeb.Domain</assembly>
                    </assemblies>
                </factory>
            </facility>
        </facilities>
    </castle>

<system.webServer>
        <validation validateIntegratedModeConfiguration="false" />
        <modules runAllManagedModulesForAllRequests="true">
              <add name="NHibernateSessionWebModule"
type="Castle.Facilities.NHibernateIntegration.Components.Web.SessionWebModule,
Castle.Facilities.NHibernateIntegration" />
        </modules>
</sytem.webServer>

NHibernate.Connection.DriverConnectionProvider
NHibernate.Driver.SqlClientDriver
资料
来源=zzz;数据库=xxx;Trusted_Connection=True;
NHibernate.dialogue.mssql2005dialogue
NHibernate.ByteCode.LinFu.ProxyFactoryFactory,
NHibernate.ByteCode.LinFu
AppWeb.Domain
Global.asax

public class MvcApplication : System.Web.HttpApplication,
IContainerAccessor
    {
        private static IWindsorContainer container;

        public IWindsorContainer Container
        {
            get { return container; }
        }

        void Application_Error(Object sender, EventArgs e)
        {
            Logger.Error(Server.GetLastError());
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            #region Ignores
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.IgnoreRoute("admin/{*url}");
            #endregion

            routes.MapRoute(
                "Default",
                "{controller}/{action}/{id}",
                new { controller = "Home", action = "Main", id = "" },
                new { controller = @"[^\.]*" }
            );
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            log4net.Config.XmlConfigurator.Configure();
            SetupWindsor();
        }

        private void SetupWindsor()
        {
            container = new WindsorContainer(new XmlInterpreter());

            container.Register(
                        AllTypes
                            .FromAssemblyContaining<UserRepository>()
                            .Where(t => t.Name.EndsWith("Repository",
StringComparison.Ordinal))
                            .WithService
                            .FirstInterfaceOnType()
                            .Configure(r =>
r.LifeStyle.PerWebRequest),

AllTypes.FromAssembly(Assembly.GetExecutingAssembly())
                            .BasedOn<IController>()
                            .Configure(c =>
c.LifeStyle.PerWebRequest),

                    );
                    //.AddFacility<TransactionFacility>();

            ControllerBuilder.Current.SetControllerFactory(new
WindsorControllerFactory(container));
        }
    }
公共类mvcapapplication:System.Web.HttpApplication,
IContainer存取器
{
私有静态IWindsorContainer;
公共集装箱
{
获取{返回容器;}
}
无效应用程序错误(对象发送方,事件参数e)
{
Logger.Error(Server.GetLastError());
}
公共静态无效注册表项(路由收集路由)
{
#区域忽略
routes.IgnoreRoute(“{resource}.axd/{*pathInfo}”);
IgnoreRoute(“admin/{*url}”);
#端区
routes.MapRoute(
“默认”,
“{controller}/{action}/{id}”,
新建{controller=“Home”,action=“Main”,id=”“},
新的{controller=@“[^\.]*”}
);
}
受保护的无效应用程序\u Start()
{
注册地址(RouteTable.Routes);
log4net.Config.XmlConfigurator.Configure();
温莎();
}
温莎私人酒店()
{
容器=新的WindsorContainer(新的XML解释器());
集装箱。登记(
各种类型
.FromAssemblyContaining()
.Where(t=>t.Name.EndsWith(“存储库”,
字符串比较(序数)
.带服务
.FirstInterfaceOnType()
.Configure(r=>
r、 生活方式。PerWebRequest),
AllTypes.FromAssembly(Assembly.getExecutionGassembly())
.BasedOn()
.Configure(c=>
c、 生活方式。PerWebRequest),
);
//.AddFacility();
ControllerBuilder.Current.SetControllerFactory(新建)
温莎控制器工厂(集装箱);
}
}
延迟加载异常:

[LazyInitializationException:正在初始化[AppWeb.Domain.City#31135]- 无法初始化代理-没有会话。] NHibernate.Proxy.AbstractLazyInitializer.Initialize()+138 NHibernate.Proxy.AbstractLazyInitializer.GetImplementation()+37 NHibernate.ByteCode.LinFu.LazyInitializer.Intercept(调用信息 信息)+72 CityProxy.get_Name()+143 ASP.views\u tour\u filters\u ascx.\uuuuu Render\uuuuu control1(HtmlTextWriter\uuuu w, c:\wwwroot\AppWeb\AppWeb.Web\Views\Tour中的控件参数(容器) \过滤器。ascx:14 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter, i收集儿童)+256 System.Web.UI.Control.RenderChildren(HtmlTextWriter)+19 System.Web.UI.Control.Render(HtmlTextWriter)+10 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter, 控制适配器+27 System.Web.UI.Control.RenderControl(HtmlTextWriter, 控制适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter)+25 System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter, i收集儿童)+134 System.Web.UI.Control.RenderChildren(HtmlTextWriter)+19 System.Web.UI.Page.Render(HtmlTextWriter)+29 System.Web.Mvc.ViewPage.Render(HtmlTextWriter)+59 System.Web.UI.Control.RenderControlInternal(HtmlTextWriter, 控制适配器+27 System.Web.UI.Control.RenderControl(HtmlTextWriter, 控制适配器)+99 System.Web.UI.Control.RenderControl(HtmlTextWriter)+25 System.Web.UI.Page.ProcessRequestMain(布尔值 IncludeStages前同步点,布尔值IncludeStages后同步点)
+1266这是设计的。您必须保持会话处于打开状态

如果您正在进行web,我建议您在请求开始时打开会话,在请求结束时关闭会话


获取本文中的示例:

如果您使用的是MVC,我将使用操作过滤器关闭我的nhibernate会话

public class NHibernateSessionAttribute : ActionFilterAttribute
{
    public NHibernateSessionAttribute()
    {
        Order = 100;
    }


    public override void OnResultExecuted(ResultExecutedContext filterContext)
    {
        if (filterContext.IsChildAction) return;

         SessionSource.EndContextSession();
    }
}
SessionSource是我管理创建会话的地方。。。而SessionSource.EndContextSession()方法实际上调用:

    public static void EndContextSession()
    {
        var session = CurrentSessionContext.Unbind(Factory);
        if (session != null && session.IsOpen)
        {
            try
            {
                if (session.Transaction != null && session.Transaction.IsActive)
                {
                    // an unhandled exception has occurred and no db commit should be made
                    session.Transaction.Rollback();
                }
            }
            finally
            {
                session.Close();
                session.Dispose();
            }
        }
    }

我相信您必须将
isWeb
属性设置为true:

<facility id="nhibernatefacility" isWeb="true" type="Castle.Facilities.NHibernateIntegration.NHibernateFacility, Castle.Facilities.NHibernateIntegration">

Im使用“Nhibernate Facility”,它应该处理会话的所有管理…也许,但在您的代码中,您使用“using”.Nhiber显式地处理会话