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.

Serialzation Exception when passing parameter from MVC Controller Action

See original GitHub issue

Im getting an exception on expressionSerializer.SerializeText() when trying to serialize an Expression, seemingly when it contains a reference from an MVC action controller. When assign a new local variable in its place with the same value, the serialization succeeds.

Exception Detail Error converting type: Type 'WhiteBox.WebApp.Helpers.ConnectorInstanceHelper+<>c__DisplayClass4_0' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. Alternatively, you can ensure that the type is public and has a parameterless constructor - all public members of the type will then be serialized, and no attributes will be required.

If I inspect the Expression while debugging, i can see the following difference between the local variable

.Constant<Whitebox.WebApp.Helpers.ConnectorInstanceHelper+<>c__DisplayClass4_0>(Whitebox.WebApp.Helpers.ConnectorInstanceHelper+<>c__DisplayClass4_0).productId

and the one being passed from the controller action. The difference being the CS$<>8__locals1 suffix.

.Constant<Whitebox.WebApp.Helpers.ConnectorInstanceHelper+<>c__DisplayClass4_1>(Whitebox.WebApp.Helpers.ConnectorInstanceHelper+<>c__DisplayClass4_1).CS$<>8__locals1).productId

-Visual Studio 2017 -Net CORE 2.2 -Microsoft.AspNetCore.MVC 2.2.0

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
gorillapowercommented, Apr 25, 2019

Hi Sascha,

For me the exception is caught and writes “Exception Thrown”. Does the code not enter the Catch()?

On Thu, 25 Apr 2019 at 14:30, Sascha Kiefer notifications@github.com wrote:

Is there something else, because if i run this in a console app, it runs without problems

[image: image] https://user-images.githubusercontent.com/65206/56735738-95428300-6766-11e9-841b-0a7e10b11ef9.png

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/esskar/Serialize.Linq/issues/113#issuecomment-486652444, or mute the thread https://github.com/notifications/unsubscribe-auth/ACDXXWMIIOX7DZKQFJTWJHLPSGP55ANCNFSM4HE2FZ4Q .

1reaction
gorillapowercommented, Apr 15, 2019

Hi @esskar, sorry for delayed response. I tried to create a test case where i can consistently reproduce the issue. It still seems to be related to the naming I see in the debug view of the expression, but ive narrowed it down it bit further.

If you initiate the code with await new TestClass().TestMethod("1231231"); the code within try-catch will throw an exception. The code after that executes without exception. If you move the `var userId = “12313”;’ outside the try-catch and similarily, if you remove the try-catch altogether (demonstrated by the subsequent code) the code executes fine. Its cases when the CS$<>8__locals1 is present in the expression that the serialization seems to fail.

I also noted that removing the the productId parameter from TestMethod results in no errors.

public class TestClass
    {
        public TestClass() { }

        public async Task TestMethod(string productId)
        {
            try
            {
                var userId = "12313";
                DoSerialization<ProductTest>(x => x.UserId == userId && x.ProductId == productId);
            }
            catch (Exception)
            {
                Trace.WriteLine("Exception thrown");
            }

            var userId2 = "12313";
            DoSerialization<ProductTest>(x => x.UserId == userId2 && x.ProductId == productId);
        }

        public static void DoSerialization<T>(Expression<Func<T, bool>> predicate)
        {
            var expressionSerializer = new ExpressionSerializer(new Serialize.Linq.Serializers.JsonSerializer());
            var queryJson = expressionSerializer.SerializeText(predicate);
        }
    }
    public class ProductTest
    {
        public string ProductId { get; set; }

        public string UserId { get; set; }
    }

    public class UserTest
    {
        public string Email { get; set; }
        public string UserId { get; set; }
    }

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - RedirectToAction to another Controller and passing ...
I believe the parameters are passed by name, and now he can't link "Error" ... public ActionResult YourAction() { // Here user object...
Read more >
How can I send the model from MVC Controller Action to ...
The Exception is related to Null Exception and It always return null. This is the page model but it doesn't receive any debugger....
Read more >
Exception Handling in ASP.NET Web API
An exception filter is executed when a controller method throws any unhandled exception that is not an HttpResponseException exception.
Read more >
From MVC to Minimal APIs with ASP.NET Core 6.0 - Ben Foster
Both MVC and Minimal APIs will automatically serialize your return types to the response body and return a HTTP 200 (OK) response, for...
Read more >
Serilzing MVC action result
When data size large, getting below error. System.InvalidOperationException: Error during serialization or deserialization using the JSON ...
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