0

FYI: I am using SpecFlow.Autofac nuget package.

Following GetContainer method exists within a parent class library project.

    [ScenarioDependencies]
    public static ContainerBuilder GetContainerWithDependencies()
    {
        var containerBuilder = new ContainerBuilder();

        containerBuilder.RegisterType<PosMgmtScenarioCtx>().As<IScenarioContext>().InstancePerLifetimeScope();

        return containerBuilder;
    }

At a later point, within a child project (which references the parent), I attempt replacing the interface implementation with a new class but it does not work.

    [Given(@"...Step...")]
    public void GivenGenerateEmpWithXRefCode(some params)
    {
        using 
        (
            var scope = _lifetimeScope.BeginLifetimeScope(
                builder =>  {
                    // THIS DOES NOT WORK.
                    builder.RegisterType<EmployeeScenarioCtx>().As<IScenarioContext>();
                }
            )
        )
        {                
            _createUpdatePositionStepsLogic.PostPatchTheObject(service, "POST", "Employee", url, expectedStatusCode,
                version);
        }
    }

Need to replace IScenarioContext with EmployeeScenarioCtx instead of PosMgmtScenarioCtx.

Any help would be appreciated, thanks!

Kanwar
  • 53
  • 7
  • 1
    Your scope is not used anywhere to resolve the service. The registration you are doing works pretty well but you will still receive the old registration when you are not using the child scope that you are creating to actually resolve `IScenarioContext`. – alsami May 27 '21 at 06:15
  • Could you please guide how do I use the child scope to resolve IScenarioContext now? ```_createUpdatePositionStepsLogic.PostPatchTheObject(service, "POST", "Employee", url, expectedStatusCode, version);``` ends up calling the ```WithPayload(IEntityMappings entityMappings, IRestRequest restRequest, IHelpers helpers, IFormatHelpers formatHelpers, IScenarioContext scenarioContext``` My assumption was that the child scope should automatically be used to resolve IScenarioContext now. – Kanwar May 27 '21 at 21:34

2 Answers2

1

You are supposed to use the scope which you create to Resove the dependency.

var createUpdatePositionStepsLogic = scope.Resolve<...>();

The original dependencies have all already been resolved so you need to re resolve them using your new scope in order to use the new registration.

misha130
  • 5,457
  • 2
  • 29
  • 51
  • Thanks for your response. So, ```_createUpdatePositionStepsLogic.PostPatchTheObject(service, "POST", "Employee", url, expectedStatusCode, version); ``` is calling ```WithPayload(IEntityMappings entityMappings, IRestRequest restRequest, IHelpers helpers, IFormatHelpers formatHelpers, IScenarioContext scenarioContext, ...)``` My expectation was that IScenarioContext should now be resolved with EmployeeScenarioCtx automatically. – Kanwar May 27 '21 at 21:31
0

So, my question should have been how to override dependencies within a constructor using Autofac to which there are many answers on SO already. I don't have reputation to mark my question as a duplicate but please do so if necessary.

Following answers helped me:

  1. https://stackoverflow.com/a/30836235/1524213
  2. https://stackoverflow.com/a/17054237/1524213

This is how the eventual code looks now:

using(var scope = _lifetimeScope.BeginLifetimeScope(
                builder =>
                {
                    builder.RegisterType<WithPayload>().WithParameter(
                        (p, c) => p.ParameterType == typeof(IScenarioContext),
                        (p, c) => c.Resolve<EmployeeScenarioCtx>()).As<IWithPayload>();
                }
            )
        )
        {
            switch (version)
            {
                case PosMgmtStepsVersion.V1:
                    var withPayload = scope.Resolve<IWithPayload>();
                    withPayload.GivenPOST_PATCH_EndPoint_WithStatusCodeCheck("POST", "Employee", UniqueIdentifier,
                        url,
                        expectedStatusCode);
                    break;
                default:
                    throw new ArgumentOutOfRangeException(nameof(version), version, null);
            }
Kanwar
  • 53
  • 7