dependency injection - How to use Autofac with WCF fileless activation and a custom ServiceHostFactory? -


i need add binding/configuration in cusotmservicehostfactory. however, use autofac. how can customservicehostfacotry implement autofacservicehostfactory? i'm using fileless activation config section looks this:

<serviceactivations>                   <add service="project.business.services.accountservice"          relativeaddress="account/accountservice.svc"          factory="project.webhost.customservicehostfactory"/> </serviceactivations> 

here current custom factory:

public class customservicehostfactory : servicehostfactory {     protected override servicehost createservicehost(type servicetype, uri[] baseaddresses)     {         var builder = new containerbuilder();         builder.registertype<mydbcontext>().as<idatacontextasync>();         builder.registertype<unitofwork>().as<iunitofworkasync>();          builder.registergeneric(typeof (repository<>)).as(typeof (irepositoryasync<>));          builder.registerassemblytypes(typeof(accountservice).assembly)             .where(t => t.name.endswith("service"))             .as(t => t.getinterfaces().firstordefault(                 => i.name == "i" + t.name));          var container = builder.build();         var host = new customservicehost(servicetype, baseaddresses);         type contracttype = getcontracttype(servicetype);         host.adddependencyinjectionbehavior(contracttype, container);          return host;     }     private static type getcontracttype(type servicetype)     {         return servicetype.getinterfaces()             .firstordefault(i => attribute.isdefined(i, typeof(servicecontractattribute), false));     } } 

as can see, there set autfoacservicehostfactory.container property. i've tried changing customservicehostfactory implement autofacservicehostfactory so:

public class customservicehostfactory : autofacservicehostfactory 

..but no matter do, when publish iis , browse service error:

the autofacservicehost.container static property must set before services can instantiated.


edit 1: i've tried this.

public class customservicehostfactory : autofacservicehostfactory 

then assigning container. original code changes here:

autofacservicehostfactory.container = builder.build(); var host = new customservicehost(servicetype, baseaddresses); type contracttype = getcontracttype(servicetype); host.adddependencyinjectionbehavior(contracttype, autofacservicehostfactory.container); 

this gives same error. in fact, when run on localhost no longer createservicehost method when debugging. browser renders error. not proper entry point / composition root anymore?


edit 2: turns out work rather annoying. have override createservicehost method string constructorstring parameter. if try override other method signature (that takes type servicetype parameter) never hit breakpoint in method... no idea why. seems hack. why don't ever createservicehost(type servicetype... override?

public class customservicehostfactory : autofacservicehostfactory {     public customservicehostfactory()     {         var builder = new containerbuilder();         builder.registertype<mydbcontext>().as<idatacontextasync>();         builder.registertype<unitofwork>().as<iunitofworkasync>();         builder.registergeneric(typeof(repository<>)).as(typeof(irepositoryasync<>));          builder.registerassemblytypes(typeof(accountservice).assembly)             .where(t => t.name.endswith("service"))             .as(t => t.getinterfaces().firstordefault(                 => i.name == "i" + t.name));          container = builder.build();     }      public override servicehostbase createservicehost(string constructorstring, uri[] baseaddresses)     {         type servicetype = gettype(constructorstring);         type contracttype = getcontracttype(servicetype);         var host = new customservicehost(servicetype, baseaddresses);         host.adddependencyinjectionbehavior(contracttype, container);          return host;     }      private static type getcontracttype(type servicetype)     {         return servicetype.getinterfaces()             .firstordefault(i => attribute.isdefined(i, typeof(servicecontractattribute), false));     }      private static type gettype(string typename)     {         var type = type.gettype(typename);         if (type != null) return type;          foreach (var in appdomain.currentdomain.getassemblies())         {             type = a.gettype(typename);             if (type != null)                 return type;         }          return null;     } } 

ok, got working. hope can else. here customservicehostfactory:

public class customservicehostfactory : servicehostfactory {     public customservicehostfactory()     {         var builder = new containerbuilder();         builder.registertype<mydbcontext>().as<idatacontextasync>().instanceperlifetimescope();         builder.registertype<unitofwork>().as<iunitofworkasync>().instanceperlifetimescope();         builder.registergeneric(typeof(repository<>)).as(typeof(irepositoryasync<>)).instanceperlifetimescope();          builder.registerassemblytypes(typeof(accountservice).assembly)             .where(t => t.name.endswith("service"))             .as(t => t.getinterfaces().firstordefault(                 => i.name == "i" + t.name)).instanceperlifetimescope();          autofachostfactory.container = builder.build();     }      protected override servicehost createservicehost(type servicetype, uri[] baseaddresses)     {         type contracttype = getcontracttype(servicetype);         var host = new customservicehost(servicetype, baseaddresses);         host.adddependencyinjectionbehavior(contracttype, autofachostfactory.container);          return host;     }      private static type getcontracttype(type servicetype)     {         return servicetype.getinterfaces()             .firstordefault(i => attribute.isdefined(i, typeof(servicecontractattribute), false));     } } 

here customservicehost code (implementation omitted brevity):

public class customservicehost : servicehost {             public customservicehost(type servicetype, uri[] baseaddresses)         : base(servicetype, baseaddresses)     {     }      protected override void applyconfiguration()     {         base.applyconfiguration();         addservicedebugbehavior();         addwcfmessageloggingbehavior();         addglobalerrorhandlingbehavior();         addservicecredentialbehavior();         addendpoints();         configurethrottling();     }       //implement above methods here... } 

here config (section matters):

<servicehostingenvironment>   <!-- virtual .svc files defined -->   <serviceactivations>                     <add service="project.business.services.accountclassservice"            relativeaddress="account/accountclassservice.svc"            factory="project.webhost.customservicehostfactory"/>      <add service="project.business.services.userservice"           relativeaddress="user/userservice.svc"           factory="project.webhost.customservicehostfactory"/>   </serviceactivations> </servicehostingenvironment> 

each of wcf services has behavior attribute make them function per call (instead of persession default):

instancecontextmode = instancecontextmode.percall 

Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -