Stiamo creando un WebApi che stiamo ospitando utilizzando Owin. In precedenza abbiamo utilizzato HttpResponseException per la restituzione dei codici di stato 404 ecc. Nelle azioni del nostro controller e ha funzionato bene.Lancio HttpResponseException dal controller WebApi quando si utilizza l'host automatico Owin
Tuttavia, quando abbiamo iniziato a lavorare con Owin (self hosted) abbiamo riscontrato un problema con questo approccio che ha portato alla serializzazione di HttpResponseException su json/xml e al codice di stato da 404 a 500 (Errore interno del server) . Ecco il codice che abbiamo:
public class InvoicesController : ApiController
{
private readonly IInvoiceRepository _invoiceRepository;
public InvoicesController(IInvoiceRepository invoiceRepository)
{
_invoiceRepository = invoiceRepository;
}
[HttpGet]
public IEnumerable<AccountCodeAssignment> AssignAccountCodesToInvoiceById(int id)
{
var invoice = _invoiceRepository.Get(id);
if (invoice == null) throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Invoice not found"));
yield return new AccountCodeAssignment(1, ...);
yield return new AccountCodeAssignment(2, ...);
yield return new AccountCodeAssignment(3, ...);
yield return new AccountCodeAssignment(4, ...);
}
}
e questa è la risposta torniamo insieme a un codice di 500 risposta:
{
"Message": "An error has occurred.",
"ExceptionMessage": "Processing of the HTTP request resulted in an exception. Please see the HTTP response returned by the 'Response' property of this exception for details.",
"ExceptionType": "System.Web.Http.HttpResponseException",
"StackTrace": " at AccountCodeAssignmentService.Controllers.InvoicesController.<AssignAccountCodesToInvoiceById>d__0.MoveNext() in c:\\Projects\\AccountCodeAssignmentService\\Source\\AccountCodeAssignmentService\\Controllers\\InvoicesController.cs:line 38\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeList(JsonWriter writer, IEnumerable values, JsonArrayContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)\r\n at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)\r\n at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.JsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, Encoding effectiveEncoding)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStream(Type type, Object value, Stream writeStream, HttpContent content)\r\n at System.Net.Http.Formatting.BaseJsonMediaTypeFormatter.WriteToStreamAsync(Type type, Object value, Stream writeStream, HttpContent content, TransportContext transportContext, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Web.Http.Owin.HttpMessageHandlerAdapter.<BufferResponseContentAsync>d__13.MoveNext()"
}
Tutte le idee su quello che stiamo facendo di sbagliato o è HttpResponseException non supportato quando usando Owin self-hosting?
Edit: Uno dei grandi vantaggi di utilizzare WebAPI per noi è la capacità di lavorare con e restituire i nostri tipi, quindi vorremmo evitare di dover cambiare il tipo di ritorno. Attualmente stiamo cedendo AccountCodeAssignment in modo che la modifica del tipo di reso non sia un'opzione.
Sì, il problema sembra essere correlato a "rendimento". Se estraiamo le righe di rendimento in un metodo che restituisce IEnumberable e restituisci il risultato di tale metodo nell'azione, funziona! Quindi immagino che la causa sia stata trovata ma non vedo ancora perché questo potrebbe accadere !? Per riassumere, non cedere e utilizzare HttpResponseException nello stesso metodo! –