Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7950597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:19:25+00:00 2026-06-04T02:19:25+00:00

I am new to IoC and am using Autofac in my current project. I

  • 0

I am new to IoC and am using Autofac in my current project.

I have the following 2 classes:

public class UserService : IUserService
{
    private readonly IUserRepository _repo;
    private readonly IMailService _mailService;
    public UserService(IUserRepository repo, IMailService mailService)
    {
        _repo = repo;
        _mailService = mailService;
    }
}

public class MailService : IMailService
{
    private readonly IMailRepository _repo;
    private readonly IUserService _userService;
    public MailService(IMailRepository repo, IUserService userService)
    {
        _repo = repo;
        _userService = userService;
    }
}

Initially, my UserService class didn’t require an instance of the MailService class, but now it does, and it’s since introducing this into the UserService Constructor that this circular dependency error has arisen, and being a newbie, I’m not sure how to resolve this.

This is how my classes are currently registered in Autofac:

var builder = new ContainerBuilder();

// controllers                      
builder.RegisterControllers(Assembly.GetAssembly(typeof(UsersController)));

// register other classes
builder.RegisterType<UserRepository>().As<IUserRepository>();
builder.RegisterType<MailRepository>().As<IMailRepository>();
builder.RegisterType<UserService>().As<IUserService>();
builder.RegisterType<MailService>().As<IMailService>();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T02:19:26+00:00Added an answer on June 4, 2026 at 2:19 am

    If a UserService needs an IMailService and a MailService needs an IUserService you have a dependency loop. I see a couple of options:

    1. Does your UserService need an IMailService right away? Could you pass one in when it needs to send a message?

    2. Can either query the resolver on-demand – that is, don’t pass IUserService to MailService in the constructor but rather code MailService to resolve IUserService when it needs it?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're currently following the DI model using Autofac as an IoC container. We've recently
I am brand new to IoC and thus have been following the examples provided
I have couple of class libraries in my project and all are using Ninject
I'm new to IoC and I'm trying to get started using StructureMap, but it
Possible Duplicate: Using IoC for Unit Testing I think I do have a Problem
The project am working implements IOC/DI using castle windsor. There are many modules in
I am using AutoFac as a IoC to inject the ObjectContext inside the Controllers
I'm using Autofac to implement IoC in my solution, But I am doubtful whether
When using dependency injection (DI) and inversion of control (IoC) objects will typically have
This question is about IoC in general, but I'm using Autofac, so an Autofac

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.