Skip to content

Commit

Permalink
Merge pull request #1073 from filipw/http-reuse-jsonserializer
Browse files Browse the repository at this point in the history
reuse JsonSerializer in the HTTP server
  • Loading branch information
david-driscoll authored Jan 8, 2018
2 parents f224846 + d03997f commit 108dd1a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
8 changes: 4 additions & 4 deletions src/OmniSharp.Host/WorkspaceInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static void Initialize(
{
try
{
var projectConfiguration = configuration.GetSection((string)projectSystem.Key);
var projectConfiguration = configuration.GetSection(projectSystem.Key);
var enabledProjectFlag = projectConfiguration.GetValue<bool>("enabled", defaultValue: true);
if (enabledProjectFlag)
{
Expand All @@ -45,7 +45,7 @@ public static void Initialize(
{
var message = $"The project system '{projectSystem.GetType().FullName}' threw exception during initialization.";
// if a project system throws an unhandled exception it should not crash the entire server
LoggerExceptions.LogError((ILogger)logger, e, message);
logger.LogError(e, message);
}
}

Expand All @@ -61,7 +61,7 @@ public static void Initialize(
ProvideWorkspaceOptions(compositionHost, workspace, options, logger);
});

LoggerExtensions.LogInformation((ILogger)logger, "Configuration finished.");
logger.LogInformation("Configuration finished.");
}

private static void ProvideWorkspaceOptions(
Expand All @@ -83,7 +83,7 @@ private static void ProvideWorkspaceOptions(
catch (Exception e)
{
var message = $"The workspace options provider '{providerName}' threw exception during initialization.";
LoggerExceptions.LogError(logger, e, message);
logger.LogError(e, message);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/OmniSharp.Http/Middleware/EndpointMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public async Task Invoke(HttpContext httpContext)
Command = endpoint,
ArgumentsStream = httpContext.Request.Body
});
MiddlewareHelpers.WriteTo(httpContext.Response, response);

httpContext.Response.WriteJson(response);
return;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@

namespace OmniSharp.Http.Middleware
{
static class MiddlewareHelpers
static class MiddlewareExtensions
{
private static readonly Encoding _encoding = new System.Text.UTF8Encoding(false);
private static readonly JsonSerializer _jsonSerializer = JsonSerializer.Create();
private static readonly Encoding _encoding = new UTF8Encoding(false);

public static void WriteTo(HttpResponse response, object value)
public static void WriteJson(this HttpResponse response, object value)
{
using (var writer = new StreamWriter(response.Body, _encoding, 1024, true))
using (var jsonWriter = new JsonTextWriter(writer))
{
jsonWriter.CloseOutput = false;
var jsonSerializer = JsonSerializer.Create(/*TODO: SerializerSettings*/);
jsonSerializer.Serialize(jsonWriter, value);
_jsonSerializer.Serialize(jsonWriter, value);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/OmniSharp.Http/Middleware/StatusMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ public async Task Invoke(HttpContext httpContext)
var endpoint = httpContext.Request.Path.Value;
if (endpoint == OmniSharpEndpoints.CheckAliveStatus)
{
MiddlewareHelpers.WriteTo(httpContext.Response, true);
httpContext.Response.WriteJson(true);
return;
}

if (endpoint == OmniSharpEndpoints.CheckReadyStatus)
{
MiddlewareHelpers.WriteTo(httpContext.Response, _workspace.Initialized);
httpContext.Response.WriteJson(_workspace.Initialized);
return;
}
}
Expand Down
5 changes: 1 addition & 4 deletions src/OmniSharp.Http/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public void Configure(
IApplicationBuilder app,
IServiceProvider serviceProvider,
ILoggerFactory loggerFactory,
IEventEmitter eventEmitter,
ISharedTextWriter writer,
HttpEnvironment httpEnvironment,
IOptionsMonitor<OmniSharpOptions> options)
HttpEnvironment httpEnvironment)
{
var workspace = _compositionHost.GetExport<OmniSharpWorkspace>();
var logger = loggerFactory.CreateLogger<Startup>();
Expand Down

0 comments on commit 108dd1a

Please sign in to comment.