Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to serialize instance of 'System.IO.FileInfo'. #2208

Open
lheinold opened this issue Oct 30, 2019 · 1 comment
Open

Unable to serialize instance of 'System.IO.FileInfo'. #2208

lheinold opened this issue Oct 30, 2019 · 1 comment

Comments

@lheinold
Copy link

lheinold commented Oct 30, 2019

Source/destination types

System.IO.FileInfo

Expected behavior

FileInfo to be serialized into a json properly. Json schema generated by swagger:

{
  "length": 0,
  "directoryName": "string",
  "directory": {
    "fullName": "string",
    "extension": "string",
    "name": "string",
    "exists": true,
    "creationTime": "2019-10-30T09:01:36.597Z",
    "creationTimeUtc": "2019-10-30T09:01:36.597Z",
    "lastAccessTime": "2019-10-30T09:01:36.597Z",
    "lastAccessTimeUtc": "2019-10-30T09:01:36.597Z",
    "lastWriteTime": "2019-10-30T09:01:36.597Z",
    "lastWriteTimeUtc": "2019-10-30T09:01:36.597Z",
    "attributes": "ReadOnly"
  },
  "isReadOnly": true,
  "fullName": "string",
  "extension": "string",
  "name": "string",
  "exists": true,
  "creationTime": "2019-10-30T09:01:36.597Z",
  "creationTimeUtc": "2019-10-30T09:01:36.597Z",
  "lastAccessTime": "2019-10-30T09:01:36.597Z",
  "lastAccessTimeUtc": "2019-10-30T09:01:36.597Z",
  "lastWriteTime": "2019-10-30T09:01:36.597Z",
  "lastWriteTimeUtc": "2019-10-30T09:01:36.597Z",
  "attributes": "ReadOnly"
}

Actual behavior

An error was thrown with no additional info. Visual studio refuses to break on the exception (not sure why, I'm looking into it)

Steps to reproduce

Create a controller action in blazor project that returns a file info like:

public async Task<ActionResult<FileInfo>> ...

It is caught by the exception handler middleware, with the only text of the exception 'Unable to serialize instance of 'System.IO.FileInfo'. I can't seem to find any more information on why the serialization failed.

Full exception from output window:

Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware: Error: An unhandled exception has occurred while executing the request.

Newtonsoft.Json.JsonSerializationException: Unable to serialize instance of 'System.IO.FileInfo'.
   at Newtonsoft.Json.Serialization.DefaultContractResolver.ThrowUnableToSerializeError(Object o, StreamingContext context)
   at Newtonsoft.Json.Serialization.JsonContract.InvokeOnSerializing(Object o, StreamingContext context)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.OnSerializing(JsonWriter writer, JsonContract contract, Object value)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeObject(JsonWriter writer, Object value, JsonObjectContract contract, JsonProperty member, JsonContainerContract collectionContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.SerializeValue(JsonWriter writer, Object value, JsonContract valueContract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerProperty)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalWriter.Serialize(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.SerializeInternal(JsonWriter jsonWriter, Object value, Type objectType)
   at Newtonsoft.Json.JsonSerializer.Serialize(JsonWriter jsonWriter, Object value)
   at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Formatters.NewtonsoftJsonOutputFormatter.WriteResponseBodyAsync(OutputFormatterWriteContext context, Encoding selectedEncoding)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResultFilterAsync>g__Awaited|29_0[TFilter,TFilterAsync](ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResultExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.ResultNext[TFilter,TFilterAsync](State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeResultFilters()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeNextResourceFilter>g__Awaited|24_0(ResourceInvoker invoker, Task lastTask, State next, Scope scope, Object state, Boolean isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Rethrow(ResourceExecutedContextSealed context)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.Next(State& next, Scope& scope, Object& state, Boolean& isCompleted)
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.InvokeFilterPipelineAsync()
--- End of stack trace from previous location where exception was thrown ---
   at Microsoft.AspNetCore.Mvc.Infrastructure.ResourceInvoker.<InvokeAsync>g__Logged|17_1(ResourceInvoker invoker)
   at Microsoft.AspNetCore.Routing.EndpointMiddleware.<Invoke>g__AwaitRequestTask|6_0(Endpoint endpoint, Task requestTask, ILogger logger)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Diagnostics.ExceptionHandlerMiddleware.<Invoke>g__Awaited|6_0(ExceptionHandlerMiddleware middleware, HttpContext context, Task task)

I just found: #1541 . Any workaround? Would also be helpful to make the error more specific (like Newtonsoft doesn't support serializing...) because it seems like the error is thrown specifically for FileInfo/DirectoryInfo types.

@dbc2
Copy link

dbc2 commented Nov 4, 2019

FileInfo in .Net Core cannot be serialized successfully by Json.NET because, in .Net Core only, it implements ISerializable but is not marked as [Serializable]. (In fact the docs are wrong about this. They indicate it is so marked, but the source code shows it's not.) In comparison, in the .Net framework it is both marked as [Serializable] and implements ISerializable and thus can be round-tripped successfully by Json.NET there.

For a further discussion and a workaround see ASP.NET Core serialized object with a FileInfo returns incomplete JSON.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants