3

I'm trying to create a response which sends the HttpStatusCode along with a dynamic object like this:

var t = _rep.GetOrders(params, sft);
...........
return Request.CreateResponse(HttpStatusCode.OK, Enumerable.Select(t, 
  (Func<Order, dynamic>)(x =>
    new
    {
       x.Id,
       x.Name,
       .....
    })));

But it gives me this error message:

'System.Net.Http.HttpRequestMessage' has no applicable method named 'CreateResponse' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax.

I don't quite understand what it's saying, or what I'm doing wrong here.

Can anyone shine some light on this matter please?

Yustme
  • 6,125
  • 22
  • 75
  • 104
  • 1
    possible duplicate of [What causes "extension methods cannot be dynamically dispatched" here?](http://stackoverflow.com/questions/15391115/what-causes-extension-methods-cannot-be-dynamically-dispatched-here) – CodeCaster Feb 24 '14 at 12:22
  • 1
    Extension methods and dynamic objects do not work together. – danish Feb 24 '14 at 12:30
  • 2
    It's pointing you to a workaround - instead of doing `Request.CreateResponse` you can call the static method in a non-extension way `HttpRequestMessageExtensions.CreateResponse`. – Joanna Derks Feb 24 '14 at 14:05
  • that should be an answer – cori Mar 25 '17 at 03:24
  • Yes, the question is very old and the author may not pay attention, However, adding the method like this HttpRequestMessageExtensions.CreateResponse.(request, HttpStatusCode.Unauthorized) exposed the reason why it was not able to see the extension methods. It pointed that I needed package Micorsoft.AspNet.WebApi.Client that was not imported because I was working in a ClassLibrary type of project. After adding the required packagem, I was able to revert the change obviously and it was able to see the extension method. – Tauqir Mar 11 '22 at 21:57

0 Answers0