question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unhandled Exception occurred. Unable to process the request. Could not create an instance of type Microsoft.AspNetCore.Http.IFormFile. Type is an interface or abstract class and cannot be instantiated.

See original GitHub issue

Describe the bug We are using Core 3.1 and we have a API to upload a file. Here is the how the endpoint looks like

[ProducesResponseType(typeof(ApiResponse<bool>), 200)]
       [HttpPost("File")]
       public async Task<IActionResult> AddFile(
           [FromHeader] string customerId,
           [FromForm] IFormFile file,
           CancellationToken token = default(CancellationToken))
       {
}

Auditing is enabled and when we consume this endpoint we are getting an exception This was working in Core 2.1, but as soon as this is converted to Core 3.1 it started giving exception for Audit.WebApi

The detailed exception is as follows

"ResponseException": {
        "Details": "   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewObject(JsonReader reader, JsonObjectContract objectContract, JsonProperty containerMember, JsonProperty containerProperty, String id, Boolean& createdFromNonDefaultCreator)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)\r\n   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)\r\n   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)\r\n   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)\r\n   at Audit.WebApi.AuditApiHelper.<>c.<SerializeParameters>b__2_1(KeyValuePair`2 v)\r\n   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector, IEqualityComparer`1 comparer)\r\n   at System.Linq.Enumerable.ToDictionary[TSource,TKey,TElement](IEnumerable`1 source, Func`2 keySelector, Func`2 elementSelector)\r\n   at Audit.WebApi.AuditApiHelper.SerializeParameters(IDictionary`2 parameters)\r\n   at Audit.WebApi.AuditApiAdapter.GetActionParameters(ControllerActionDescriptor actionDescriptor, IDictionary`2 actionArguments, Boolean serializeParams)\r\n   at Audit.WebApi.AuditApiAdapter.CreateOrUpdateAction(ActionExecutingContext actionContext, Boolean includeHeaders, Boolean includeRequestBody, Boolean serializeParams, String eventTypeName)\r\n   at Audit.WebApi.AuditApiAdapter.BeforeExecutingAsync(ActionExecutingContext actionContext, Boolean includeHeaders, Boolean includeRequestBody, Boolean serializeParams, String eventTypeName)\r\n   at Audit.WebApi.AuditApiAttribute.OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ControllerActionInvoker.<InvokeInnerFilterAsync>g__Awaited|13_0(ControllerActionInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)\r\n   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)\r\n   at Prozesscenter.Response.Decorate.Core.ResponseMiddleware.Invoke(HttpContext context)",
        "ExceptionMessage": "Unhandled Exception occurred. Unable to process the request. Could not create an instance of type Microsoft.AspNetCore.Http.IFormFile. Type is an interface or abstract class and cannot be instantiated. Path 'file.ContentDisposition', line 1, position 37."        
    }

To Reproduce Create an endpoint which has a parameter of type IFormFile and configure web Api auditing

Expected behavior There should not be any exception and the Audit API has to log the details.

Libraries (specify the Audit.NET extensions being used including version):

  • Audit.WebApi: 15.2.2
  • Audit.EntityFramework: 15.2.2

Target .NET framework: For example:

  • .NET Standard 2.1

Additional context If I add [AuditIgnore] the issue is not happening, this makes it clear that the issue is happening from AuditWebApi

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kiranchandrancommented, Jul 31, 2020

Can you share a minimal sample project code that fails?

Hey @thepirat000 Give me some time, I can create a sample and attach

0reactions
thepirat000commented, Aug 8, 2020

Closing assuming you are no longer having this issue. Otherwise please add the code to reproduce and I’ll reopen

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Why do I Get an "Could not create an instance of type ...
1 Answer 1 ... IFormFile is an interface. They can't be created without context. You are trying to use your form-encoded version of...
Read more >
Could not create an instance of type 'Microsoft.AspNetCore ...
An unhandled exception has occurred while executing the request. System.InvalidOperationException: Could not create an instance of type ...
Read more >
ASP.Net Core API Error (Unable to resolve service for type ' ...
I built a Web API using ASP.Net Core (from Microsoft Tutorial). When calling the API, I got this error below: The error can...
Read more >
Type is an interface or abstract class and cannot be ...
“”“Could not create an instance of type InstaSharper.API.IInstaApi. Type is an interface or abstract class and cannot be instantiated.
Read more >
Introduction to ASP.NET Core
Get started with ASP.NET Core and Entity Framework Core using Visual Studio ... An unhandled exception occurred while processing the request.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found