Jonathan
Jonathan

Reputation: 1755

StructureMap ASP.net MVC Not Injecting Interface

I have tried to get the structure map dependency to work correctly but it is not working correctly when putting an interface in the constructor versus putting the class name.

The following code works:

public class HomeController : Controller
{
    private readonly MyService _service;

    public HomeController(MyService service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        return View();
    }
}

public class MyService : IService
{
    public string GetName()
    {
        return "Hello";
    }
}

public interface IService
{
    string GetName();
}

But the following code does NOT work:

public class HomeController : Controller
{
    private readonly IService _service;

    public HomeController(IService service)
    {
        _service = service;
    }

    public ActionResult Index()
    {
        return View();
    }
}

public class MyService : IService
{
    public string GetName()
    {
        return "Hello";
    }
}

public interface IService
{
    string GetName();
}

Here is the logic from the DependencyResolution class:

  public static IContainer Initialize() {
        ObjectFactory.Initialize(x =>
                    {
                        x.Scan(scan =>
                                {
                                    scan.TheCallingAssembly();
                                    scan.WithDefaultConventions();
                                });
        //                x.For<IExample>().Use<Example>();
                    });
        return ObjectFactory.Container;
    }

I am using the StructureMap.MVC4 nuget package to setup the dependency injection. What am I doing wrong?

Upvotes: 0

Views: 1271

Answers (2)

Naveen
Naveen

Reputation: 539

In your calling assembly if you have only single implementation class representing a inteface you can use like below

x.Scan(scan =>
             {
               scan.TheCallingAssembly();
               scan.WithDefaultConventions();
               scan.SingleImplementationsOfInterface();
             });

without SingleImplementationsOfInterface() method structuremap can not identify the proper implementation class for the IService interface .

or

you can map like below

ObjectFactory.Initialize(x =>
        {
            x.Scan(scan =>
            {
                scan.TheCallingAssembly();
                scan.WithDefaultConventions();
            });
            x.For<IService>().Use<MyService>();
        });

Upvotes: 3

Ehsan
Ehsan

Reputation: 834

try this piece of code instead:

 public class MvcBootStrapper
        {
            public static void ConfigurationStructureMap()
            {
                ObjectFactory.Initialize(x =>
                {
                    x.AddRegistry<MyService>();
                });
            }
        }

and finally register you classes and interfaces like this:

 public class SampleRegistery : Registry
        {
            public SampleRegistery ()
            {
                ForRequestedType<IService>().TheDefaultIsConcreteType<MyService>();
            }
        }

see this article for more info.

Upvotes: 1

Related Questions