Problems with Litium & Microsoft.AspNet.WebApi.Versioning & Swagger

I’m looking into adding versioning to a web api hosted in a Litium application. The Microsoft.AspNet.WebApi.Versioning library seems like it might work for us, but when I add it and try to load the Swagger url I get an error message “ValueFactory attempted to access the Value property of this instance.” caused by a System.InvalidOperationException.

The sample at aspnet-api-versioning/samples/webapi/SwaggerWebApiSample at master · microsoft/aspnet-api-versioning · GitHub works fine for me so I wonder if there’s something that’s not compatible with Litium? I see that there is an implementation of IApiExplorer, Litium.Web.WebApi.Description.LitiumApiExplorer, could that interfere somehow?

Any ideas?

This is the stack trace:

 at System.Lazy`1.CreateValue()
 at System.Lazy`1.LazyInitValue()
 at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerMapping() in /_/src/Microsoft.AspNet.WebApi.Versioning/Dispatcher/ApiVersionControllerSelector.cs:line 49
 at System.Web.Http.Routing.AttributeRoutingMapper.AddRouteEntries(SubRouteCollection collector, HttpConfiguration configuration, IInlineConstraintResolver constraintResolver, IDirectRouteProvider directRouteProvider)
 at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_1.<MapAttributeRoutes>b__1()
 at System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer)
 at System.Web.Http.Routing.AttributeRoutingMapper.<>c__DisplayClass1_0.<MapAttributeRoutes>b__0(HttpConfiguration config)
 at System.Web.Http.HttpConfiguration.ApplyControllerSettings(HttpControllerSettings settings, HttpConfiguration configuration)
 at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
 at System.Web.Http.Controllers.HttpControllerDescriptor.InvokeAttributesOnControllerType(HttpControllerDescriptor controllerDescriptor, Type type)
 at System.Web.Http.Controllers.HttpControllerDescriptor..ctor(HttpConfiguration configuration, String controllerName, Type controllerType)
 at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.InitializeControllerInfoCache() in /_/src/Microsoft.AspNet.WebApi.Versioning/Dispatcher/ApiVersionControllerSelector.cs:line 173
 at System.Lazy`1.CreateValue()
 at System.Lazy`1.LazyInitValue()
 at Microsoft.Web.Http.Dispatcher.ApiVersionControllerSelector.GetControllerMapping() in /_/src/Microsoft.AspNet.WebApi.Versioning/Dispatcher/ApiVersionControllerSelector.cs:line 49
 at System.Web.Http.Description.ApiExplorer.InitializeApiDescriptions()
 at System.Lazy`1.CreateValue()
--- End of stack trace from previous location where exception was thrown ---
 at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 at System.Lazy`1.get_Value()
 at Swashbuckle.Swagger.SwaggerGenerator.GetApiDescriptionsFor(String apiVersion)
 at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)
 at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 at System.Net.Http.DelegatingHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
 at System.Web.Http.HttpServer.<SendAsync>d__24.MoveNext()

Litium version: 7.6.2

Litium have it’s own implementation of multiple version support.

To make an WebAPI visible in the Swagger that is included with Litium you implement the IApiCollection (see the SiteOpenApiCollection in accelerator).

To make the API support multiple version, use the IApiVersionCollection instead and define the version with either ApiVersion or ApiVersionLimiter attribute on the controller or specific method that are included in this api collection.

Thanks, Patric. In this case we don’t use Litium’s Swagger, we have our own config and url. Are you suggesting that we should be able to use Litium’s implemention of versioning anyway? I kind of like the approach of ASP.NET API Versioning so I’m curious if you know why it possibly won’t work here.

The ASP.NET API Versioning that you try to use is not compatible with what we have in Litium and will make the Litium API to not work as intended.

All right, thanks for clarifying!

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.