0

I have a simple mvc application that has 3 layer

  1. Ui => has refrence to Common And Services
  2. Common
  3. Services => has refrence to Common

I define my Service Contracts in Common layer and implement it in services layer

//Common layer
public interface IPersonService
{
  void Foo();
}
//Services layer
public classPersonService:IPersonService
{
  void Foo()
  {
    //To DO
  }
}

In my Global.asax I write this code for initial Structuremap container

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

Now,in my controller when I want get instance from IPersonService like this

var personService= ObjectFactory.GetInstance<IPersonService>();

I get this error

No default Instance is registered and cannot be automatically determined for type '*.IPersonService'

Is there any idea for resolve this problem?

1 Answer 1

1

You can specify other assemblies to scan using the AssembliesFromApplicationBaseDirectory() function, like so:

scan.AssembliesFromApplicationBaseDirectory(a => a.FullName.StartsWith("YourNamespace.Common"));
scan.AssembliesFromApplicationBaseDirectory(a => a.FullName.StartsWith("YourNamespace.Services"));

Seeing as you tagged this question as a StructureMap 3 question, I would advise against using the ObjectFactory as Jeremy Miller (the author of StructureMap) has made it clear that it will be removed in future version. In fact, you should get a warning that it will be removed in a future version.

Instead you should aim to configure your container like so:

IContainer container = new Container();
container.Configure(c => {
    c.IncludeRegistry<YourRegistry>();
});

DependencyResolver.SetResolver(new StructureMapDependencyResolver(container));

You can read more about StructureMap's registries here.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.