dependency injection - Access filter attributes from web API constructor C# -
i feel confident have wrong (or backwards). i'm looking determine environment request coming from, can determine database access (based on referrer url).
the end point "self-hosted" api on 1 of our servers, , i'd send requests both development , production service, , handle requests accordingly. i'd avoid adding additional frameworks, avoiding having handle environment in each action call.
here's have going far.
public serviceprovider svc; public ectaskservice lab; public baseapicontroller() { //i hoping environment reference here.... svc = new serviceprovider(); lab = new ectaskservice(); } my hope access filter context below constructor above pass environment down service provider, handle of facets of database / dev environment needed.
public class labelserverfilter : actionfilterattribute { public override void onactionexecuting(httpactioncontext actioncontext) { environment(actioncontext); base.onactionexecuting(actioncontext); } public override task onactionexecutingasync(httpactioncontext actioncontext, cancellationtoken cancellationtoken) { environment(actioncontext); return base.onactionexecutingasync(actioncontext, cancellationtoken); } public string environment(httpactioncontext actioncontext) { try { var env = "production"; uri referrer = actioncontext.request.headers.referrer; if (referrer != null) { string clientdomain = referrer.getleftpart(uripartial.authority); if (clientdomain.indexof("myinternalurldev") > -1) { env = "development"; } } //console.write("[" + env + "]"); return env; } catch (exception ex) { console.writeline(ex.message, ex); } return "production"; } } thanks in advance!
Comments
Post a Comment