【问题标题】:Bi-directional dependency relationship detected检测到双向依赖关系
【发布时间】:2017-06-09 05:35:31
【问题描述】:

好的,我收到堆栈错误。它被抓到的文件在这里

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 版本?使用自 3.0.5 以来已修复的 _instances.Contains(instance) 部分时,这可能是内部 Get 方法中的误报错误。
  • 我已经升级到 StructureMap 的最新稳定版本,但仍然出现同样的错误,仍然停在同一个位置:code private ISession GetCurrentSession() { var container = StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer; return container.GetInstance&lt;ISession&gt;(); } code 关于如何更改代码以避免这种情况的任何想法?
  • @JohnEdwardLaw 同样的问题,你解决了吗?
  • 遗憾的是,它最终归结为检查控制面板下所有已安装的组件,并将一个系统与新系统进行匹配。在一切都完全匹配之后,我能够重新编译项目并且它工作了。遗憾的是 Visual Studio 在确保组件协调方面没有做得更好。

标签: c# asp.net-mvc structuremap structuremap4


【解决方案1】:

为避免双向依赖关系异常,可以如下实现延迟加载。

    private readonly Lazy<IService> _service;

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

您现在可以将服务中的方法调用为

_service.Value.Method()

我认为这应该可以解决您的问题

【讨论】:

    【解决方案2】:

    我们面临同样的问题,将 StructureMap nuget 包更新到版本 3.1.5 解决了我们的问题。

    【讨论】:

      【解决方案3】:

      在我的情况下,存在未从 API 加载的实现之一的依赖关系。

      安装在 API 上的 Newtownsoft 版本低于实现时的版本,因此当它尝试加载该 API 时,它失败了。该错误确实具有误导性。我必须在实现上评论字段和构造函数,然后它显示哪个是真正的错误。然后只需更新 Newtosoft 即可解决所有问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-11-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-01
        • 1970-01-01
        • 2015-12-28
        • 2023-03-16
        相关资源
        最近更新 更多