title: My override of Async signature of GetWebResponse throws a runtime exception when setting property on response
body: <p>This is related to a question I asked a couple weeks ago but never got an answer on.  I need to override the asynchronous signature of System.Net.WebClient.GetWebResponse, to set the ContentType to utf-8.
  (<a href="https://msdn.microsoft.com/en-us/library/c2xze5ez(v=vs.110).aspx" rel="nofollow noreferrer">https://msdn.microsoft.com/en-us/library/c2xze5ez(v=vs.110).aspx</a>)</p>
  
  <p>I'm able to do this just fine with the synchronous call, but the asynchronous implementation throws a runtime exception if I try to set that same property - though I can SEE the property if I examine the response in the debugger.  </p>
  
  <p>I'm hoping it's something dumb that I've overlooked.</p>
  
  <p>In the example below, I've created a Web Reference (yes, the old kind - no, I can't change it to a service reference right now) to the TempConvert service found here, and called it OldSchoolReference.TempConvert:
  <a href="http://www.w3schools.com/webservices/tempconvert.asmx" rel="nofollow noreferrer">http://www.w3schools.com/webservices/tempconvert.asmx</a></p>
  
  <p>Then I inherited from that reference, as follows:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Net;
  using System.Text;
  using System.Threading.Tasks;
  
  namespace WindowsFormsApplication1.OldSchoolReference
  {
      public partial class ServiceProxy : TempConvert
      {
          protected override System.Net.WebResponse GetWebResponse(System.Net.WebRequest request)
          {
              WebResponse response = base.GetWebResponse(request);
              response.Headers.Set("Content-Type", "text/xml; charset=UTF-8");    // Works fine!
              return response;
          }
  
          protected override System.Net.WebResponse GetWebResponse(System.Net.WebRequest request, IAsyncResult result)
          {
              HttpWebResponse response = base.GetWebResponse(request, result) as HttpWebResponse;
              response.Headers.Set("Content-Type", "text/xml; charset=UTF-8"); 
              // You will note that this implementation is an System.Net.HttpWebResponse whereas the sychronous one above is a System.Net.WebResponse
              return response;
          }
      }
  }
  </code></pre>
  
  <p>I'm exercising the behavior with a simple one-button windows form that calls the service.</p>
  
  <pre><code>    private void button1_Click(object sender, EventArgs e)
      {
          OldSchoolReference.ServiceProxy service = new OldSchoolReference.ServiceProxy();
  
          service.CelsiusToFahrenheitCompleted += CallBack;
          service.CelsiusToFahrenheitAsync("0"); // returns 32, naturally
      }
  
      private static void CallBack(object sender, OldSchoolReference.CelsiusToFahrenheitCompletedEventArgs e)
      {
          MessageBox.Show(e.Result);
      }
  </code></pre>
  
  <p>But it's throwing an exception :(</p>
  
  <p><img src="https://i.stack.imgur.com/DKPUi.png" alt="Exception"></p>
  
  <p>As you can see in the screenshot, there's a ContentType property on the response, but it seems the setter method for the property isn't implemented.  Yes, I know it's already set to utf-8, but that's not the issue.  I need to force it to always be utf-8 via my override.</p>
  
  <p><img src="https://i.stack.imgur.com/Jy26U.png" alt="Debugger view"></p>
  
  <p>Lastly, but of no consequence for me, the synchronous implementation I've included the override for as well does work - but I need it to be on the async version.</p>
  
  <p>Does anyone know how I can set this property on the asynchronous response?  I would be much obliged!</p>