Potential Bug in 6.0 C# Scripting inside SDK

Date : Wed, 27 Dec 2006 03:35:18 -0600
To : XSI(at)Softimage.COM
From : "Kent Bowling" <kentamanos(at)gmail.com>
Subject : Potential Bug in 6.0 C# Scripting inside SDK
I'm really stoked about the C# support (in reality, I would guess any .NET language could actually be used) in 6.0. I ran into a small problem playing around and just wanted to give a heads up for anyone playing around with C# scripting in XSI 6.0 (and trying to use any classes not in System.dll or System.Windows.Forms.dll).
 
One of the SDK examples is a C# scripting plugin. The plugin allows you to write scripting in C# inside XSI and run them from inside XSI. The SDK Documentation on this example notes the following:
 
"Note: The assemblies required by your C# code are not referenced automatically by the C# scripting plug-in. The C# compiler will issue an error if you are using an unknown assembly. The workaround is to add the reference manually to the C# scripting plug-in project. For instance, if you need to use System.Web in your code, you must add a reference to the References folder of CSharpScripting.csproj and recompile the project."
 
While adding the reference to the project usually allows "using" directives to succeed (never could get that to work for System.Xml, the first assembly I was trying to tinker with), using actual code from those assemblies doesn't work. For instance, if I add System.Web to the project (like mentioned above) and attempt to run this script:
 
using System;
using Softimage.XSIOM;
using System.Web;
 
class Script
{
    public static void Main()
    {
        CXSIApplicationClass xsi = new CXSIApplicationClass(); 

        xsi.LogMessage(System.Web.HttpUtility.UrlEncode("a b c"), siSeverity.siInfo) ;
    }
}

The plugin fails to compile the dynamic assembly properly, and outputs the following to XSI's logging window:
 
// ERROR : C# compilation failed:
// c:\Documents and Settings\Kent Bowling\Local Settings\Temp\XSI_Temp_2956\4fwzcolp.0.cs(11,29) : error CS0234: The type or namespace name 'HttpUtility' does not exist in the namespace ' System.Web' (are you missing an assembly reference?)
//
 
If I had not added the reference to the project by the way, it would have failed at the "using System.Web;" line, but still would fail when actually trying to use classes from System.Web. As I mentioned above, System.Xml never even worked on the "using" line by itself when adding the reference to the project.
 
Instead of adding the reference to the project, one workaround is to add it to the list of assemblies referenced by hand in the code. The code has the following section in it (around line 341):
 
public static string[] m_refAssemblies = new string[2]
{
    "System.dll",
    "System.Windows.Forms.dll"
};
 
If I change this to:
 
public static string[] m_refAssemblies = new string[3]
{
    "System.dll",
    "System.Windows.Forms.dll",
    "System.Web.dll"
};
 
Suddenly, the above script works, outputting:
 
// INFO : a+b+c
 
(which is just a URL encoding for "a SPACE b SPACE c")
 
A somewhat easy fix for this would obviously be changing the script a little so that the referenced assemblies are passed in and some configuration is added to specify them. Obviously that makes an SDK "example" a bit more complex (possibly too complex to be used as an example any more :)). I'll try to play around with this later and see what happens. This list could also be stored in a "standard" .NET config file for the assembly (foregoing the potentially confusing configuration UI).
 
Another solution (although not as flexible) would be to somehow determine which assemblies the CSharpScripting.dll assembly references and automatically adding those to the above array. This would cause the example to work as the documentation indicates.
 
Hope this helps, and let me know if anyone needs more information (and if there's a better place to report these types of things).
 
-Kent Bowling

Search the XSI List archives here or use the advanced search form to search across mailing lists. Searching help is available.
This site supposedly brought to you by Benjamin Grosser and the Imaging Technology Group.