asp.net mvc - I copy/pasted code from ArcGIS .NET ashx proxy into my Controller method, now debugging is jumping all over the place (multi-threaded, apparently) -
i have controller takes in json data via http post. inside controller call method copied/pasted arcgis's .net implementation of proxy that's needed connect arcgis's servers. sake of problem i'm having, part irrelavent.
before copying/pasting, execution flow line line. now, after copying , pasting (and subsequently adding call method), debugging execution flow jumping on place (because of different threads going on @ same time). don't know why happening- didn't see had threads in code copied , pasted. tell me why happening because of code copied/pasted appears not have multithreading?
here's controller code makes call method copied/pasted:
[httppost] public void postpicture(httprequestmessage msg) { httpcontext context = httpcontext.current; processrequest(context); ...
here's code copied , pasted arcgis (i'm sorry, it's long):
public void processrequest(httpcontext context) { httpresponse response = context.response; system.net.httpwebrequest req = (system.net.httpwebrequest)system.net.httpwebrequest.create(context.request.url); req.method = context.request.httpmethod; req.servicepoint.expect100continue = false; // set body of request post requests if (context.request.inputstream.length > 0) { byte[] bytes = new byte[context.request.inputstream.length]; context.request.inputstream.read(bytes, 0, (int)context.request.inputstream.length); req.contentlength = bytes.length; string ctype = context.request.contenttype; if (string.isnullorempty(ctype)) { req.contenttype = "application/x-www-form-urlencoded"; } else { req.contenttype = ctype; } using (stream outputstream = req.getrequeststream()) { outputstream.write(bytes, 0, bytes.length); } } // send request server system.net.webresponse serverresponse = null; try { serverresponse = req.getresponse(); } catch (system.net.webexception webexc) { response.statuscode = 500; response.statusdescription = webexc.status.tostring(); response.write(webexc.response); response.end(); return; } // set response client if (serverresponse != null) { response.contenttype = serverresponse.contenttype; using (stream bytestream = serverresponse.getresponsestream()) { // text response if (serverresponse.contenttype.contains("text") || serverresponse.contenttype.contains("json")) { using (streamreader sr = new streamreader(bytestream)) { string strresponse = sr.readtoend(); response.write(strresponse); } } else { // binary response (image, lyr file, other binary file) binaryreader br = new binaryreader(bytestream); byte[] outb = br.readbytes((int)serverresponse.contentlength); br.close(); // tell client not cache image since it's dynamic response.cachecontrol = "no-cache"; // send image client // (note: if large images/files sent, modify send in chunks) response.outputstream.write(outb, 0, outb.length); } serverresponse.close(); } } response.end(); } public bool isreusable { { return false; } } // gets token server url configuration file // todo: ?modify can generate new short-lived token username/password in config file private string gettokenfromconfigfile(string uri) { try { proxyconfig config = proxyconfig.getcurrentconfig(); if (config != null) return config.gettoken(uri); else throw new applicationexception( "proxy.config file not exist @ application root, or not readable."); } catch (invalidoperationexception) { // proxy being used unsupported service (proxy.config has mustmatch="true") httpresponse response = httpcontext.current.response; response.statuscode = (int)system.net.httpstatuscode.forbidden; response.end(); } catch (exception e) { if (e applicationexception) throw e; // return empty string @ point // -- may want throw exception, or add log file } return string.empty; } } [xmlroot("proxyconfig")] public class proxyconfig { #region static members private static object _lockobject = new object(); public static proxyconfig loadproxyconfig(string filename) { proxyconfig config = null; lock (_lockobject) { if (system.io.file.exists(filename)) { xmlserializer reader = new xmlserializer(typeof(proxyconfig)); using (system.io.streamreader file = new system.io.streamreader(filename)) { config = (proxyconfig)reader.deserialize(file); } } } return config; } public static proxyconfig getcurrentconfig() { proxyconfig config = httpruntime.cache["proxyconfig"] proxyconfig; if (config == null) { string filename = getfilename(httpcontext.current); config = loadproxyconfig(filename); if (config != null) { cachedependency dep = new cachedependency(filename); httpruntime.cache.insert("proxyconfig", config, dep); } } return config; } public static string getfilename(httpcontext context) { return context.server.mappath("~/proxy.config"); } #endregion serverurl[] serverurls; bool mustmatch; [xmlarray("serverurls")] [xmlarrayitem("serverurl")] public serverurl[] serverurls { { return this.serverurls; } set { this.serverurls = value; } } [xmlattribute("mustmatch")] public bool mustmatch { { return mustmatch; } set { mustmatch = value; } } public string gettoken(string uri) { foreach (serverurl su in serverurls) { if (su.matchall && uri.startswith(su.url, stringcomparison.invariantcultureignorecase)) { return su.token; } else { if (string.compare(uri, su.url, stringcomparison.invariantcultureignorecase) == 0) return su.token; } } if (mustmatch) throw new invalidoperationexception(); return string.empty; } } public class serverurl { string url; bool matchall; string token; [xmlattribute("url")] public string url { { return url; } set { url = value; } } [xmlattribute("matchall")] public bool matchall { { return matchall; } set { matchall = value; } } [xmlattribute("token")] public string token { { return token; } set { token = value; } } }
Comments
Post a Comment