Web Api Request Content is empty in action filter

The request body is a non-rewindable stream; it can be read only once. The formatter has already read the stream and populated the model. We’re not able to read the stream again in the action filter. You could try: public class LogAttribute : ActionFilterAttribute { public override void OnActionExecuting(HttpActionContext actionContext) { var myModel = actionContext.ActionArguments[“myModel”]; … Read more

How to use Container instead of ObjectFactory in StructureMap ServiceActivator?

The static stuff is going away. If your not using a Service Locator of some type you’re going to have implement your own “ObjectFactory” as referenced here: public static class ObjectFactory { private static readonly Lazy<Container> _containerBuilder = new Lazy<Container>(defaultContainer, LazyThreadSafetyMode.ExecutionAndPublication); public static IContainer Container { get { return _containerBuilder.Value; } } private static Container … Read more

Error handling (Sending ex.Message to the client)

Here is an simple error DTO class public class ErrorDto { public int Code {get;set;} public string Message { get; set; } // other fields public override string ToString() { return JsonConvert.SerializeObject(this); } } And then using the ExceptionHandler middleware: app.UseExceptionHandler(errorApp => { errorApp.Run(async context => { context.Response.StatusCode = 500; // or another Status accordingly … Read more

Model always null on XML POST

Two things: You don’t need quotes “” around the content type and accept header values in Fiddler: User-Agent: Fiddler Content-Type: application/xml Accept: application/xml Web API uses the DataContractSerializer by default for xml serialization. So you need to include your type’s namespace in your xml: <TestModel xmlns=”http://schemas.datacontract.org/2004/07/YourMvcApp.YourNameSpace”> <Output>Sito</Output> </TestModel> Or you can configure Web API to … Read more

Request.Content.ReadAsMultipartAsync never returns

I ran into something similar in .NET 4.0 (no async/await). Using the debugger’s Thread stack I could tell that ReadAsMultipartAsync was launching the task onto the same thread, so it would deadlock. I did something like this: IEnumerable<HttpContent> parts = null; Task.Factory .StartNew(() => parts = Request.Content.ReadAsMultipartAsync().Result.Contents, CancellationToken.None, TaskCreationOptions.LongRunning, // guarantees separate thread TaskScheduler.Default) .Wait(); … Read more

tech