OPCFoundation / UA-ModelCompiler

ModelCompiler converts XML files into C# and ANSI C
MIT License
150 stars 93 forks source link

Custom struct throwing NotSupportedException on getting JsonEncodingId #185

Open OlivierVerhaegen opened 1 month ago

OlivierVerhaegen commented 1 month ago

Hi All,

I noticed that the JsonEncodingId currently throws an NotSupportedException although the class implements IJsonEncodeable. The class was generated from a custom struct in the NodeSet2.xml model. This happens when running Server.Factory.AddEncodeableTypes(...).

Can someone explain why this is implemented this way and why custom structs are not supported for JSON encoding?

image

image

Thanks already for the information.

opcfoundation-org commented 1 month ago

If the JsonEncodingIds are not defined in the NodeSet then it can't be implemented. The JsonEncodingIds are optional in 1.05.03 and removed from 1.05.4. The DataTypeId is used instead.

OlivierVerhaegen commented 1 month ago

Hi thank you for the quick response!

I'm currently using Model version 1.4.10 so that would explain why no JsonEncodingIds are available? How should I implement this, just by updating to 1.05.4?

opcfoundation-org commented 1 month ago

You not get an exception in AddEncodeableTypes since there is a try catch block to suppress this error.

Can you explain exactly where the exception is coming from?

OlivierVerhaegen commented 1 month ago

I cannot see the insides of AddEncodeableTypes but I only get the exception when running in debug mode.

opcfoundation-org commented 1 month ago

Code is here: https://github.com/OPCFoundation/UA-.NETStandard/blob/master/Stack/Opc.Ua.Core/Types/Encoders/EncodeableFactory.cs

Do you have some setting enabled to stop on exception? This can cause the debugger to halt even if an exception is handled.

OlivierVerhaegen commented 1 month ago

This seems that the exception is categorized as "User-Unhandled". When I disable the breakpoint optionf for these kind of exceptions the code runs but weirdly I don't get the error logs defined in the code you mentioned above. I only get these logs: image

opcfoundation-org commented 1 month ago

opcua.dll means you are using someone else's code base. where did you get the dll from?

OlivierVerhaegen commented 1 month ago

I'm just compiling my own project using .NET core on Linux and removed a part of the project name, hence why you only see opcua.dll in the name

opcfoundation-org commented 1 month ago

You should not get an error. AddEncodeableTypes suppresses the NotImplementedException and continues.

OlivierVerhaegen commented 1 month ago

Yeah I know I've read the code (not only master but also specific to the version I'm using). I'm using the following nuget package: <PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.78" />

.Net Core version 8.0.303 on Ubuntu 20.04.3 LTS