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

  • Home
  • SEARCH
  • 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 8538539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:08:28+00:00 2026-06-11T11:08:28+00:00

Hopefully I can explain this to where someone might understand it enough to help

  • 0

Hopefully I can explain this to where someone might understand it enough to help 🙂

Anyways, I want to take a property from an injected type and use that in another injection. So imagine you have MVC model state on a controller that you want to inject into a service the controller uses.

public class MyController
{
    public MyController(IService service)
    {
        ....
    }
}

public class MyService : IService
{
    public MyService(IModelStateWrapper modelState)
    {
        ....
    }
}

How can I accomplish basically this:

public class MyController
{
    public MyController(IService service)
    {
        service.ModelState = new ModelStateWrapper(ModelState);
    }
}

Using an injection with Autofac or whatever DI container.

  • 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-11T11:08:30+00:00Added an answer on June 11, 2026 at 11:08 am

    You can do this, but you have to unravel a couple of design problems before it’s possible.

    First, it appears that your IService implementation requires you pass the IModelStateWrapper in during construction only to be overwritten later during the creation of the controller. You have to make it so the IService implementation only has it as a property, not as a constructor requirement.

    Second, you have to make sure it’s OK that the service.ModelState setting happens just after construction of the controller. If there is other constructor logic that assumes the service.ModelState is set, then you have something that can’t really be done via DI.

    If you do that unraveling, Autofac will let you do some pretty cool stuff. When you register your controller type, register a lambda instead of just a type.

    var builder = new ContainerBuilder();
    builder.RegisterType<MyService>().As<IMyService>();
    builder.Register(
        c=>
        {
          var service = c.Resolve<IMyService>();
          var controller = new MyController(service);
          service.ModelState = new ModelStateWrapper(controller.ModelState);
          return controller;
        }).As<IController>();
    var container = builder.Build();
    

    Note the circular logic thing is handled in the lambda of the registration. Now when you resolve a controller…

    var controller = container.Resolve<IController>();
    

    …that logic will run and you’ll get the effect you’re looking for.

    Again, I’ll voice some concern over the circular reference stuff going on here. If there’s a way to remove that circular dependency between the controller and the service, you’d be in a better spot to let DI work for you.

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

Sidebar

Related Questions

Sorry for the unhelpful title, but hopefully I can explain this well enough. Lets
Hopefully I can explain this well enough, but please let me know if more
Maybe I'm a little behind the eight ball. Hopefully someone can explain this. I
This is hard to explain but hopefully I can articulate my problem well enough.
I'm seeing a strange behaviour, hopefully someone out there can explain this. I have
Hopefully I can explain my issue clearly enough for others to understand, here we
Hopefully someone can help here, it's incredibly frustrating! I have a couple of iOS
Hopefully someone can help me. I'm attempting to do the following: If a user
Quick question hopefully someone can help out here. I'm trying to copy and paste
This is a weird one, but hopefully someone can give me an idea here.

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.