Property InvalidDataException on instantiation

222 views Asked by At

I'm using the Rustici Scorm Cloud API to generate a URL to preview some learning material:

The code creates a LaunchLinkRequestSchema object with the string field RedirectOnExitUrl populated. However, I get an InvalidDataException at the point of instantiation:

Code

var l = new LaunchLinkRequestSchema() 
{
    RedirectOnExitUrl = "https://www.example.com"
};

Exception

System.IO.InvalidDataException   HResult=0x80131501  
Message=RedirectOnExitUrl is a required property for  LaunchLinkRequestSchema and cannot be null  
Source=Com.RusticiSoftware.Cloud.V2   
StackTrace:
at Com.RusticiSoftware.Cloud.V2.Model.LaunchLinkRequestSchema..ctor(Nullable`1 Expiry, String RedirectOnExitUrl, Nullable`1 Tracking, String StartSco, String Culture, String CssUrl, List`1 LearnerTags, List`1 CourseTags, List`1 RegistrationTags, List`1 Additionalvalues)     
at ScormAPI_Tests.Program.Main() in
C:\...\Program.cs:line 26

enter image description here

enter image description here

I don't understand why I see this error, when the property is being given a value. Can anybody please explain what's happening here?

1

There are 1 answers

1
GazTheDestroyer On BEST ANSWER

The error is being thrown by the constructor

at Com.RusticiSoftware.Cloud.V2.Model.LaunchLinkRequestSchema..ctor

But you are supplying the value after construction via C#'s object initializers.

Object initializers are just syntactic sugar. The object is constructed first, and then they are set. It's exactly the same as doing:

var l = new LaunchLinkRequestSchema();
l.RedirectOnExitUrl = "https://www.example.com";

You need to supply the parameter in the constructor itself, it looks to be the second parameter from the exception.

eg

var l = new LaunchLinkRequestSchema(null, "https://www.example.com");