Skip to content

How to Resolve an Instance in Structuremap RegistrationConvention #18

Description

@SaMirzaei

I'm using Structuremap as IOC container.

In old version of Structremap I used ObjectFactory, now I have updated to Structremap.Mvc5 But I can't create an instance of 'ISettingService' in my BuildRegistration method.

My SettingConvention class is:
`public class SettingsConvention : IRegistrationConvention
{
static readonly MethodInfo BuildMethod = typeof(SettingsConvention).GetMethod(
"BuildRegistration",
BindingFlags.Static | BindingFlags.NonPublic);

static ISettings BuildRegistration<TSettings>() where TSettings : ISettings, new()
{
    // It's work in old version but now It's not working
    // How I should create an Instance Here...
    // var settingService = ObjectFactory.Resolve<ISettingService>();

    return settingService.LoadSetting<TSettings>();
}

public void ScanTypes(TypeSet types, StructureMap.Registry registry)
{
    // Only work on concrete types
    types.FindTypes(TypeClassification.Concretes | TypeClassification.Closed).ForEach(type =>
    {
        // Register against all the interfaces implemented
        // by this concrete class
        type.GetInterfaces().ForEach(@interface =>
        {
            if (type.CanBeCastTo(typeof(ISettings)) && typeof(ISettings).IsAssignableFrom(type))
            {
                var buildMethod = BuildMethod.MakeGenericMethod(type);

                registry.For(type).Use(buildMethod.Invoke(null, null));
            }
        });
    });
}

}`

How can I create an instance of 'ISettingService' instead of writing 'ObjectFactory.Resolve()' ?

Thanks everyone for taking the time to try and help explain.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions