Skip to content

Instantly share code, notes, and snippets.

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="AzureCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2015-04.2.6">
<WorkerRole name="WorkerRole" vmsize="Standard_D1_v2">
<ConfigurationSettings>
<Setting name="foo" />
</ConfigurationSettings>
<Runtime executionContext="limited">
<EntryPoint>
<ProgramEntryPoint commandLine="NetCoreConsoleApp.exe" setReadyOnProcessStart="true" />
</EntryPoint>
dotnet publish -r win-x64 -c Release /p:PublishSingleFile=true -o bin/publish
using Microsoft.WindowsAzure.ServiceRuntime.Internal;
using System;
using System.Diagnostics;
using System.Threading;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
private class StartupWithEmptyContentTypeFormatter
{
public void ConfigureServices(IServiceCollection services)
{
_ = services ?? throw new ArgumentNullException(nameof(services));
services
.AddMvc(o => { o.AddEmptyContentTypeFormatter(); })
.WithController<TestController>();
}
public static class MvcApplicationPartManagerExtensions
{
public static IMvcBuilder WithController<T>(this IMvcBuilder builder)
{
return builder.ConfigureApplicationPartManager(m =>
{
m.FeatureProviders.Add(new CustomControllerFeatureProvider(typeof(T).GetTypeInfo()));
});
}
namespace Microsoft.AspNetCore.Mvc.Controllers
{
/// <summary>
/// Discovers controllers from a list of <see cref="ApplicationPart"/> instances.
/// </summary>
public class ControllerFeatureProvider : IApplicationFeatureProvider<ControllerFeature>
{
// ....
}
}
[Test]
public async Task Controller_ShouldReturn200WithJsonOutput_IfNoContentTypeIsProvided()
{
// arrange
var builder = new WebHostBuilder()
.UseStartup<StartupWithEmptyContentTypeFormatter>();
var server = new TestServer(builder);
var client = server.CreateClient();
private class TestController : Controller
{
[HttpPost("/200")]
public IActionResult HandlePost200([FromBody]InputRequestData data)
{
return Json(data);
}
}
private class InputRequestData
private class StartupWithEmptyContentTypeFormatter
{
public void ConfigureServices(IServiceCollection services)
{
_ = services ?? throw new ArgumentNullException(nameof(services));
services
.AddMvc(o => { o.AddEmptyContentTypeFormatter(); });
}
public class EmptyContentTypeJsonInputFormatter : InputFormatter
{
private readonly JsonInputFormatter _inner;
public EmptyContentTypeJsonInputFormatter(JsonInputFormatter inner)
{
_inner = inner ?? throw new ArgumentNullException(nameof(inner));
}
public override bool CanRead(InputFormatterContext context)