wcf - Autofac instance management? -


lets manually instantiate objects this:

var context = new mydbcontext(); _uow = new unitofwork(context); _repository = new repository<account>(context, _uow); 

instead, want inject them , register them so:

private readonly iunitofworkasync _uow; private readonly irepositoryasync<account> _repository;  public accountservice(iunitofworkasync uow, irepositoryasync<account> repository) {     _uow = uow;     _repository = repository; } 

here registration.

builder.registertype<mydbcontext>().as<idatacontextasync>().instanceperdependency(); 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)); 

my service gets published iis , looks fine, doesn't function correctly. example, go insert new account , doesn't save db , doesn't error. seems injected unitofwork not same unitofwork used instantiate repository. confirmed code in constructor so:

if(_uow.gethashcode() != _repository.myuow.gethashcode())     throw new argumentexception("bad uow"); 

if inject unitofwork , repository ge exception. if manually instantiate objects not exception. i've tried changing registration .instanceperowned() service , other various registration changes no avail. how use autofac registration instantiate repository based on unitofwork instantiated? thought default .instanceperdependency() suffice doens't. thanks.


edit 1: using wcf , here customhostfactory. don't see option specify .instanceperrequest() stated in documentation. also, interestingly, line doesn't matter in below code. works same if take out. autofacservicehostfactory.container = container;

protected override servicehost createservicehost(type servicetype, uri[] baseaddresses) {     type contracttype = getcontracttype(servicetype);      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();     autofacservicehostfactory.container = container;     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)); } 

instanceperrequest available webforms , mvc integration packages.

try using autofac integration wcf in svc.less mode properly, described below.

from autofac docs:

svc-less services

if want use services without .svc file, autofac work that.

as shown above, register service container.

var builder = new containerbuilder(); builder.registertype<service1>(); autofachostfactory.container = builder.build(); 

to use svc-less services, add factory entry under serviceactivation element in web.config file. ensures autofacservicehostfactory used activate service.

<servicehostingenvironment aspnetcompatibilityenabled="true" multiplesitebindingsenabled="true">   <serviceactivations>     <add factory="autofac.integration.wcf.autofacservicehostfactory"          relativeaddress="~/service1.svc"          service="testservice.service1" />   </serviceactivations> </servicehostingenvironment> 

Comments

Popular posts from this blog

IF statement in MySQL trigger -

c++ - What does MSC in "// appease MSC" comments mean? -

javascript - Blogger related post gadget image Resize s72-c [ Need Expert Help ] -