Autofac Not Intercepting Calls To Classes The Next CEO of Stack OverflowMEF part unable to import Autofac autogenerated factoryAutofac 2.6.1 AOP InterceptionIntercepting a WCF Service with AutofacAutofac interception configuration for genericsAutofac class interception doesn't work in a certain setupAutofac Interception with custom attributesHow do I dynamically choose a connection string based on Environment in .Net Core startup?Using Autofac Interface Interception with IAsyncInterceptorAutofac Interception Not Workingforce innodb engine with Pomelo.EntityFrameworkCore.MySql 2.1.4

How can the PCs determine if an item is a phylactery?

Do I need to write [sic] when including a quotation with a number less than 10 that isn't written out?

How to show a landlord what we have in savings?

Planeswalker Ability and Death Timing

Is it possible to make a 9x9 table fit within the default margins?

Which acid/base does a strong base/acid react when added to a buffer solution?

Could a dragon use its wings to swim?

Car headlights in a world without electricity

What happens if you break a law in another country outside of that country?

Why did early computer designers eschew integers?

Was the Stack Exchange "Happy April Fools" page fitting with the 90s code?

Traveling with my 5 year old daughter (as the father) without the mother from Germany to Mexico

Is it a bad idea to plug the other end of ESD strap to wall ground?

Is it "common practice in Fourier transform spectroscopy to multiply the measured interferogram by an apodizing function"? If so, why?

How can I prove that a state of equilibrium is unstable?

Avoiding the "not like other girls" trope?

Is a linearly independent set whose span is dense a Schauder basis?

Why can't we say "I have been having a dog"?

Does Germany produce more waste than the US?

Find the majority element, which appears more than half the time

Does the Idaho Potato Commission associate potato skins with healthy eating?

How should I connect my cat5 cable to connectors having an orange-green line?

Find a path from s to t using as few red nodes as possible

How to pronounce fünf in 45



Autofac Not Intercepting Calls To Classes



The Next CEO of Stack OverflowMEF part unable to import Autofac autogenerated factoryAutofac 2.6.1 AOP InterceptionIntercepting a WCF Service with AutofacAutofac interception configuration for genericsAutofac class interception doesn't work in a certain setupAutofac Interception with custom attributesHow do I dynamically choose a connection string based on Environment in .Net Core startup?Using Autofac Interface Interception with IAsyncInterceptorAutofac Interception Not Workingforce innodb engine with Pomelo.EntityFrameworkCore.MySql 2.1.4










0















I can not get autofac to intercept the calls to my classes.
I have modified the Program.cs file such that it includes Autofac.
I have modified the Startup.cs file such that it includes ConfigureContainer.
When the method is called it bypasses the Intercept method within the Loggable attribute. What is missing?



public class Program

public static void Main(string[] args)

var host = WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.ConfigureServices(services => services.AddAutofac())
.UseStartup<Startup>().Build();
host.Run();



public class Startup

public IConfiguration Configuration get; private set;

public Startup(IConfiguration configuration)

Configuration = configuration;


public void ConfigureContainer(ContainerBuilder builder)

var helper = new StartupHelper();
helper.Configure(builder, Configuration);


public void ConfigureServices(IServiceCollection services)

services.AddOData();
services.AddMvc()
.AddControllersAsServices();


public void Configure(IApplicationBuilder app, IHostingEnvironment env)

if (env.IsDevelopment())

app.UseDeveloperExceptionPage();


var name = "OData";
var prefix = "OData";
var builder = new ODataConventionModelBuilder();

builder.EntitySet<DistributorParent>("DistributorParents")
.EntityType
.HasKey(a => a.DistparKey);

app.UseMvc(routebuilder =>

routebuilder.Select()
.Expand()
.Filter()
.OrderBy()
.MaxTop(null)
.Count();
routebuilder.MapODataServiceRoute(name, prefix, builder.GetEdmModel());
);



