// public InsertRecordRetro InsertRecordRetro; // private readonly SetupController _setupController; //Constructor public DesktopWebController() { Trace.TraceInformation("Entering DesktopWebController constructor..."); MyMongoCollection = SetupController.GetNewCollectionIfNotExist(WebToMongoTypes.WEB); _mongoDataAccess = new MongoDataAccess(); }
public async Task<HttpResponseMessage> Post(HttpRequestMessage request) { Trace.TraceInformation("Entering Post..."); if (MyMongoCollection == null) { Trace.TraceInformation("Post method. Collection == null."); return new HttpResponseMessage(HttpStatusCode.OK); } //Get content from http header string value = await Request.Content.ReadAsStringAsync(); // Request.Content.ReadAsStringAsync(); MobileWeb mdata = null; try { //convert string to type MobileWeb Trace.TraceInformation(WebToMongoHelpers.TruncateLongString(value, 65000)); //64k is the max a field can contain in Azure table storage Trace.TraceInformation("Starting JSON deserialize..."); mdata = JsonConvert.DeserializeObject<MobileWeb>(value); mdata.info.ipaddress = WebToMongoHelpers.GetClientIp(request, this); Trace.TraceInformation("Done with JSON deserialize."); } catch (JsonSerializationException ex) { //if received string (json) does not conform to model, write an error to the WAD log Trace.TraceError(ex.Message,ex); return new HttpResponseMessage(HttpStatusCode.OK); } catch (Exception ex) { //if received string (json) does not conform to model, write an error to the WAD log Trace.TraceError(ex.Message, ex); return new HttpResponseMessage(HttpStatusCode.OK); } #region mike code // Thread work = new Thread(responseObject => InsertRecordRetro.InsertRecord(responseObject, Collection)); // work.Start(mdata); #endregion var mongoDataAccess = new MongoDataAccess(); new Thread(() => { try { mongoDataAccess.InsertWeb(mdata, MyMongoCollection); } catch(Exception e) { Trace.TraceError(e.Message); } }).Start(); //we tell the caller that the record was created even though the write to mongo may have failed Trace.TraceInformation("Exiting Post..."); return new HttpResponseMessage(HttpStatusCode.Created); }