From 2578b8107f56d2715b32e96b5c7f1c1fb8225f6d Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 8 Oct 2014 13:50:13 -0700 Subject: [PATCH] Fix for #1277 - Add Options/Startup API for WebAPI shim Adds an options class, as well as a default options setup that will configure the default set of formatters. Currently most of what options needs to do is a placeholder, but it later do things like add ApplicationModelConventions, filters, formatters, model binders, etc. Those will be added in follow up items. --- .../WebApiCompatShimOptions.cs | 19 ++++++++++++ .../WebApiCompatShimOptionsSetup.cs | 30 +++++++++++++++++++ ...piCompatShimServiceCollectionExtensions.cs | 16 ++++++++++ .../WebApiCompatShimBasicTest.cs | 29 +++++++++++++++++- .../Controllers/BasicApiController.cs | 13 ++++++++ .../WebApiCompatShimWebSite/Startup.cs | 4 ++- 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptions.cs create mode 100644 src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs create mode 100644 src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimServiceCollectionExtensions.cs diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptions.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptions.cs new file mode 100644 index 0000000000..e498c22864 --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptions.cs @@ -0,0 +1,19 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Linq; +using System.Net.Http.Formatting; + +namespace Microsoft.AspNet.Mvc.WebApiCompatShim +{ + public class WebApiCompatShimOptions + { + public WebApiCompatShimOptions() + { + // Start with an empty collection, our options setup will add the default formatters. + Formatters = new MediaTypeFormatterCollection(Enumerable.Empty()); + } + + public MediaTypeFormatterCollection Formatters { get; set; } + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs new file mode 100644 index 0000000000..e2f837828b --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimOptionsSetup.cs @@ -0,0 +1,30 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System.Net.Http.Formatting; +using Microsoft.Framework.OptionsModel; + +namespace Microsoft.AspNet.Mvc.WebApiCompatShim +{ + public class WebApiCompatShimOptionsSetup : IOptionsAction, IOptionsAction + { + public int Order + { + // We want to run after the default MvcOptionsSetup. + get { return DefaultOrder.DefaultFrameworkSortOrder + 100; } + } + + public string Name { get; set; } + + public void Invoke(MvcOptions options) + { + // Placeholder + } + + public void Invoke(WebApiCompatShimOptions options) + { + // Add the default formatters + options.Formatters.AddRange(new MediaTypeFormatterCollection()); + } + } +} diff --git a/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimServiceCollectionExtensions.cs b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimServiceCollectionExtensions.cs new file mode 100644 index 0000000000..8ff52fb81a --- /dev/null +++ b/src/Microsoft.AspNet.Mvc.WebApiCompatShim/WebApiCompatShimServiceCollectionExtensions.cs @@ -0,0 +1,16 @@ +// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using Microsoft.AspNet.Mvc.WebApiCompatShim; + +namespace Microsoft.Framework.DependencyInjection +{ + public static class WebApiCompatShimServiceCollectionExtensions + { + public static IServiceCollection AddWebApiConventions(this IServiceCollection services) + { + services.AddOptionsAction(); + return services; + } + } +} diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs index ebc4129d2c..9069278dd4 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/WebApiCompatShimBasicTest.cs @@ -3,11 +3,13 @@ #if ASPNET50 using System; +using System.Net; +using System.Net.Http.Formatting; using System.Threading.Tasks; using Microsoft.AspNet.Builder; using Microsoft.AspNet.TestHost; +using Newtonsoft.Json; using Xunit; -using System.Net; namespace Microsoft.AspNet.Mvc.FunctionalTests { @@ -51,6 +53,31 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests "Visited: /BasicApi/GenerateUrl", content); } + + [Fact] + public async Task Options_SetsDefaultFormatters() + { + // Arrange + var server = TestServer.Create(_provider, _app); + var client = server.CreateClient(); + + var expected = new string[] + { + typeof(JsonMediaTypeFormatter).FullName, + typeof(XmlMediaTypeFormatter).FullName, + typeof(FormUrlEncodedMediaTypeFormatter).FullName, + }; + + // Act + var response = await client.GetAsync("http://localhost/BasicApi/GetFormatters"); + var content = await response.Content.ReadAsStringAsync(); + + var formatters = JsonConvert.DeserializeObject(content); + + // Assert + Assert.Equal(HttpStatusCode.OK, response.StatusCode); + Assert.Equal(expected, formatters); + } } } #endif \ No newline at end of file diff --git a/test/WebSites/WebApiCompatShimWebSite/Controllers/BasicApiController.cs b/test/WebSites/WebApiCompatShimWebSite/Controllers/BasicApiController.cs index 9dc05e5467..09478361be 100644 --- a/test/WebSites/WebApiCompatShimWebSite/Controllers/BasicApiController.cs +++ b/test/WebSites/WebApiCompatShimWebSite/Controllers/BasicApiController.cs @@ -1,15 +1,21 @@ // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. +using System.Linq; using System.Threading.Tasks; using System.Web.Http; using Microsoft.AspNet.Http; using Microsoft.AspNet.Mvc; +using Microsoft.AspNet.Mvc.WebApiCompatShim; +using Microsoft.Framework.OptionsModel; namespace WebApiCompatShimWebSite { public class BasicApiController : ApiController { + [Activate] + public IOptionsAccessor OptionsAccessor { get; set; } + // Verifies property activation [HttpGet] public async Task WriteToHttpContext() @@ -32,5 +38,12 @@ namespace WebApiCompatShimWebSite await Context.Response.WriteAsync(message); return new EmptyResult(); } + + // Verifies the default options configure formatters correctly. + [HttpGet] + public string[] GetFormatters() + { + return OptionsAccessor.Options.Formatters.Select(f => f.GetType().FullName).ToArray(); + } } } \ No newline at end of file diff --git a/test/WebSites/WebApiCompatShimWebSite/Startup.cs b/test/WebSites/WebApiCompatShimWebSite/Startup.cs index 284d184502..daa7f3fd7f 100644 --- a/test/WebSites/WebApiCompatShimWebSite/Startup.cs +++ b/test/WebSites/WebApiCompatShimWebSite/Startup.cs @@ -15,8 +15,10 @@ namespace WebApiCompatShimWebSite app.UsePerRequestServices(services => { services.AddMvc(configuration); - }); + services.AddWebApiConventions(); + }); + app.UseMvc(); } }