5

OK I am getting a stack error. The file it is getting caught on is here

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;
        }
    }
}

I have looked everywhere and it references version 3.0.5 as a possible resolution. Any assistance is appreciated!

The error I am receiving is here:

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
Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
John Edward Law
  • 121
  • 1
  • 9
  • Which `StructureMap` version you currently use? Possibly this is false-positive error in internal `Get` method when using `_instances.Contains(instance)` section which has fixed since 3.0.5. – Tetsuya Yamamoto Jun 09 '17 at 08:40
  • I have upgraded to the latest stable version of StructureMap and I still get the same error, still stopping at the same location: `code private ISession GetCurrentSession() { var container = StructuremapMvc.StructureMapDependencyScope.CurrentNestedContainer; return container.GetInstance(); } code` Any ideas of how I can change the code to avoid this? – John Edward Law Jun 12 '17 at 08:33
  • @JohnEdwardLaw Same issue here, did you get it fixed? – Flappy Aug 20 '17 at 17:47
  • Sadly it ended up boiling down to reviewing all of the installed components under the Control Panel and matching from one system to the new. After everything then matched exactly I was able to recompile the project and it worked. Sad that Visual Studio doesn't do a better job of ensuring component reconciliation. – John Edward Law Aug 20 '17 at 22:14

3 Answers3

4

To avoid bi-direction dependency relationship exception,you can implement lazy loading as below.

    private readonly Lazy<IService> _service;

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

You can now call methods in the service as

_service.Value.Method()

I think this should solve your problem

0

We are facing the same issue, Updating the StructureMap nuget package to version 3.1.5 has resolved our issue.

0

In my case there was a dependency of one of the implementations not being loaded from the API.

The version Newtownsoft that was installed on the API was lower than on the implementation, so when it tried to load that, it failed. The error was really missleading. I had to comment the fields and constructor on the implementations and after that it showed which was the real error. Then just updating Newtosoft fixed everything.

g_brahimaj
  • 197
  • 7
  • 21