双向依赖关系检测到。

5

好的,我遇到了一个堆栈错误。它卡在这个文件上:

using System.Web;
using NHibernate;
using Nichols.Web.App_Start;

namespace Nichols.Web.DependencyResolution
{
    public class StructureMapScopeModule : IHttpModule
    {
        public void Dispose()
        {
            StructuremapMvc.StructureMapDependencyScope.Dispose();
        }

        public void Init(HttpApplication context)
        {
            context.BeginRequest += (sender, e) =>
                {
                    InitializeNestedContainerForRequest();

                    var session = GetCurrentSession();
                    session.BeginTransaction();
                };

            context.EndRequest += (sender, e) =>
                {
                    var session = GetCurrentSession();

                    if (context.Context.Error == null
                        && IsOk(context.Response.StatusCode))
                    {
                        session.Transaction.Commit();
                    }
                    else
                    {
                        session.Transaction.Rollback();
                    }

                    DisposeNestedContainerForRequest();
                };
        }

        private ISession GetCurrentSession()
        {
            var container = StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer;
            return container.GetInstance<ISession>();
        }

        private void InitializeNestedContainerForRequest()
        {
            StructuremapMvc.StructureMapDependencyScope.CreateNestedContainer();
        }

        private void DisposeNestedContainerForRequest()
        {
            StructuremapMvc.StructureMapDependencyScope.DisposeNestedContainer();
        }

        private bool IsOk(int statusCode)
        {
            return statusCode >= 200 && statusCode < 300;
        }
    }
}

我已经到处查找了,在可能的解决方案中提到了版本3.0.5。感谢您的帮助!

我收到的错误在这里:

Server Error in '/' Application.

Bi-directional dependency relationship detected!
Check the StructureMap stacktrace below:
1.) Lambda: default
2.) Instance of NHibernate.ISession
3.) Container.GetInstance(NHibernate.ISession)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: StructureMap.Building.StructureMapBuildException: Bi-directional dependency relationship detected!
Check the StructureMap stacktrace below:
1.) Lambda: default
2.) Instance of NHibernate.ISession
3.) Container.GetInstance(NHibernate.ISession)


Source Error: 


Line 43:         {
Line 44:             var container = StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer;
Line 45:             return container.GetInstance<ISession>();
Line 46:         }
Line 47: 

Source File: C:\projects\NicholsFarms_demo_newCropYear\Nichols\src\Nichols.Web\DependencyResolution\StructureMapScopeModule.cs    Line: 45 

Stack Trace: 


[StructureMapBuildException: Bi-directional dependency relationship detected!
Check the StructureMap stacktrace below:
1.) Lambda: default
2.) Instance of NHibernate.ISession
3.) Container.GetInstance(NHibernate.ISession)
]
   lambda_method(Closure , IBuildSession , IContext ) +692
   StructureMap.Building.BuildPlan.Build(IBuildSession session, IContext context) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Building\BuildPlan.cs:151
   StructureMap.Pipeline.LifecycleObjectCache.Get(Type pluginType, Instance instance, IBuildSession session) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Pipeline\LifecycleObjectCache.cs:71
   StructureMap.SessionCache.GetObject(Type pluginType, Instance instance, ILifecycle lifecycle) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\SessionCache.cs:88
   StructureMap.SessionCache.GetDefault(Type pluginType, IPipelineGraph pipelineGraph) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\SessionCache.cs:66
   StructureMap.Container.GetInstance(Type pluginType) in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:335
   StructureMap.Container.GetInstance() in c:\BuildAgent\work\996e173a8ceccdca\src\StructureMap\Container.cs:200
   Nichols.Web.DependencyResolution.StructureMapScopeModule.GetCurrentSession() in C:\projects\NicholsFarms_demo_newCropYear\Nichols\src\Nichols.Web\DependencyResolution\StructureMapScopeModule.cs:45
   Nichols.Web.DependencyResolution.StructureMapScopeModule.<Init>b__1_0(Object sender, EventArgs e) in C:\projects\NicholsFarms_demo_newCropYear\Nichols\src\Nichols.Web\DependencyResolution\StructureMapScopeModule.cs:20
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +142
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +92

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.6.1055.0

你目前使用的是哪个版本的 StructureMap?可能是在使用 _instances.Contains(instance) 部分的内部 Get 方法中出现了误报错误,该问题已自 3.0.5 版本得到修复。 - Tetsuya Yamamoto
我已经升级到最新的StructureMap稳定版本,但仍然遇到相同的错误,停留在相同的位置: `code private ISession GetCurrentSession() { var container = StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer; return container.GetInstance(); } code`有什么想法可以更改代码以避免这种情况吗? - John Edward Law
@JohnEdwardLaw 我也遇到了同样的问题,你解决了吗? - Flappy
可悲的是,最终只能通过审查控制面板下安装的所有组件,并从一个系统匹配到新系统。在确保一切完全匹配后,我才能重新编译项目并使其正常工作。遗憾的是,Visual Studio没有更好地确保组件协调的工作。 - John Edward Law
3个回答

4
为避免双向依赖关系异常,您可以按以下方式实现延迟加载。
    private readonly Lazy<IService> _service;

    public MainService(Lazy<IService> service)
   {
     _service=service;
   }

现在您可以调用服务中的方法,如下:

_service.Value.Method()

我认为这应该解决你的问题。

0

我们遇到了同样的问题,将StructureMap NuGet包更新到3.1.5版本后,问题得到了解决。


0

在我的情况下,有一个依赖项的实现未从API中加载。

安装在API上的Newtownsoft版本低于实现版本,因此当尝试加载它时会失败。错误信息真的很误导人。我不得不在实现中注释字段和构造函数,之后它显示了真正的错误。然后只需更新Newtosoft就可以解决所有问题。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接