title: Middleware - performance issues
body: <p>I'm developing a Middleware for my client and it's found that there are some delay when executing the process.</p>
  
  <p>We are currently investigating what's the exact causes of the issue, from architecture design to coding.</p>
  
  <p>Below are part of the scripts we are calling, and just wondering if the codes itself is optimized and properly written?</p>
  
  <p>There's <strong>no error</strong> from the codes below, just wondering if there's any <strong>performance or coding issues</strong>. Tks</p>
  
  <pre><code>public string getResponse(System.Type type, Object input, string taskName, string method)
  {
      string response = string.Empty;
  
      try
      {
          DataContractJsonSerializer ser = new DataContractJsonSerializer(type);
          MemoryStream mem = new MemoryStream();
          ser.WriteObject(mem, input);
          string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
  
          using (WebClient webClient = new WebClient())
          {
              webClient.Headers["Content-type"] = "application/json";
              webClient.Encoding = Encoding.UTF8;
  
              logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");
  
              response = webClient.UploadString(string.Format(Configuration.getTargetURI(), taskName), method, data);
  
              logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
              logger.Debug("Response: " + response);
          }
      }
      catch (Exception ex)
      {
          logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
          logger.Error(ex.Message + ex.StackTrace);
  
          throw ex;
      }
  
      return response;
  }
  
  public string getResponseNoInput(string taskName, string method)
  {
      string response = string.Empty;
  
      try
      {
          WebRequest req = WebRequest.Create(String.Format(Configuration.getTargetURI(), taskName));
          req.Method = method;
          req.ContentType = "application/json; charset=utf-8";
  
          logger.Info("Sending [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "]");
  
          HttpWebResponse resp = req.GetResponse() as HttpWebResponse;
          using (StreamReader sr = new StreamReader(resp.GetResponseStream()))
          {
              response = sr.ReadToEnd();
          }
  
          logger.Info("Response of API[" + string.Format(Configuration.getTargetURI(), taskName) + "] successfully retrieved");
          logger.Debug("Response: " + response);
      }
      catch(Exception ex)
      {
          logger.Info("Failed to invoke [" + method + "] request to API[" + string.Format(Configuration.getTargetURI(), taskName) + "], Error: " + ex.Message);
          logger.Error(ex.Message + ex.StackTrace);
  
          throw ex;
      }
  
      return response;
  }
  </code></pre>