/// <summary> /// Wraps a POCO object returned by the service method with an <see cref="IResult"/>. /// </summary> /// <param name="handler">The service context handler.</param> /// <param name="returnedObj">The returned object.</param> /// <param name="methodReturnType">The method return type.</param> /// <returns>The wrapper result.</returns> public virtual IResult Wrap(IServiceContextHandler handler, object returnedObj, Type methodReturnType) { if (handler == null) { throw new ArgumentNullException("handler"); } var result = returnedObj as IResult; return result ?? CreateFormatterResult(handler, returnedObj, methodReturnType); }
private IResult CreateFormatterResult(IServiceContextHandler handler, object returnedObj, Type methodReturnType) { string preferredMediaType = m_contentNegotiator.GetPreferredMediaType(handler.Context.Request); IMediaTypeFormatter formatter = MediaTypeFormatterRegistry.GetHandlerFormatter(handler, preferredMediaType) ?? MediaTypeFormatterRegistry.GetFormatter(preferredMediaType); if (formatter == null || !formatter.CanFormatResponse) { throw new HttpResponseException(HttpStatusCode.NotAcceptable, Resources.Global.MissingOrInvalidAcceptType); } return formatter.FormatResponse(handler.Context, methodReturnType, returnedObj, preferredMediaType); }
public static void AddHandlerFormatter(IServiceContextHandler handler, string mediaType, IMediaTypeFormatter formatter) { handlerFormatters.AddOrUpdate(handler, handlerToAdd => AddHandlerFactory(mediaType, formatter), (handlerToUpdate, formattersToUpdate) => UpdateHandlerFactory(mediaType, formatter, formattersToUpdate)); }
private static void GenerateFaultBody(IServiceContext context, IServiceContextHandler handler, FaultCollection faults) { if (faults.General.Length == 0 && faults.Resource.Length == 0) { return; } var resultWrapper = Rest.Configuration.ServiceLocator.GetService<IResultWrapper>(); IResult result; try { result = resultWrapper.Wrap(handler, faults, faults.GetType()); } catch (HttpResponseException ex) { context.Response.SetStatus(ex.StatusCode, ex.StatusDescription); return; } if (result != null) { result.Execute(context); } }
public static IMediaTypeFormatter GetHandlerFormatter(IServiceContextHandler handler, string mediaType) { if (handler == null || String.IsNullOrEmpty(mediaType)) { return null; } Dictionary<string, IMediaTypeFormatter> formatterDictionary; if (!handlerFormatters.TryGetValue(handler, out formatterDictionary)) { return null; } IMediaTypeFormatter formatter; return formatterDictionary.TryGetValue(mediaType, out formatter) ? formatter : null; }