question

MartinMarchant-9826 avatar image
0 Votes"
MartinMarchant-9826 asked BrandoZhang-MSFT edited

JSonConverter Write Method writing to Utf8JsonWriter object terminates application

I am creating a pass through application, which receives a json object, does some processing and
passes the data to a remote system, my issue is:

devenv.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception

let me try and explain:

I have a base class that implements an interface:

 public abstract class SliderModelBase : ISliderConfiguration
 {
     [JsonProperty("sliderType")]
     public SliderStyle SlideType { get; set; } = SliderStyle.Range;**

     [JsonProperty("systemDetail")]
     public string SystemDetail { get; set; } = string.Empty;

     [JsonProperty("pigment")]
     public string Pigment { get; set; } = string.Empty;

...more properties
}


there are additional properties based on the value of the property [SlideType]... for example:
I have implemented these different options as different classes:

 public class SliderRangeModel : SliderModelBase
 {
     [JsonProperty("rangeMin")]
     public decimal RangeMin { get; set; }

     [JsonProperty("rangeMax")]
     public decimal RangeMax { get; set; }

...}

public class SliderTextModel : SliderModelBase
{
[JsonProperty("value")]
public string Value { get; set; } = string.Empty;

     [JsonProperty("referenceValue")]
     public string ReferenceValue { get; set; } = string.Empty;

...}
...(there are more implementations)


These different implementations of the ISliderConfiguration interface are stored in a
list contained in the business domain model, a similar list exists for ISliderValue 's
here is the final model:

namespace Xyz.Models.Matches
{
using Newtonsoft.Json;

 [Serializable]
 public class WorkflowOptionCollection
 {
     [JsonProperty("sliderConfiguratons")]
     public List<ISliderConfiguration> SliderConfigurations { get; set; } = new List<ISliderConfiguration>();

     [JsonProperty("sliderValues")]
     public List<ISliderValue> SliderValues { get; set; } = new List<ISliderValue>();
 }

}


so I have created a custom JsonConverter, the object is deSerialised without an issue...
however I also have a requirement to pass the same model as a response..


public class WorkflowOptionJsonConverter : JsonConverter<WorkflowOptionCollection>

....

         public override void Write(Utf8JsonWriter writer, WorkflowOptionCollection value, JsonSerializerOptions options)
         {
             JsonSerializer.Serialize(writer, value, value.GetType(), options);
         }


The following is logged into the application event log when the Write method is called, the solution exists without
triggering middleware error handling:



