OWASP O2 Platform Blog

O2 Scripting – solving the missing references compilation problem (using FVDL as an example)

A common error when starting to write O2 scripts is the “The name ‘…’ does not exist in the current context” compilation problem. This tends to happen because the C# comments that provide the dll and namespace references were not used (since it can easy to assume that they are not needed 🙂  )

Here is how to replicate the problem and how to fix it.

Start with an new instance of an O2 Quick Development GUI

Right-click on the source code editor and chose the ‘show log view option)

Then (for example) paste the code below:

var topPanel = panel.clear().add_Panel();
var xmlFile = @"C:\O2\Fortify\test.fvdl";
var xsdFile = @"C:\O2\Fortify\test.fvdl.xsd";
var csharpFile = xsdFile.xsdCreateCSharpFile();
csharpFile.fileContents().insertBefore("//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line()).saveAs(csharpFile);
var fvdl = FVDL.Load(xmlFile);

… this will trigger the code compilation, which will fail with the error:

[8:24:04 AM] ERROR: [CSharp_FastCompiler] Compilation Error: 32::14::CS0103::The name ‘FVDL’ does not exist in the current context::c:\Users\o2\AppData\Local\Temp\meqvg7zx.0.cs

The reference that is missing is the C# file that has the FVDL class.

In fact, in this case, the script (sent to me by an O2 user who was following the code samples in the  Fortify FVDL files – Creating and consuming the schema and CSharp file blog post) is actually jumping a step (btw, see  here for more posts about O2 support for Fortify’s FVDL files)

Let’s comment the last line,  and make the code compile

Before we execute it, let’s change the script a little bit to create the XSD file and then return the value of the csharpFile variable (which is the C# file created that represents the XSD file)

var xmlFile = @"C:\O2\Demos\Fortify-Sate-2008\sate2008-Fvdl\naim.fvdl";
var xsdFile = xmlFile.xmlCreateXSD();
var csharpFile = xsdFile.xsdCreateCSharpFile();
return csharpFile.fileContents().insertBefore("//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll".line()).saveAs(csharpFile);

In this case (due to the format of the Fortify XSD), the XML -> XSD conversion using Linq2Xml will not work (see the post  Fortify FVDL files – Creating and consuming the schema and CSharp file for more details and for a solution using Visual Studio 2010):

To show how to fix the  “The name ‘…’ does not exist in the current context” problem, let go back to the orignal script, without the bits that are not needed (and using a file from the SATE 2008 project):

var topPanel = panel.clear().add_Panel();
var xmlFile = @"C:\O2\Demos\Fortify-Sate-2008\sate2008-Fvdl\naim.fvdl";
var fvdl = FVDL.Load(xmlFile);

To compile this we need to add two things:

1) a C#file or dll that has the FVDL class
2) an include reference to the namespace that contains the FVDL class:

In this case we are going to use the file that was created in the previous blog post example, which is now included in the local O2 Scripts folder:

To consume this file (and tell the O2 scripting engine to compile it with the current script), use this syntax:

//O2File:C:\O2\O2Scripts_Database\_Scripts\3rdParty_Tools\Fortify\Fortify.fvdl.1.6.xsd.cs

The compilation process now will show a lot more activity,  including an entry with ‘Compilated OK to: C:\O2\_tempDir\7-29-2011\tmp3A4C.tmp.dll‘  , which basically means that the files we provided as references compiled ok.

The reason we sill get the original error is because we are missing the namespace reference (altough you can already gain acccess to the imported code/dll Code Complete :

… and here is the FVDL class:

… which when  used as a reference using this syntax:

//using xmlns.www.fortifysoftware.com.schema.fvdl

… will result in this error:

 … which is also a very common error, and basically means that we also need to provide the C# compiler a reference to the O2_Misc_Microsoft_MPL_Libs.dll.

This can be done using this syntax:

//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll

which will make the following code

var xmlFile = @"C:\O2\Demos\Fortify-Sate-2008\sate2008-Fvdl\naim.fvdl";
var fvdl = FVDL.Load(xmlFile);</pre>
&nbsp;

//using xmlns.www.fortifysoftware.com.schema.fvdl
//O2File:C:\O2\O2Scripts_Database\_Scripts\3rdParty_Tools\Fortify\Fortify.fvdl.1.6.xsd.cs
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll

 

… to compile OK :

Note that since the Fortify.fvdl.1.6.xsd.cs file is included inside the O2 Scripts folder, we can use it just its filename:

var xmlFile = @"C:\O2\Demos\Fortify-Sate-2008\sate2008-Fvdl\naim.fvdl";
var fvdl = FVDL.Load(xmlFile);
 
//using xmlns.www.fortifysoftware.com.schema.fvdl
//O2File:Fortify.fvdl.1.6.xsd.cs
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll

To see the object created add a return statement:

…. (since we are inside the O2 Quick Development Gui and have access to a panel),  add a PropertyGrid to the provided Panel control and show the FVDL object there:

var topPanel = panel.clear().add_Panel();
var xmlFile = @"C:\O2\Demos\Fortify-Sate-2008\sate2008-Fvdl\naim.fvdl";
var fvdl = FVDL.Load(xmlFile);
topPanel.add_PropertyGrid().show(fvdl);
 
//using xmlns.www.fortifysoftware.com.schema.fvdl
//O2File:Fortify.fvdl.1.6.xsd.cs
//O2Ref:O2_Misc_Microsoft_MPL_Libs.dll</pre>
&nbsp;

 

And if want to make this into a stand alone tool (that can be shared with others and invoked directly from the windows explorer), replace with first line with a call to open a popup window

//var topPanel = panel.clear().add_Panel();   
var topPanel = "View FVDL file".popupWindow(500,370);

If you save this script as a stand alone tool, go to the script editor, right click on it, and chose the current source code ->  save As menu item

Chose a location:

and execute this script by double clicking on it:

You might have noticed that I saved this file on my local development box in one of the O2 Scripts folders.

This means that to add it to the O2 scripts that exist locally to O2 users (and that O2 automatically syncs up when the main GUI starts), I just need to do this:

and it is done:

 … by the time you are reading this blog post you should have this script on your local O2 Scripts folder

July 28, 2011 - Posted by | Fortify, O2 Internals

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: