c# - 检测到双向依赖关系

标签 c# asp.net-mvc structuremap structuremap4

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

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

最佳答案

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

    private readonly Lazy<IService> _service;

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

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

_service.Value.Method()

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

关于c# - 检测到双向依赖关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44449987/

相关文章:

asp.net-mvc-3 - StructureMap HttpContextScoped 是必要的吗?

.net - StructureMap GetAllInstances 在需要多个实例时返回一个实例

c# - 如何将文件下载到远程机器,就像我在自己的机器上一样或类似?

c# - 在 C# 中拆分字符串

c# - WPF WF4.5 Rehosted Designer 内存问题

c# - .Net MVC 中的 URL 重写

c# - 如何在 C# 中获取 HTTP Post 数据 - FiddlerCore?

jquery - 内联网开发人员制作面向公众的网站的安全考虑因素?

javascript - AngularJS 的 404 图片加载问题

开放泛型类型上的 StructureMap GetAllInstances