public class StartupHelper

public ContainerBuilder Configure(ContainerBuilder builder, IConfiguration configuration = null)

builder.Register(a => configuration);
builder.Register(a => new Loggable());
builder.RegisterType<DistributorParentBusiness>()
.As<IBusiness<DistributorParent>>()
.EnableInterfaceInterceptors()
.InterceptedBy(typeof(Loggable));
return builder;




[Loggable]
public class DistributorParentBusiness : IBusiness<DistributorParent>

private IConfiguration Configuration;
private String Connection;

public DistributorParentBusiness(IConfiguration configuration)

Configuration = configuration;
Connection = Configuration.GetConnectionString("CorpdbConnection");


public virtual DistributorParent Get(Int32 key)

var context = new CorpdbContext(Connection);
var query = from a in context.DistributorParent
where a.DistparKey == key
select a;
return query.FirstOrDefault();


public virtual IQueryable<DistributorParent> Get()

var context = new CorpdbContext(Connection);
var query = from a in context.DistributorParent
select a;
return query;











share|improve this question




























    0















    I can not get autofac to intercept the calls to my classes.
    I have modified the Program.cs file such that it includes Autofac.
    I have modified the Startup.cs file such that it includes ConfigureContainer.
    When the method is called it bypasses the Intercept method within the Loggable attribute. What is missing?



    public class Program

    public static void Main(string[] args)

    var host = WebHost.CreateDefaultBuilder(args)
    .UseKestrel()
    .ConfigureServices(services => services.AddAutofac())
    .UseStartup<Startup>().Build();
    host.Run();



    public class Startup

    public IConfiguration Configuration get; private set;

    public Startup(IConfiguration configuration)

    Configuration = configuration;


    public void ConfigureContainer(ContainerBuilder builder)

    var helper = new StartupHelper();
    helper.Configure(builder, Configuration);


    public void ConfigureServices(IServiceCollection services)

    services.AddOData();
    services.AddMvc()
    .AddControllersAsServices();


    public void Configure(IApplicationBuilder app, IHostingEnvironment env)

    if (env.IsDevelopment())

    app.UseDeveloperExceptionPage();


    var name = "OData";
    var prefix = "OData";
    var builder = new ODataConventionModelBuilder();

    builder.EntitySet<DistributorParent>("DistributorParents")
    .EntityType
    .HasKey(a => a.DistparKey);

    app.UseMvc(routebuilder =>

    routebuilder.Select()
    .Expand()
    .Filter()
    .OrderBy()
    .MaxTop(null)
    .Count();
    routebuilder.MapODataServiceRoute(name, prefix, builder.GetEdmModel());
    );



    public class StartupHelper

    public ContainerBuilder Configure(ContainerBuilder builder, IConfiguration configuration = null)

    builder.Register(a => configuration);
    builder.Register(a => new Loggable());
    builder.RegisterType<DistributorParentBusiness>()
    .As<IBusiness<DistributorParent>>()
    .EnableInterfaceInterceptors()
    .InterceptedBy(typeof(Loggable));
    return builder;




    [Loggable]
    public class DistributorParentBusiness : IBusiness<DistributorParent>

    private IConfiguration Configuration;
    private String Connection;

    public DistributorParentBusiness(IConfiguration configuration)

    Configuration = configuration;
    Connection = Configuration.GetConnectionString("CorpdbConnection");


    public virtual DistributorParent Get(Int32 key)

    var context = new CorpdbContext(Connection);
    var query = from a in context.DistributorParent
    where a.DistparKey == key
    select a;
    return query.FirstOrDefault();


    public virtual IQueryable<DistributorParent> Get()

    var context = new CorpdbContext(Connection);
    var query = from a in context.DistributorParent
    select a;
    return query;











    share|improve this question


























      0












      0








      0








      I can not get autofac to intercept the calls to my classes.
      I have modified the Program.cs file such that it includes Autofac.
      I have modified the Startup.cs file such that it includes ConfigureContainer.
      When the method is called it bypasses the Intercept method within the Loggable attribute. What is missing?



      public class Program

      public static void Main(string[] args)

      var host = WebHost.CreateDefaultBuilder(args)
      .UseKestrel()
      .ConfigureServices(services => services.AddAutofac())
      .UseStartup<Startup>().Build();
      host.Run();



      public class Startup

      public IConfiguration Configuration get; private set;

      public Startup(IConfiguration configuration)

      Configuration = configuration;


      public void ConfigureContainer(ContainerBuilder builder)

      var helper = new StartupHelper();
      helper.Configure(builder, Configuration);


      public void ConfigureServices(IServiceCollection services)

      services.AddOData();
      services.AddMvc()
      .AddControllersAsServices();


      public void Configure(IApplicationBuilder app, IHostingEnvironment env)

      if (env.IsDevelopment())

      app.UseDeveloperExceptionPage();


      var name = "OData";
      var prefix = "OData";
      var builder = new ODataConventionModelBuilder();

      builder.EntitySet<DistributorParent>("DistributorParents")
      .EntityType
      .HasKey(a => a.DistparKey);

      app.UseMvc(routebuilder =>

      routebuilder.Select()
      .Expand()
      .Filter()
      .OrderBy()
      .MaxTop(null)
      .Count();
      routebuilder.MapODataServiceRoute(name, prefix, builder.GetEdmModel());
      );



      public class StartupHelper

      public ContainerBuilder Configure(ContainerBuilder builder, IConfiguration configuration = null)

      builder.Register(a => configuration);
      builder.Register(a => new Loggable());
      builder.RegisterType<DistributorParentBusiness>()
      .As<IBusiness<DistributorParent>>()
      .EnableInterfaceInterceptors()
      .InterceptedBy(typeof(Loggable));
      return builder;




      [Loggable]
      public class DistributorParentBusiness : IBusiness<DistributorParent>

      private IConfiguration Configuration;
      private String Connection;

      public DistributorParentBusiness(IConfiguration configuration)

      Configuration = configuration;
      Connection = Configuration.GetConnectionString("CorpdbConnection");


      public virtual DistributorParent Get(Int32 key)

      var context = new CorpdbContext(Connection);
      var query = from a in context.DistributorParent
      where a.DistparKey == key
      select a;
      return query.FirstOrDefault();


      public virtual IQueryable<DistributorParent> Get()

      var context = new CorpdbContext(Connection);
      var query = from a in context.DistributorParent
      select a;
      return query;











      share|improve this question
















      I can not get autofac to intercept the calls to my classes.
      I have modified the Program.cs file such that it includes Autofac.
      I have modified the Startup.cs file such that it includes ConfigureContainer.
      When the method is called it bypasses the Intercept method within the Loggable attribute. What is missing?



      public class Program

      public static void Main(string[] args)

      var host = WebHost.CreateDefaultBuilder(args)
      .UseKestrel()
      .ConfigureServices(services => services.AddAutofac())
      .UseStartup<Startup>().Build();
      host.Run();



      public class Startup

      public IConfiguration Configuration get; private set;

      public Startup(IConfiguration configuration)

      Configuration = configuration;


      public void ConfigureContainer(ContainerBuilder builder)

      var helper = new StartupHelper();
      helper.Configure(builder, Configuration);


      public void ConfigureServices(IServiceCollection services)

      services.AddOData();
      services.AddMvc()
      .AddControllersAsServices();


      public void Configure(IApplicationBuilder app, IHostingEnvironment env)

      if (env.IsDevelopment())

      app.UseDeveloperExceptionPage();


      var name = "OData";
      var prefix = "OData";
      var builder = new ODataConventionModelBuilder();

      builder.EntitySet<DistributorParent>("DistributorParents")
      .EntityType
      .HasKey(a => a.DistparKey);

      app.UseMvc(routebuilder =>

      routebuilder.Select()
      .Expand()
      .Filter()
      .OrderBy()
      .MaxTop(null)
      .Count();
      routebuilder.MapODataServiceRoute(name, prefix, builder.GetEdmModel());
      );



      public class StartupHelper

      public ContainerBuilder Configure(ContainerBuilder builder, IConfiguration configuration = null)

      builder.Register(a => configuration);
      builder.Register(a => new Loggable());
      builder.RegisterType<DistributorParentBusiness>()
      .As<IBusiness<DistributorParent>>()
      .EnableInterfaceInterceptors()
      .InterceptedBy(typeof(Loggable));
      return builder;




      [Loggable]
      public class DistributorParentBusiness : IBusiness<DistributorParent>

      private IConfiguration Configuration;
      private String Connection;

      public DistributorParentBusiness(IConfiguration configuration)

      Configuration = configuration;
      Connection = Configuration.GetConnectionString("CorpdbConnection");


      public virtual DistributorParent Get(Int32 key)

      var context = new CorpdbContext(Connection);
      var query = from a in context.DistributorParent
      where a.DistparKey == key
      select a;
      return query.FirstOrDefault();


      public virtual IQueryable<DistributorParent> Get()

      var context = new CorpdbContext(Connection);
      var query = from a in context.DistributorParent
      select a;
      return query;








      asp.net-core autofac






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 8 at 15:44









      Travis Illig

      16.2k14471




      16.2k14471










      asked Mar 7 at 19:52









      user1931270user1931270

      245




      245






















          2 Answers
          2






          active

          oldest

          votes


















          0














          There's a lot to unpack here and not 100% of the information required to do it. I'll give you some things to look at and one of them should help.



          You're using interface interceptors, so only interface methods will be intercepted. I don't know what the IBusiness<T> interface looks like (part of the missing information) so I don't know which of the methods in DistributorParentBusiness is part of the interface. If you call a method that isn't in the interface, it won't get intercepted.



          Loggable looks like an attribute but needs to implement IInterceptor. I don't know what Loggable looks like, but it must implement IInterceptor if you're using it to intercept. It does strike me as odd that it's an attribute at all; it doesn't seem to be doing anything as it's applied in attribute form.



          The Autofac docs have examples of setting up interface injection. If you use the InterceptedBy(typeof(T)) then you don't use attributes. If you want to use attributes, it needs to be [Intercept(typeof(Logger))] and, if you use the attribute, you don't use InterceptedBy(typeof(T)). It's either/or, and the interceptor is never the attribute itself.



          Controllers are services but I don't know where IBusiness<DistributorParent> is getting used. Again, missing info. If it's being injected into a controller it should work; I think it should even work if you don't add controllers-as-services.



          That route builder call where you count all the routes is weird. No, this has nothing to do with the question, but... it's counting them and not using the value? What's... uh... what's going on there?



          What I would try is creating a unit-test level repro for yourself. That is, create a tiny repro just in a unit test that you can step into. Make sure you're wiring up the interface interception correctly first. Make a super simple, basically empty IBusiness<DistributorParent> implementation; create a container that has the interface interception wired up on that; resolve IBusiness<DistributorParent> manually; set a breakpoint in the interceptor; call a method you expect to be intercepted and see if the breakpoint is hit. Or create an interceptor that always returns a fixed known value and use that in your test if breakpoints aren't getting hit - sometimes settings like "Just My Code" and so on can end up causing certain things to be skipped.



          Point being, if none of the above things get you unwound, create a real minimum repro in a unit test form and make sure it works. Widen it up a little if it does - maybe you used some other interceptor in the repro and it works; try the actual Loggable interceptor. Does it still work? If not, maybe the problem's the interceptor. If so, keep widening scope. Eventually you'll either get the whole thing working or you'll find the spot that isn't working.



          If you do get the whole thing working in a minimal repro, then it's possible the issue is something you don't realize. For example, is the code in the repro exactly the same as the code in the real system? What's different? Work up to making the repro code close to the real code and see when it stops working.



          The minimum repro is really going to be your key here, though, I think.



          Recommendation for future questions - when you create the code for the question, try to make it the simple, tiny repro that I described above for troubleshooting. Enough to show the problem (and possibly be compiled by someone who wants to try it out) but no more.



          • Include all the class and interface definitions in play. Loggable and IBusiness<T>, for example.

          • Remove stuff that isn't needed. The route configuration in the app.UseMvc() call isn't relevant, for example.

          • Try to simplify indirection and complexity. The code from the StartupHelper could be inlined into the ConfigureContainer method to make this easier to read through and understand for folks answering the question.





          share|improve this answer
































            0














            The issue was very silly. I was manually creating the class and not using di. Therefore the methods weren't getting intercepted. Also, the original setup was trying to intercept the api controller and the methods weren't virtual. Little config changes to inject IBusiness and voila, it worked...






            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function ()
              StackExchange.using("externalEditor", function ()
              StackExchange.using("snippets", function ()
              StackExchange.snippets.init();
              );
              );
              , "code-snippets");

              StackExchange.ready(function()
              var channelOptions =
              tags: "".split(" "),
              id: "1"
              ;
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function()
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled)
              StackExchange.using("snippets", function()
              createEditor();
              );

              else
              createEditor();

              );

              function createEditor()
              StackExchange.prepareEditor(
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader:
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              ,
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              );



              );













              draft saved

              draft discarded


















              StackExchange.ready(
              function ()
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051823%2fautofac-not-intercepting-calls-to-classes%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              0














              There's a lot to unpack here and not 100% of the information required to do it. I'll give you some things to look at and one of them should help.



              You're using interface interceptors, so only interface methods will be intercepted. I don't know what the IBusiness<T> interface looks like (part of the missing information) so I don't know which of the methods in DistributorParentBusiness is part of the interface. If you call a method that isn't in the interface, it won't get intercepted.



              Loggable looks like an attribute but needs to implement IInterceptor. I don't know what Loggable looks like, but it must implement IInterceptor if you're using it to intercept. It does strike me as odd that it's an attribute at all; it doesn't seem to be doing anything as it's applied in attribute form.



              The Autofac docs have examples of setting up interface injection. If you use the InterceptedBy(typeof(T)) then you don't use attributes. If you want to use attributes, it needs to be [Intercept(typeof(Logger))] and, if you use the attribute, you don't use InterceptedBy(typeof(T)). It's either/or, and the interceptor is never the attribute itself.



              Controllers are services but I don't know where IBusiness<DistributorParent> is getting used. Again, missing info. If it's being injected into a controller it should work; I think it should even work if you don't add controllers-as-services.



              That route builder call where you count all the routes is weird. No, this has nothing to do with the question, but... it's counting them and not using the value? What's... uh... what's going on there?



              What I would try is creating a unit-test level repro for yourself. That is, create a tiny repro just in a unit test that you can step into. Make sure you're wiring up the interface interception correctly first. Make a super simple, basically empty IBusiness<DistributorParent> implementation; create a container that has the interface interception wired up on that; resolve IBusiness<DistributorParent> manually; set a breakpoint in the interceptor; call a method you expect to be intercepted and see if the breakpoint is hit. Or create an interceptor that always returns a fixed known value and use that in your test if breakpoints aren't getting hit - sometimes settings like "Just My Code" and so on can end up causing certain things to be skipped.



              Point being, if none of the above things get you unwound, create a real minimum repro in a unit test form and make sure it works. Widen it up a little if it does - maybe you used some other interceptor in the repro and it works; try the actual Loggable interceptor. Does it still work? If not, maybe the problem's the interceptor. If so, keep widening scope. Eventually you'll either get the whole thing working or you'll find the spot that isn't working.



              If you do get the whole thing working in a minimal repro, then it's possible the issue is something you don't realize. For example, is the code in the repro exactly the same as the code in the real system? What's different? Work up to making the repro code close to the real code and see when it stops working.



              The minimum repro is really going to be your key here, though, I think.



              Recommendation for future questions - when you create the code for the question, try to make it the simple, tiny repro that I described above for troubleshooting. Enough to show the problem (and possibly be compiled by someone who wants to try it out) but no more.



              • Include all the class and interface definitions in play. Loggable and IBusiness<T>, for example.

              • Remove stuff that isn't needed. The route configuration in the app.UseMvc() call isn't relevant, for example.

              • Try to simplify indirection and complexity. The code from the StartupHelper could be inlined into the ConfigureContainer method to make this easier to read through and understand for folks answering the question.





              share|improve this answer





























                0














                There's a lot to unpack here and not 100% of the information required to do it. I'll give you some things to look at and one of them should help.



                You're using interface interceptors, so only interface methods will be intercepted. I don't know what the IBusiness<T> interface looks like (part of the missing information) so I don't know which of the methods in DistributorParentBusiness is part of the interface. If you call a method that isn't in the interface, it won't get intercepted.



                Loggable looks like an attribute but needs to implement IInterceptor. I don't know what Loggable looks like, but it must implement IInterceptor if you're using it to intercept. It does strike me as odd that it's an attribute at all; it doesn't seem to be doing anything as it's applied in attribute form.



                The Autofac docs have examples of setting up interface injection. If you use the InterceptedBy(typeof(T)) then you don't use attributes. If you want to use attributes, it needs to be [Intercept(typeof(Logger))] and, if you use the attribute, you don't use InterceptedBy(typeof(T)). It's either/or, and the interceptor is never the attribute itself.



                Controllers are services but I don't know where IBusiness<DistributorParent> is getting used. Again, missing info. If it's being injected into a controller it should work; I think it should even work if you don't add controllers-as-services.



                That route builder call where you count all the routes is weird. No, this has nothing to do with the question, but... it's counting them and not using the value? What's... uh... what's going on there?



                What I would try is creating a unit-test level repro for yourself. That is, create a tiny repro just in a unit test that you can step into. Make sure you're wiring up the interface interception correctly first. Make a super simple, basically empty IBusiness<DistributorParent> implementation; create a container that has the interface interception wired up on that; resolve IBusiness<DistributorParent> manually; set a breakpoint in the interceptor; call a method you expect to be intercepted and see if the breakpoint is hit. Or create an interceptor that always returns a fixed known value and use that in your test if breakpoints aren't getting hit - sometimes settings like "Just My Code" and so on can end up causing certain things to be skipped.



                Point being, if none of the above things get you unwound, create a real minimum repro in a unit test form and make sure it works. Widen it up a little if it does - maybe you used some other interceptor in the repro and it works; try the actual Loggable interceptor. Does it still work? If not, maybe the problem's the interceptor. If so, keep widening scope. Eventually you'll either get the whole thing working or you'll find the spot that isn't working.



                If you do get the whole thing working in a minimal repro, then it's possible the issue is something you don't realize. For example, is the code in the repro exactly the same as the code in the real system? What's different? Work up to making the repro code close to the real code and see when it stops working.



                The minimum repro is really going to be your key here, though, I think.



                Recommendation for future questions - when you create the code for the question, try to make it the simple, tiny repro that I described above for troubleshooting. Enough to show the problem (and possibly be compiled by someone who wants to try it out) but no more.



                • Include all the class and interface definitions in play. Loggable and IBusiness<T>, for example.

                • Remove stuff that isn't needed. The route configuration in the app.UseMvc() call isn't relevant, for example.

                • Try to simplify indirection and complexity. The code from the StartupHelper could be inlined into the ConfigureContainer method to make this easier to read through and understand for folks answering the question.





                share|improve this answer



























                  0












                  0








                  0







                  There's a lot to unpack here and not 100% of the information required to do it. I'll give you some things to look at and one of them should help.



                  You're using interface interceptors, so only interface methods will be intercepted. I don't know what the IBusiness<T> interface looks like (part of the missing information) so I don't know which of the methods in DistributorParentBusiness is part of the interface. If you call a method that isn't in the interface, it won't get intercepted.



                  Loggable looks like an attribute but needs to implement IInterceptor. I don't know what Loggable looks like, but it must implement IInterceptor if you're using it to intercept. It does strike me as odd that it's an attribute at all; it doesn't seem to be doing anything as it's applied in attribute form.



                  The Autofac docs have examples of setting up interface injection. If you use the InterceptedBy(typeof(T)) then you don't use attributes. If you want to use attributes, it needs to be [Intercept(typeof(Logger))] and, if you use the attribute, you don't use InterceptedBy(typeof(T)). It's either/or, and the interceptor is never the attribute itself.



                  Controllers are services but I don't know where IBusiness<DistributorParent> is getting used. Again, missing info. If it's being injected into a controller it should work; I think it should even work if you don't add controllers-as-services.



                  That route builder call where you count all the routes is weird. No, this has nothing to do with the question, but... it's counting them and not using the value? What's... uh... what's going on there?



                  What I would try is creating a unit-test level repro for yourself. That is, create a tiny repro just in a unit test that you can step into. Make sure you're wiring up the interface interception correctly first. Make a super simple, basically empty IBusiness<DistributorParent> implementation; create a container that has the interface interception wired up on that; resolve IBusiness<DistributorParent> manually; set a breakpoint in the interceptor; call a method you expect to be intercepted and see if the breakpoint is hit. Or create an interceptor that always returns a fixed known value and use that in your test if breakpoints aren't getting hit - sometimes settings like "Just My Code" and so on can end up causing certain things to be skipped.



                  Point being, if none of the above things get you unwound, create a real minimum repro in a unit test form and make sure it works. Widen it up a little if it does - maybe you used some other interceptor in the repro and it works; try the actual Loggable interceptor. Does it still work? If not, maybe the problem's the interceptor. If so, keep widening scope. Eventually you'll either get the whole thing working or you'll find the spot that isn't working.



                  If you do get the whole thing working in a minimal repro, then it's possible the issue is something you don't realize. For example, is the code in the repro exactly the same as the code in the real system? What's different? Work up to making the repro code close to the real code and see when it stops working.



                  The minimum repro is really going to be your key here, though, I think.



                  Recommendation for future questions - when you create the code for the question, try to make it the simple, tiny repro that I described above for troubleshooting. Enough to show the problem (and possibly be compiled by someone who wants to try it out) but no more.



                  • Include all the class and interface definitions in play. Loggable and IBusiness<T>, for example.

                  • Remove stuff that isn't needed. The route configuration in the app.UseMvc() call isn't relevant, for example.

                  • Try to simplify indirection and complexity. The code from the StartupHelper could be inlined into the ConfigureContainer method to make this easier to read through and understand for folks answering the question.





                  share|improve this answer















                  There's a lot to unpack here and not 100% of the information required to do it. I'll give you some things to look at and one of them should help.



                  You're using interface interceptors, so only interface methods will be intercepted. I don't know what the IBusiness<T> interface looks like (part of the missing information) so I don't know which of the methods in DistributorParentBusiness is part of the interface. If you call a method that isn't in the interface, it won't get intercepted.



                  Loggable looks like an attribute but needs to implement IInterceptor. I don't know what Loggable looks like, but it must implement IInterceptor if you're using it to intercept. It does strike me as odd that it's an attribute at all; it doesn't seem to be doing anything as it's applied in attribute form.



                  The Autofac docs have examples of setting up interface injection. If you use the InterceptedBy(typeof(T)) then you don't use attributes. If you want to use attributes, it needs to be [Intercept(typeof(Logger))] and, if you use the attribute, you don't use InterceptedBy(typeof(T)). It's either/or, and the interceptor is never the attribute itself.



                  Controllers are services but I don't know where IBusiness<DistributorParent> is getting used. Again, missing info. If it's being injected into a controller it should work; I think it should even work if you don't add controllers-as-services.



                  That route builder call where you count all the routes is weird. No, this has nothing to do with the question, but... it's counting them and not using the value? What's... uh... what's going on there?



                  What I would try is creating a unit-test level repro for yourself. That is, create a tiny repro just in a unit test that you can step into. Make sure you're wiring up the interface interception correctly first. Make a super simple, basically empty IBusiness<DistributorParent> implementation; create a container that has the interface interception wired up on that; resolve IBusiness<DistributorParent> manually; set a breakpoint in the interceptor; call a method you expect to be intercepted and see if the breakpoint is hit. Or create an interceptor that always returns a fixed known value and use that in your test if breakpoints aren't getting hit - sometimes settings like "Just My Code" and so on can end up causing certain things to be skipped.



                  Point being, if none of the above things get you unwound, create a real minimum repro in a unit test form and make sure it works. Widen it up a little if it does - maybe you used some other interceptor in the repro and it works; try the actual Loggable interceptor. Does it still work? If not, maybe the problem's the interceptor. If so, keep widening scope. Eventually you'll either get the whole thing working or you'll find the spot that isn't working.



                  If you do get the whole thing working in a minimal repro, then it's possible the issue is something you don't realize. For example, is the code in the repro exactly the same as the code in the real system? What's different? Work up to making the repro code close to the real code and see when it stops working.



                  The minimum repro is really going to be your key here, though, I think.



                  Recommendation for future questions - when you create the code for the question, try to make it the simple, tiny repro that I described above for troubleshooting. Enough to show the problem (and possibly be compiled by someone who wants to try it out) but no more.



                  • Include all the class and interface definitions in play. Loggable and IBusiness<T>, for example.

                  • Remove stuff that isn't needed. The route configuration in the app.UseMvc() call isn't relevant, for example.

                  • Try to simplify indirection and complexity. The code from the StartupHelper could be inlined into the ConfigureContainer method to make this easier to read through and understand for folks answering the question.






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 8 at 15:50

























                  answered Mar 8 at 15:44









                  Travis IlligTravis Illig

                  16.2k14471




                  16.2k14471























                      0














                      The issue was very silly. I was manually creating the class and not using di. Therefore the methods weren't getting intercepted. Also, the original setup was trying to intercept the api controller and the methods weren't virtual. Little config changes to inject IBusiness and voila, it worked...






                      share|improve this answer



























                        0














                        The issue was very silly. I was manually creating the class and not using di. Therefore the methods weren't getting intercepted. Also, the original setup was trying to intercept the api controller and the methods weren't virtual. Little config changes to inject IBusiness and voila, it worked...






                        share|improve this answer

























                          0












                          0








                          0







                          The issue was very silly. I was manually creating the class and not using di. Therefore the methods weren't getting intercepted. Also, the original setup was trying to intercept the api controller and the methods weren't virtual. Little config changes to inject IBusiness and voila, it worked...






                          share|improve this answer













                          The issue was very silly. I was manually creating the class and not using di. Therefore the methods weren't getting intercepted. Also, the original setup was trying to intercept the api controller and the methods weren't virtual. Little config changes to inject IBusiness and voila, it worked...







                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 10 at 0:20









                          user1931270user1931270

                          245




                          245



























                              draft saved

                              draft discarded
















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid


                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.

                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function ()
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55051823%2fautofac-not-intercepting-calls-to-classes%23new-answer', 'question_page');

                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              1928 у кіно

                              Захаров Федір Захарович

                              Ель Греко