Application: devenv.exe Framework Version: v4.0.30319 Description: The process was terminated due to an unhandled exception. Exception Info: System.IO.FileNotFoundException at MS.Internal.Text.TextInterface.Native.Util.ConvertHresultToException(Int32) at MS.Internal.Text.TextInterface.FontFace.GetDisplayGlyphMetrics(UInt16*, UInt32, MS.Internal.Text.TextInterface.GlyphMetrics*, Single, Boolean, Boolean, Single) at System.Windows.Media.GlyphTypeface.GlyphMetrics(UInt16*, Int32, MS.Internal.Text.TextInterface.GlyphMetrics*, Double, Single, System.Windows.Media.TextFormattingMode, Boolean) at System.Windows.Media.GlyphTypeface.GetGlyphMetricsAndIndicesOptimized(UInt32*, Int32, Double, Single, UInt16[], MS.Internal.Text.TextInterface.GlyphMetrics[], System.Windows.Media.TextFormattingMode, Boolean) at System.Windows.Media.GlyphTypeface.GetGlyphMetricsOptimized(System.Windows.Media.TextFormatting.CharacterBufferRange, Double, Single, UInt16[], MS.Internal.Text.TextInterface.GlyphMetrics[], System.Windows.Media.TextFormattingMode, Boolean) at System.Windows.Media.Typeface.CheckFastPathNominalGlyphs(System.Windows.Media.TextFormatting.CharacterBufferRange, Double, Single, Double, Double, Boolean, Boolean, System.Globalization.CultureInfo, System.Windows.Media.TextFormattingMode, Boolean, Boolean, Int32 ByRef) at MS.Internal.TextFormatting.SimpleRun.CreateSimpleTextRun(System.Windows.Media.TextFormatting.CharacterBufferRange, System.Windows.Media.TextFormatting.TextRun, MS.Internal.TextFormatting.TextFormatterImp, Int32, Boolean, Boolean, Double) at MS.Internal.TextFormatting.SimpleRun.Create(MS.Internal.TextFormatting.FormatSettings, System.Windows.Media.TextFormatting.CharacterBufferRange, System.Windows.Media.TextFormatting.TextRun, Int32, Int32, Int32, Int32, Int32, Double) at MS.Internal.TextFormatting.SimpleTextLine.Create(MS.Internal.TextFormatting.FormatSettings, Int32, Int32, Double) at MS.Internal.TextFormatting.TextFormatterImp.FormatLineInternal(System.Windows.Media.TextFormatting.TextSource, Int32, Int32, Double, System.Windows.Media.TextFormatting.TextParagraphProperties, System.Windows.Media.TextFormatting.TextLineBreak, System.Windows.Media.TextFormatting.TextRunCache) at MS.Internal.TextFormatting.TextFormatterImp.FormatLine(System.Windows.Media.TextFormatting.TextSource, Int32, Double, System.Windows.Media.TextFormatting.TextParagraphProperties, System.Windows.Media.TextFormatting.TextLineBreak, System.Windows.Media.TextFormatting.TextRunCache) at System.Windows.Controls.TextBlock.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.StackPanel.StackMeasureHelper(System.Windows.Controls.IStackMeasure, System.Windows.Controls.IStackMeasureScrollData, System.Windows.Size) at System.Windows.Controls.StackPanel.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ItemsPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Control.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.StackPanel.StackMeasureHelper(System.Windows.Controls.IStackMeasure, System.Windows.Controls.IStackMeasureScrollData, System.Windows.Size) at System.Windows.Controls.StackPanel.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ItemsPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Control.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at MS.Internal.Helper.MeasureElementWithSingleChild(System.Windows.UIElement, System.Windows.Size) at System.Windows.Controls.ContentPresenter.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Border.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Control.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Documents.AdornerDecorator.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Decorator.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.Controls.Primitives.PopupRoot.MeasureOverride(System.Windows.Size) at System.Windows.FrameworkElement.MeasureCore(System.Windows.Size) at System.Windows.UIElement.Measure(System.Windows.Size) at System.Windows.ContextLayoutManager.UpdateLayout() at System.Windows.ContextLayoutManager.UpdateLayoutCallback(System.Object) at System.Windows.Media.MediaContext.FireInvokeOnRenderCallbacks() at System.Windows.Media.MediaContext.RenderMessageHandlerCore(System.Object) at System.Windows.Media.MediaContext.RenderMessageHandler(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(System.Object) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean) at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object) at MS.Internal.CulturePreservingExecutionContext.Run(MS.Internal.CulturePreservingExecutionContext, System.Threading.ContextCallback, System.Object) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)

dotnet-csharpdotnet-aspnet-core-generaldotnet-runtime
· 5
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@MartinMarchant-9826, Welcome to Microsoft Q&A, I try to reproduce your problem, but failed. Could you provide the related about ISliderConfiguration and ISliderValue the two interfaces?

0 Votes 0 ·

Sure, I was going to give some more detail and context
however I had a little experiment and made a discovery

from my controller, this serialization works:

     [HttpPost("WorkFlow")]
     public WorkflowOptionCollection GenerateWorkflowRecommendations(WorkflowOptionCollection userWorkflow)
     {
         // testing only
         var serializedWorkflow = JsonSerializer.Serialize(userWorkflow);


but when returned as a result, it fails...

     [HttpPost("WorkFlow")]
     public WorkflowOptionCollection GenerateWorkflowRecommendations(WorkflowOptionCollection userWorkflow)
     {
         // testing only
         return userWorkflow;

         //return await matchService.GenerateWorkflowRecommendations(userWorkflow);
     }

do you still need the interface details?

0 Votes 0 ·
JackJJun-MSFT avatar image JackJJun-MSFT MartinMarchant-9826 ·

@MartinMarchant-9826, thanks for giving the above information, I noted that you used controller. Then what is your current project type, is asp.net mvc project or asp.net core mvc project?

0 Votes 0 ·
Show more comments

0 Answers