dependency injection - Resolving the same interface with Unity from two different assemblies in a single custom WCF Service Host Factory -
i building poc aims demonstrate how dependency hierarchy can constructed within wcf service unity , keep assemblies in application loosely coupled.
what did create following class libraries:
data access layer:
- 1 assembly repository interface.
- 1 assembly implementation of interface pretends access db.
- 1 assembly implementation of interface pretends access xml docs.
business layer:
- 1 assembly business object interface.
- 1 assembly implementation of interface receives on constructor repository interface.
service layer:
- 1 assembly service interface.
- 1 assembly implementation of interface receives on constructor business object interface.
finally created assembly service host factory, service host, , instance provider in charge of creating dependency hierarchy. code looks so:
public class unityservicehostfactory : servicehostfactory { private readonly unitycontainer _container; public unityservicehostfactory() { _container = new unitycontainer(); new containerconfigurator().configure(_container); } protected override servicehost createservicehost(type servicetype, uri[] baseaddresses) { return new unityservicehost(_container, servicetype, baseaddresses); } } public class containerconfigurator { public void configure(unitycontainer container) { container.registertype<iinvoicerepository, invoicerepository>("dbinvoicerepository"); container.registertype<iinvoicerepository, xmlinvoice>("xmlinvoicerepository"); container.registertype<iinvoicefinder, invoicefinder>(); } } public class unityservicehost : servicehost { public unityservicehost(unitycontainer container, type servicetype, params uri[] baseaddresses) : base(servicetype, baseaddresses) { if(container == null) throw new argumentnullexception("container"); var contracts = implementedcontracts.values; foreach (var c in contracts) { var instanceprovider = new unityinstanceprovider(container, servicetype); c.behaviors.add(instanceprovider); } } } public class unityinstanceprovider : iinstanceprovider, icontractbehavior { private readonly unitycontainer _container; private readonly type _servicetype; public unityinstanceprovider(unitycontainer container, type servicetype) { if (container == null) throw new argumentnullexception("container"); if (servicetype == null) throw new argumentnullexception("servicetype"); _container = container; _servicetype = servicetype; } public object getinstance(instancecontext instancecontext) { return getinstance(instancecontext, null); } public object getinstance(instancecontext instancecontext, message message) { return _container.resolve(_servicetype); } public void releaseinstance(instancecontext instancecontext, object instance) { _container.teardown(instance); } public void validate(contractdescription contractdescription, serviceendpoint endpoint) { } public void applydispatchbehavior(contractdescription contractdescription, serviceendpoint endpoint, dispatchruntime dispatchruntime) { dispatchruntime.instanceprovider = this; } public void applyclientbehavior(contractdescription contractdescription, serviceendpoint endpoint, clientruntime clientruntime) { } public void addbindingparameters(contractdescription contractdescription, serviceendpoint endpoint, bindingparametercollection bindingparameters) { } }
i testing console application, instantiate service proxy , make call method but, since both registrations named unity not know 1 instantiate. if remove name either of them gets resolved successfully.
basically able this:
static void main(string[] args) { //first call, want resolve invoicerepository concrete type //new invoiceservice(new invoicefinder(new invoicerepository)) var invoiceservice1 = new invoiceproxy(); var response1 = invoiceservice1.getsumarizedinvoiceby(new invoicerequest(1)); //second call, want resolve xmlinvoice concrete type //new invoiceservice(new invoicefinder(new xmlinvoice)) var invoiceservice2 = new invoiceproxy(); var response2 = invoiceservice2.getsumarizedinvoiceby(new invoicerequest(2)); }
notice how invoiceservice1 , invoiceservice2 2 different instances of same service dependency within own dependency resolved differently both.
what have can tell unity repository instantiate when either instantiating service proxy or calling method?
thanks help.
pass name gave unity in registertype<>() call resolve<>().
from resolving object type , registration name
// create container , register types iunitycontainer mycontainer = new unitycontainer(); mycontainer.registertype(typeof(myservicebase), typeof(dataservice), "data"); mycontainer.registertype(typeof(myservicebase), typeof(loggingservice), "logging"); // retrieve instance of each type myservicebase mydataservice = (myservicebase)mycontainer.resolve(typeof(myservicebase), "data");
you using generic extension methods equivalent example.
Comments
Post a Comment