http headers - Which HttpConfiguration object do I need to access to create a custom HttpParameterBinding? -
in this post, mike wasson states:
"besides parameterbindingattribute, there hook adding custom httpparameterbinding. on httpconfiguration object"
but have 3 httpconfiguration objects in web api app, namely:
public static void register(httpconfiguration config, iwindsorcontainer container) <-- in webapiconfig.cs private static void maproutes(httpconfiguration config) <-- "" public static void configurewindsor(httpconfiguration configuration) <-- in global.asax.cs
which of these (config, config, or configuration) should use (if any)?
update
i tried this, breakpoint on "if" line:
public static void configurewindsor(httpconfiguration configuration) { _container = new windsorcontainer(); _container.install(fromassembly.this()); _container.kernel.resolver.addsubresolver(new collectionresolver(_container.kernel, true)); var dependencyresolver = new windsordependencyresolver(_container); configuration.dependencyresolver = dependencyresolver; if (configuration.properties.values.count > 0) // <-- put casey jones here { object o = configuration.properties.values.elementat(configuration.properties.values.count - 1); string s = o.tostring(); } }
...but hit spot once, on server starting up, not when client sent request it...there must some event gets fired when server passes request incoming url can examined...no?
usually have 1 instance of httpconfiguration 1 globalconfiguration.configuration.
said so, that's how plugged custom binders
in global.asax
var bindermappings = new dictionary<type, type> { {typeof(yourmodeltype), typeof(yourmodeltypebinder)}, //.... }; config.services.add( typeof(modelbinderprovider), new windsormodelbinderprovider(container, bindermappings));
windsormodelbinderprovider
public class windsormodelbinderprovider : modelbinderprovider { private readonly iwindsorcontainer _container; private readonly idictionary<type, type> _bindermappings; public windsormodelbinderprovider(iwindsorcontainer container, idictionary<type, type> bindermappings) { _container = container; _bindermappings = bindermappings; } public override imodelbinder getbinder(httpconfiguration configuration, type modeltype) { imodelbinder binder = null; if (_bindermappings.containskey(modeltype)) { binder = _container.resolve(_bindermappings[modeltype]) imodelbinder; if (binder == null) { throw new componentnotfoundexception(modeltype); } } return binder; } }
yourmodeltypebinder
public class yourmodeltypebinder : imodelbinder { public yourmodeltypebinder(iyourservicetoloadyourmodeltype service) { //... } public bool bindmodel(httpactioncontext actioncontext, modelbindingcontext bindingcontext) { bindingcontext.model = yourcustomcodetoloadyourmodeltypeusingtheconstructordependecies(actioncontext.request); return true; } private yourmodeltype yourcustomcodetoloadyourmodeltypeusingtheconstructordependecies(httprequestmessage requestmessage) { ... } }
yourmodeltypebinder resolved container(see windsormodelbinderprovider), need registered first.
after plumbing, controller may have parameter, among others, following
[modelbinder]yourmodeltype user
Comments
Post a Comment