Search code examples
dependency-injectioninversion-of-controlautofacioc-containerumbraco7

Autofac Working In ConsoleApplcation but not working from Website


I have a VS solution with a few different projects:

  • Project.ConsoleApplication
  • Project.Common
  • Project.Website

I have managed to set up Autofac to work from the Console Application, the dependencies are being injected correctly and everything works as expected.

The Console Application has very simple logic in it, all it does is add a new record to the database and then display all the records in the database.

The website is trying to do exactly the same thing but when I call the GetAll method in the website it returns no records. No Errors are thrown.

All the Interfaces and Concrete classes are in the Project.Common project which both the Console Application and Website are referencing.

I can't work out why it works in the Console Application and not in the Website Project - Do I need to configure Autofac in a different way for each project?

My data-layer is built using entity framework following this approach:
http://techbrij.com/autofac-ioc-container-asp-net-mvc-di

The website is an Umbraco CMS website that has its own database (not sure if that makes a difference)

This is how my code currently looks:

class Program
{
    static void Main(string[] args)
    {
       var container = ConfigureContainer();
       var application = container.Resolve<ApplicationLogic>();
       application.Run();
    }

    private static IContainer ConfigureContainer()
    {
        var builder = new ContainerBuilder();

        builder.RegisterModule(new RepositoryModule());
        builder.RegisterModule(new ServiceModule());
        builder.RegisterModule(new EFModule());

        builder.RegisterType<ApplicationLogic>();
        var container = builder.Build();
        DependencyResolver.SetResolver(new AutofacDependencyResolver(container));

        return container;
    }

    private class ApplicationLogic
    {
        private readonly ICampaignService campaignService;

        public ApplicationLogic(ICampaignService campaignService)
        {
            this.campaignService = campaignService;
        }

        public void Run()
        {
            Console.WriteLine("enter a name to create a new campaign: ");
            string campaignName = Console.ReadLine();
            Console.WriteLine("Attempting to create new campaign, please wait...");

                var newCampaign = new Campaign();

                newCampaign.CampaignName = campaignName;
                newCampaign.IsActive = true;

                campaignService.Create(newCampaign);

                Console.WriteLine(string.Format("The Campaign'{0}' has been created successfully.", campaignName));


                var currentCampaigns = campaignService.GetAll();

                foreach (var campaign in currentCampaigns)
                {
                    Console.WriteLine(string.Format("The Campaign'{0}' Found.", campaign.CampaignName));
                    Console.WriteLine(System.Environment.NewLine);
                }


                Console.ReadLine();
        }
    }
}

These are the modules that are being loaded in BOTH the console app and the Website.

public class EFModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterModule(new RepositoryModule());

        builder.RegisterType(typeof(RewardsDbContext)).As(typeof(DbContext)).InstancePerLifetimeScope();
        builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerLifetimeScope();
        //builder.RegisterType(typeof(UnitOfWork)).As(typeof(IUnitOfWork)).InstancePerRequest();

    }
}

public class RepositoryModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterAssemblyTypes(Assembly.Load("Project.Common"))
               .Where(t => t.Name.EndsWith("Repository"))
               .AsImplementedInterfaces()
              .InstancePerLifetimeScope();
    }
}

public class ServiceModule : Autofac.Module
{
    protected override void Load(ContainerBuilder builder)
    {

        builder.RegisterAssemblyTypes(Assembly.Load("Project.Common"))
                  .Where(t => t.Name.EndsWith("Service"))
                  .AsImplementedInterfaces()
                  .InstancePerLifetimeScope();
    }
}

So anyone out there got any ideas?


Solution

  • It was a connection string issue, silly me.