title: How to read json file in asp.net web api
body: <p>In my asp.web api 2.0 project I have a Json file, where all the error codes are mapped. I want to read the json file in order to return response to the caller.</p>
  
  <p>I am unable to read the same, however if I use console application following code works, any suggestion will be helpful.</p>
  
  <p>Code that works in console application:</p>
  
  <pre><code> var assembly = Assembly.GetExecutingAssembly();
   using (var stream = new StreamReader(assembly.GetManifestResourceStream("ConsoleApp24.Utilities.StatusCodes.json") ?? throw new InvalidOperationException()))
   {
       var status = JsonConvert.DeserializeObject&lt;RootObject&gt;(stream.ReadToEnd());    
   }
  </code></pre>
  
  <p>Using above code provides assembly as null in web api project, hence I changed it to following:</p>
  
  <pre><code>var assembly = GetWebEntryAssembly();
  using (var stream = new StreamReader(assembly.GetManifestResourceStream("PaymentAccount.Api.Resources.StatusCodes.json") ?? throw new InvalidOperationException()))
  {
       var status = JsonConvert.DeserializeObject&lt;RootObject&gt;(stream.ReadToEnd());       
  }
  
  private Assembly GetWebEntryAssembly()
  {
       if (System.Web.HttpContext.Current == null ||
              System.Web.HttpContext.Current.ApplicationInstance == null)
       {
             return null;
       }
  
       var type = System.Web.HttpContext.Current.ApplicationInstance.GetType();
       while (type != null &amp;&amp; type.Namespace == "ASP")
       {
             type = type.BaseType;
       }
  
       return type == null ? null : type.Assembly;
  }
  </code></pre>
  
  <p>The exception I get is: </p>
  
  <blockquote>
    <p>Operation is not valid due to the current state of the object.</p>
  </blockquote>