public void Register(string contentType, StreamSerializerDelegate streamSerializer, StreamDeserializerDelegate streamDeserializer) { if (contentType.IsNullOrEmpty()) throw new ArgumentNullException("contentType"); var parts = contentType.Split('/'); var format = parts[parts.Length - 1]; this.ContentTypeFormats[format] = contentType; SetContentTypeSerializer(contentType, streamSerializer); SetContentTypeDeserializer(contentType, streamDeserializer); }
public void Register(string contentType, StreamSerializerDelegate streamSerializer, StreamDeserializerDelegate streamDeserializer) { if (contentType.IsNullOrEmpty()) { throw new ArgumentNullException(nameof(contentType)); } var format = contentType.LastRightPart('/'); var normalizedContentType = ContentFormat.NormalizeContentType(contentType); ContentTypeFormats[format] = normalizedContentType; SetContentTypeSerializer(normalizedContentType, streamSerializer); SetContentTypeDeserializer(normalizedContentType, streamDeserializer); }
private static async Task serializeSync(StreamSerializerDelegate serializer, IRequest httpReq, object dto, Stream stream) { if (HostContext.Config.BufferSyncSerializers) { using (var ms = MemoryStreamFactory.GetStream()) { serializer(httpReq, dto, ms); ms.Position = 0; await ms.CopyToAsync(stream); return; } } httpReq.Response.AllowSyncIO(); serializer(httpReq, dto, stream); }
public void SetContentTypeSerializer(string contentType, StreamSerializerDelegate streamSerializer) { this.ContentTypeSerializers[contentType] = streamSerializer; }
/// <summary> /// Writes to response. /// Response headers are customizable by implementing IHasOptions an returning Dictionary of Http headers. /// </summary> /// <param name="response">The response.</param> /// <param name="result">Whether or not it was implicity handled by ServiceStack's built-in handlers.</param> /// <param name="defaultAction">The default action.</param> /// <param name="serializerCtx">The serialization context.</param> /// <returns></returns> public static bool WriteToResponse(this IHttpResponse response, object result, StreamSerializerDelegate defaultAction, IRequestContext serializerCtx) { var defaultContentType = serializerCtx.ResponseContentType; try { if (result == null) { return(true); } foreach (var globalResponseHeader in EndpointHost.Config.GlobalResponseHeaders) { response.AddHeader(globalResponseHeader.Key, globalResponseHeader.Value); } var httpResult = result as IHttpResult; if (httpResult != null) { response.StatusCode = (int)httpResult.StatusCode; if (string.IsNullOrEmpty(httpResult.ContentType)) { httpResult.ContentType = defaultContentType; } response.ContentType = httpResult.ContentType; } /* Mono Error: Exception: Method not found: 'System.Web.HttpResponse.get_Headers' */ var responseOptions = result as IHasOptions; if (responseOptions != null) { //Reserving options with keys in the format 'xx.xxx' (No Http headers contain a '.' so its a safe restriction) const string reservedOptions = "."; foreach (var responseHeaders in responseOptions.Options) { if (responseHeaders.Key.Contains(reservedOptions)) { continue; } Log.DebugFormat("Setting Custom HTTP Header: {0}: {1}", responseHeaders.Key, responseHeaders.Value); response.AddHeader(responseHeaders.Key, responseHeaders.Value); } } if (WriteToOutputStream(response.OutputStream, result)) { return(true); } if (httpResult != null) { result = httpResult.Response; } var responseText = result as string; if (responseText != null) { WriteTextToResponse(response, responseText, defaultContentType); return(true); } if (defaultAction == null) { throw new ArgumentNullException("defaultAction", string.Format( "As result '{0}' is not a supported responseType, a defaultAction must be supplied", result.GetType().Name)); } //ContentType='text/html' is the default for a HttpResponse //Do not override if another has been set if (response.ContentType == null || response.ContentType == ContentType.Html) { response.ContentType = defaultContentType; } defaultAction(serializerCtx, result, response.OutputStream); return(false); } catch (Exception ex) { var errorMessage = string.Format("Error occured while Processing Request: [{0}] {1}", ex.GetType().Name, ex.Message); Log.Error(errorMessage, ex); var operationName = result != null ? result.GetType().Name.Replace("Response", "") : "OperationName"; response.WriteErrorToResponse(defaultContentType, operationName, errorMessage, ex); return(true); } finally { response.Close(); } }
public void SetContentTypeSerializer(string contentType, StreamSerializerDelegate streamSerializer) { ContentTypeSerializers[ContentFormat.NormalizeContentType(contentType)] = streamSerializer; }
public void AddContentTypeSerializer(string contentType, StreamSerializerDelegate streamSerializer) { this.ContentTypeSerializers.Add(contentType, streamSerializer); }
/// <summary> /// Writes to response. /// /// Response headers are customizable by implementing IHasOptions an returning Dictionary of Http headers. /// /// </summary> /// <param name="response">The response.</param> /// <param name="result">Whether or not it was implicity handled by ServiceStack's built-in handlers.</param> /// <param name="defaultAction">The default action.</param> /// <param name="defaultContentType">Default response ContentType.</param> /// <returns></returns> public static bool WriteToResponse(this IHttpResponse response, object result, StreamSerializerDelegate defaultAction, string defaultContentType) { try { if (result == null) return true; foreach (var globalResponseHeader in EndpointHost.Config.GlobalResponseHeaders) { response.AddHeader(globalResponseHeader.Key, globalResponseHeader.Value); } var httpResult = result as IHttpResult; if (httpResult != null) { response.StatusCode = (int)httpResult.StatusCode; if (string.IsNullOrEmpty(httpResult.ContentType)) { httpResult.ContentType = defaultContentType; } response.ContentType = httpResult.ContentType; } /* Mono Error: Exception: Method not found: 'System.Web.HttpResponse.get_Headers' */ var responseOptions = result as IHasOptions; if (responseOptions != null) { //Reserving options with keys in the format 'xx.xxx' (No Http headers contain a '.' so its a safe restriction) const string reservedOptions = "."; foreach (var responseHeaders in responseOptions.Options) { if (responseHeaders.Key.Contains(reservedOptions)) continue; Log.DebugFormat("Setting Custom HTTP Header: {0}: {1}", responseHeaders.Key, responseHeaders.Value); response.AddHeader(responseHeaders.Key, responseHeaders.Value); } } if (WriteToOutputStream(response.OutputStream, result)) { return true; } var responseText = result as string; if (responseText != null) { WriteTextToResponse(response, responseText, defaultContentType); return true; } if (defaultAction == null) { throw new ArgumentNullException("defaultAction", string.Format( "As result '{0}' is not a supported responseType, a defaultAction must be supplied", result.GetType().Name)); } //ContentType='text/html' is the default for a HttpResponse //Do not override if another has been set if (response.ContentType == null || response.ContentType == ContentType.Html) { response.ContentType = defaultContentType; } defaultAction(result, response.OutputStream); return false; } catch (Exception ex) { var errorMessage = string.Format("Error occured while Processing Request: {0}", ex.Message); Log.Error(errorMessage, ex); var operationName = result != null ? result.GetType().Name.Replace("Response", "") : "OperationName"; response.WriteErrorToResponse(defaultContentType, operationName, errorMessage, ex); return true; } finally { //Both seem to throw an exception?? //Do not use response.Close(); does not have the same effect //response.End(); } }