Skip to content

InvalidOperationException when using the API Explorer with MediaTypeApiVersionReader #156

Closed
@gareth-horstmann

Description

@gareth-horstmann

I'm trying to use the API explorer to show multiple API versions in swagger and am following the sample app but am getting an exception about the JQueryMvcFormUrlEncodedFormatter not implementing IClonable. I replicated the issue in the SwaggerWebApiSample sample app by setting the MediaTypeApiVersionReader on the ApiVersionReader property on the ApiVersioningOptions object. Here is the exception details:

System.InvalidOperationException occurred HResult=0x80131509 Message=The media type formatter JQueryMvcFormUrlEncodedFormatter could not be cloned. The type must either implement ICloneable or define a copy constructor with the signature 'JQueryMvcFormUrlEncodedFormatter(JQueryMvcFormUrlEncodedFormatter formatter)'. Source=Microsoft.AspNet.WebApi.Versioning.ApiExplorer StackTrace: at System.Web.Http.MediaTypeFormatterAdapterFactory.ResolveConstructor(Type type) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\System.Web.Http\MediaTypeFormatterAdapterFactory.cs:line 89 at System.Web.Http.MediaTypeFormatterAdapterFactory.NewActivator(Type type) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\System.Web.Http\MediaTypeFormatterAdapterFactory.cs:line 54 at System.Web.Http.MediaTypeFormatterAdapterFactory.NewCloneFunction(Type type) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\System.Web.Http\MediaTypeFormatterAdapterFactory.cs:line 45 at System.Collections.Concurrent.ConcurrentDictionary2.GetOrAdd(TKey key, Func2 valueFactory) at System.Web.Http.MediaTypeFormatterAdapterFactory.GetOrCreateCloneFunction(MediaTypeFormatter formatter) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\System.Web.Http\MediaTypeFormatterAdapterFactory.cs:line 29 at System.Web.Http.MediaTypeFormatterExtensions.Clone(MediaTypeFormatter formatter) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\System.Web.Http\MediaTypeFormatterExtensions.cs:line 13 at Microsoft.Web.Http.Description.ApiVersionParameterDescriptionContext.CloneFormattersAndAddMediaTypeParameter(NameValueHeaderValue parameter, ICollection1 formatters) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\ApiVersionParameterDescriptionContext.cs:line 221
at Microsoft.Web.Http.Description.ApiVersionParameterDescriptionContext.AddMediaTypeParameter(String name) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\ApiVersionParameterDescriptionContext.cs:line 159
at Microsoft.Web.Http.Description.ApiVersionParameterDescriptionContext.AddParameter(String name, ApiVersionParameterLocation location) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\ApiVersionParameterDescriptionContext.cs:line 93
at Microsoft.Web.Http.Versioning.MediaTypeApiVersionReader.AddParmeters(IApiVersionParameterDescriptionContext context) in C:\Work\External\aspnet-api-versioning\src\Common\Versioning\MediaTypeApiVersionReader.cs:line 129
at Microsoft.Web.Http.Description.VersionedApiExplorer.PopulateApiVersionParameters(ApiDescription apiDescription, ApiVersion apiVersion) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\VersionedApiExplorer.cs:line 641
at Microsoft.Web.Http.Description.VersionedApiExplorer.PopulateActionDescriptions(HttpActionDescriptor actionDescriptor, IHttpRoute route, String localPath, Collection1 apiDescriptions, ApiVersion apiVersion) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\VersionedApiExplorer.cs:line 777 at Microsoft.Web.Http.Description.VersionedApiExplorer.ExploreDirectRouteControllers(HttpControllerDescriptor controllerDescriptor, IReadOnlyList1 candidateActionDescriptors, IHttpRoute route, ApiVersion apiVersion) in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\VersionedApiExplorer.cs:line 568
at Microsoft.Web.Http.Description.VersionedApiExplorer.InitializeApiDescriptions() in C:\Work\External\aspnet-api-versioning\src\Microsoft.AspNet.WebApi.Versioning.ApiExplorer\Description\VersionedApiExplorer.cs:line 228
at System.Lazy1.CreateValue()

Probably also worth mentioning that I'm not doing anything special with the formatters, the JQueryMvcFormUrlEncodedFormatter is added by default to handle form-url-encoded requests

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions