Hi!
i'm on webapi 2, .net 4.51
i've installed version 5.0.1 via the package manager with no modifications, when i browse to [root]/swagger/docs/v1 i get the following json
{
"message": "An error has occurred.",
"exceptionMessage": "Invalid type owner for DynamicMethod.",
"exceptionType": "System.ArgumentException",
"stackTrace": " at System.Reflection.Emit.DynamicMethod.Init(String name, MethodAttributes attributes, CallingConventions callingConvention, Type returnType, Type[] signature, Type owner, Module m, Boolean skipVisibility, Boolean transparentMethod, StackCrawlMark& stackMark)\r\n at System.Reflection.Emit.DynamicMethod..ctor(String name, Type returnType, Type[] parameterTypes, Type owner, Boolean skipVisibility)\r\n at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDynamicMethod(String name, Type returnType, Type[] parameterTypes, Type owner)\r\n at Newtonsoft.Json.Utilities.DynamicReflectionDelegateFactory.CreateDefaultConstructorT\r\n at Newtonsoft.Json.Serialization.DefaultContractResolver.InitializeContract(JsonContract contract)\r\n at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateObjectContract(Type objectType)\r\n at Newtonsoft.Json.Serialization.DefaultContractResolver.CreateContract(Type objectType)\r\n at Newtonsoft.Json.Serialization.DefaultContractResolver.ResolveContract(Type type)\r\n at Swashbuckle.Swagger.SchemaRegistry.CreateInlineSchema(Type type, String refPrefix)\r\n at Swashbuckle.Swagger.SchemaRegistry.GetOrRegister(Type type)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreateParameter(ApiParameterDescription paramDesc, Boolean inPath, SchemaRegistry schemaRegistry)\r\n at System.Linq.Enumerable.WhereSelectEnumerableIterator2.MoveNext()\r\n at System.Collections.Generic.List1..ctor(IEnumerable1 collection)\r\n at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreateOperation(ApiDescription apiDescription, SchemaRegistry schemaRegistry)\r\n at Swashbuckle.Swagger.SwaggerGenerator.CreatePathItem(IEnumerable1 apiDescriptions, SchemaRegistry schemaRegistry)\r\n at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable1 source, Func2 keySelector, Func2 elementSelector, IEqualityComparer1 comparer)\r\n at Swashbuckle.Swagger.SwaggerGenerator.GetSwagger(String rootUrl, String apiVersion)\r\n at Swashbuckle.Application.SwaggerDocsHandler.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Net.Http.HttpMessageInvoker.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpRoutingDispatcher.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.Tracing.Tracers.RequestMessageHandlerTracer.<>c__DisplayClass4.<SendAsync>b__1()\r\n at System.Web.Http.Tracing.ITraceWriterExtensions.TraceBeginEndAsync[TResult](ITraceWriter traceWriter, HttpRequestMessage request, String category, TraceLevel level, String operatorName, String operationName, Action1 beginTrace, Func1 execute, Action2 endTrace, Action`1 errorTrace)\r\n at System.Web.Http.Tracing.Tracers.RequestMessageHandlerTracer.SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)\r\n at System.Web.Http.HttpServer.
}
i've read through all the documentation, so sorry if i've missed something... any clues as to what i'm doing wrong? please let me know if you need any more information, ta!
oh.. not sure if this matters, but i'm using newtonsoft.Json 6.0.4 too
This is surprising because it's actually the Newtonsoft ContractResolver that is failing for a certain type. Have you confirmed that this is only an issue when generating Swagger? I ask because the same method would likely be invoked by the JsonSerializer when calling a method on your API for that same type.
To investigate further, we really need to figure out the actual type that's causing this exception. Is there any chance you could pull, build from source (use build.bat) and debug locally?
it's so obvious now, thanks for that! there was some dodgy properties in my webapi base class, i just used [ApiExplorerSettings(IgnoreApi = true)] to hide them and all is good now.. thanks! :)
In my case I had a base class for the rest of my controllers and the logic was finding this. [ApiExplorerSettings(IgnoreApi = true)] resolved my issue but this is something worthwhile considering while you find the problem.
Nice to have would be a way to record the API call that is generating the error.
@qwertykeith [ApiExplorerSettings(IgnoreApi = true)] resolved my issue, thank you. As an aside/note for anyone else who might need this. If you put that on the base controller itself it will hide all children controllers (not good), but if you put it on the properties/methods itself it will show the children correctly.
Most helpful comment
In my case I had a base class for the rest of my controllers and the logic was finding this. [ApiExplorerSettings(IgnoreApi = true)] resolved my issue but this is something worthwhile considering while you find the problem.
Nice to have would be a way to record the API call that is generating the error.