c# - Generic ServiceFactory<T> for WCF Channel Factory and StructureMap with MVC 3 -
so interesting post because must include code , attempt explain how have setup architecture.
i have placed service , datacontracts in central assembly (dmt.wcf.contracts). done distributed pieces of application can reference same type of service interfaces , contracts nice.
i have setup structuremap container inject dependencies in following manner, specifying servicecontext, house of service interface properties can referenced int application later.
public interface iservicecontext { } public class servicecontext: iservicecontext { public iauthenticationservice authenticationservice { get; set; } public servicecontext(iauthenticationservice authenticationservice) { authenticationservice = authenticationservice; } }
then, have structuremapcontrollerfactory looks following:
public class structuremapcontrollerfactory:defaultcontrollerfactory { protected override icontroller getcontrollerinstance(requestcontext requestcontext, type controllertype) { if (controllertype == null) return null; return objectfactory.getinstance(controllertype) icontroller; } }
and configured in global.asax following:
protected void application_start() { controllerbuilder.current.setcontrollerfactory(new structuremapcontrollerfactory()); arearegistration.registerallareas(); registerglobalfilters(globalfilters.filters); registerroutes(routetable.routes); configure(); }
i wanted decouple services as possible appliction, have implemented following servicefactory class handles providing proxies structuremap when ioc container configured:
public static class servicefactory { private static readonly clientsection _clientsection = configurationmanager.getsection("system.servicemodel/client") clientsection; public static t create<t>() { t context = default(t); foreach(channelendpointelement endpoint in _clientsection.endpoints) { if(endpoint.contract == typeof(t).fullname) { ienumerable<type> assignables = typeof (binding).assembly.gettypes().where(p => typeof(binding).isassignablefrom(p)); type bindingtype = assignables.single(p => p.name.tolower().equals(endpoint.binding.tolower())); context = channelfactory<t>.createchannel((binding)activator.createinstance(bindingtype, false), new endpointaddress(endpoint.address)); } } return context; } }
this allows me pull directly config file when creating proxies not need select "add service reference" (as technically adding dependency).
in global.asax, can configure structuremap container this:
protected void configure() { objectfactory.configure(x => { x.scan(scanner => scanner.addalltypesof<icontroller>()); x.for<iauthenticationservice>().use(servicefactory.create<iauthenticationservice>()); x.for<iservicecontext>().use<servicecontext>(); }); }
although able use in following manner:
iauthenticationservice service = servicecontext.authenticationservice.authenticat(...);
i unable start application without exceptions being thrown such following:
structuremap configuration failures: error: 104 source: registry: structuremap.configuration.dsl.registry, structuremap, version=2.6.1.0, culture=neutral, publickeytoken=e60ad81abae3c223 type instance '685e2e2a-f271-4163-a6fa-ba074e4082d1' (object: dmt.wcf.contracts.authentication.iauthenticationservice) cannot plugged type dmt.wcf.contracts.authentication.iauthenticationservice, dmt.wcf.contracts, version=1.0.0.0, culture=neutral, publickeytoken=null
i not sure why occuring. said, able , running, not sure has changed.
i have looked @ many of hundreds of references regarding error message, specific problems dont seem match mine, unless overlooking problem.
help!!!
doesn't operation use channelfactory
new channel safe client?
context = channelfactory<t>.createchannel( (binding)activator.createinstance(bindingtype, false), new endpointaddress(endpoint.address));
Comments
Post a Comment