attributes - Web Api Autofac InstancePerRequest -


i have custom authorizationfilter attribute on web api project this

 [attributeusage(attributetargets.class | attributetargets.method, allowmultiple = false)] public class genericauthenticationfilter :  authorizationfilterattribute {      /// <summary>     /// public default constructor     /// </summary>     public genericauthenticationfilter()     {     }      private readonly bool _isactive = true;      /// <summary>     /// parameter isactive explicitly enables/disables filter.     /// </summary>     /// <param name="isactive"></param>     public genericauthenticationfilter(bool isactive)     {         _isactive = isactive;     }        /// <summary>     /// checks basic authentication request     /// </summary>     /// <param name="filtercontext"></param>     public override void onauthorization(httpactioncontext filtercontext)     {          if (!_isactive) return;         var identity = fetchauthheader(filtercontext);         if (identity == null)         {             challengeauthrequest(filtercontext);             return;         }         var genericprincipal = new genericprincipal(identity, null);         thread.currentprincipal = genericprincipal;         if (!onauthorizeuser(identity.name, identity.password, filtercontext))         {             challengeauthrequest(filtercontext);             return;         }          base.onauthorization(filtercontext);     } 

my startupclass this

  public class startup {     public void configuration(iappbuilder app)     {         // more information on how configure application, visit http://go.microsoft.com/fwlink/?linkid=316888         // httpconfiguration. in owin, you'll create 1         // rather using globalconfiguration.         var config = new httpconfiguration();         webapiconfig.register(config);         ioc.instance.registerapicontrollers(assembly.getexecutingassembly());         config.dependencyresolver =         new autofacwebapidependencyresolver(ioc.instance.getcomponentscontainer());         // register web api controllers.         ioc.instance.registerapicontrollers(assembly.getexecutingassembly());         ioc.instance.registerwebapimodelbinders(assembly.getexecutingassembly());         ioc.instance.registerwebapimodelbinderprovider();         ioc.instance.registerwebapifilterprovider(config);         // register autofac middleware first, autofac web api middleware,         // , standard web api middleware.         app.useautofacmiddleware(ioc.instance.getcomponentscontainer());         app.useautofacwebapi(config);         app.usewebapi(config);      } } 

and ioc class dependencies resolved this

public class ioc : containerbuilder {      /// <summary>     ///      /// </summary>     private readonly static ioc _instance = new ioc();      /// <summary>     ///      /// </summary>     private static object _lock;      /// <summary>     ///      /// </summary>     private icontainer _componentscontainer;      /// <summary>     ///      /// </summary>     public static ioc instance     {                 {             return _instance;         }     }      /// <summary>     ///      /// </summary>     /// <returns></returns>     public icontainer getcomponentscontainer()     {         if (_componentscontainer == null)         {             lock (_lock)             {                 if (_componentscontainer == null)                     _componentscontainer = this.build();             }         }          return _componentscontainer;     }      /// <summary>     ///      /// </summary>     /// <typeparam name="t"></typeparam>     /// <returns></returns>     public t resolve<t>() t : class     {         return getcomponentscontainer().resolve<t>();     }      /// <summary>     ///      /// </summary>     /// <returns></returns>     public ilifetimescope beginlifetimescope()     {         return getcomponentscontainer().beginlifetimescope();     }      /// <summary>     ///      /// </summary>     private ioc()     {         _lock = new object();         configuredependencies();     }      /// <summary>     ///      /// </summary>     private void configuredependencies()     {         //configure depedendencies here!!          //database connection         var connectionstring = configurationmanager.connectionstrings["dbconnectionstringname"].connectionstring;         this.register(c => new sqlconnection(connectionstring)).as<idbconnection>().instanceperrequest();// instanceperlifetimescope();          //database connection ormlite         ormliteconfig.dialectprovider = sqlserverdialect.provider;         //register repositories         this.registertype<repository>().as<irepository>().instanceperrequest();// instanceperlifetimescope();         // register services         this.registertype<userservice>().as<iuserservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<tokenservice>().as<itokenservice>().instanceperrequest();         this.registertype<dkmenuservice>().as<idkmenuservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<dkgridtblservice>().as<idkgridtblservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<fkservice>().as<ifkservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<lovservice>().as<ilovservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<jobservice>().as<ijobservice>().instanceperrequest();// instanceperlifetimescope();         this.registertype<madeservice>().as<imadeservice>().instanceperrequest();// instanceperlifetimescope();      }  } 

and decorate controllers filter this

[genericauthenticationfilter]     public authenticatecontroller(itokenservice tokenservice)     {         _tokenservice = tokenservice;     } 

my problem onauthorazation method of genericauthenticationfilter never fired. if on ioc class class change instanceperrequest instanceperlifetimescope works ok, want dependencies work per request

any ideas?

i'm not sure if part or of issue, but... can build containerbuilder once. in startup.configuration() see on lines 11-12:

config.dependencyresolver = new autofacwebapidependencyresolver(ioc.instance.getcomponentscontainer()); 

and ioc.instance.getcomponentscontainer() calls build() create container.

but 2 lines later see you're adding more components container, , after see second call:

app.useautofacmiddleware(ioc.instance.getcomponentscontainer()); 

based on code, that's going same container built before added new registrations. container won't include api controllers, model binders, or filter provider.

i'm not sure why you're not having more problems you're having now.

try moving setting of containers (the calls ioc.instance.getcomponentscontainer()) until all way @ end, after you've finished registering of dependencies.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -