title: RestSharp unable to cast object when awaiting for response
body: <p>Using RestSharp I'm building an API to perform CRUD operations given a datatype/object.</p>
  
  <p>My <strong><code>CrudAbstract</code></strong> class is generic and has the following:</p>
  
  <pre><code>public virtual async Task&lt;keyType&gt; Post(dto item)
  {
      try
      {
          var request = await _client.GetRequestAsync(_path);
          request.Method = RestSharp.Method.POST;
          request.AddJsonBody(item);
          var result = await _client.ExecuteRequestAsync&lt;keyType&gt;(request);
          return result;
      }
      catch (Exception e)
      {
          Console.WriteLine(e.Message);
      }
      throw new Exception("Was not able to process crud post operation.");
  }
  </code></pre>
  
  <p>My <strong><code>WebClient</code></strong> class has the following:</p>
  
  <pre><code>Entities = new CrudAbstract&lt;DtoEntity, int&gt;("/entities", this); // in constructor
  // So the keyType from definition above is int (Int32)
  </code></pre>
  
  <p>The post method in this class is</p>
  
  <pre><code>public async Task&lt;T&gt; ExecuteRequestAsync&lt;T&gt;(IRestRequest request)
  {
      try
      {
          var response = await GetClient().ExecuteTaskAsync&lt;T&gt;(request);
  
          // Exception occurs here. The above statement is unable to finish.
  
          var data = response.Data;
          return data;
      }
      catch (Exception e)
      {
          // Log exception
      }
      throw new Exception("Was not able to process restclient execute async method.");
  }
  </code></pre>
  
  <p>My Api <strong><code>EntitiesController</code></strong> has the following:</p>
  
  <pre><code>    public int Post(DtoEntity value)
      {
          using (var db = // some database...)
          {
              try
              {
                  // Upsert object into database here
                  //... value.Id no longer null at this point
  
                  /*
                     The problem occurs here. I only want to return the ID of the object 
                     (value.Id). I do not want to return the whole object (value)
                  */
                  return value.Id;
              }
              catch (Exception e)
              {
                  // Log exception
              }
          }
          throw new Exception("Was not able to process entities post method.");
      }
  </code></pre>
  
  <p>The exception I get is:</p>
  
  <blockquote>
    <p>Unable to cast object of type 'System.Int64' to type
    'System.Collections.Generic.IDictionary`2[System.String,System.Object]'.</p>
  </blockquote>
  
  <p>This is basically saying it is unable to cast the object <code>int</code> (which I have returned in the post with <code>value.Id</code>) to a <code>DtoEntity</code> object (which is the actual object on which CRUD operations were performed).</p>
  
  <p>What am I doing wrong?</p>
  
  <p>I have placed <code>typeof</code> and <code>.getType()</code> onto each <code>keyType</code>, <code>T</code>, and <code>value.Id</code>, and all of them are <code>Int32</code>. Is it in the RestSharp library that the problem occurs? At what stage is there a casting of <code>int</code> to a <code>DtoEntity</code> in the line:</p>
  
  <pre><code>var response = await GetClient().ExecuteTaskAsync&lt;T&gt;(request);
  </code></pre>
  
  <p><strong>Note:</strong> when I change the return type of the post method in my controller to <code>DtoEntity</code> and change the <code>value.Id</code> to just <code>value</code> it works. The <code>response</code> is received, and the <code>response.Data</code> is the <code>DtoEntity</code> object.</p>
  
  <p>I've seen similar questions <a href="https://stackoverflow.com/questions/12678834/rest-sharp-executeint-fails-due-to-cast-exception">here</a> but not found a solution yet.</p>