Loading a Language Definition File - SecurityException

SyntaxEditor for Silverlight Forum

Posted 14 years ago by Christel
Version: 10.2.0105
Avatar
I created a custom.langdef file, saved it in a local directory and tried to load it using the SyntaxEditorHelper class of the SampleBrowser-CSharp2010.
I don't want to use it as a resource as the end user would like to modify it.
When I use the example in the documentation

(SyntaxLanguageDefinitionSerializer serializer = new SyntaxLanguageDefinitionSerializer();
string path = @"C:\ECMAScript.langdef";
ISyntaxLanguage language = serializer.LoadFromFile(path);)

or the SyntaxEditorHelper class,
both with describing the path as @"C:\TestLanguage\custom.langdef"
I get a System.SecurityException "Access to Path not allowed".
When I try to openFileDialog, even debugging the sample in the sample browser, I got a SecurityException as well, that a dialog must be prepared by user....
How can I load the langdef as a File wihout Security Exception?

Thanks for the appreciatet help!

Comments (3)

Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Christel,

Silverlight is very security sensitive. You are right, you must be triggering open file dialogs in the context of a mouse click or keyboard event. If they are called any other time or even dispatched from within a mouse/keyboard event, they will fail with the "must be prepared by user" exception.

For this reason you'll notice that in our SDI Editor demo on the Languages menu where you can click the load custom language item, we have this:
<shared:MenuItem Header="Custom..." Click="OnLanguageMenuItemClick" AllowClickBeginInvoke="False" />

Normally our MenuItems invoke the handlers so that the menu can close before any handlers execute. However we turn that feature off for handlers that will open Windows dialogs.

Then just look at our code that loads the langdef from a Stream:
// Open a language definition
using (Stream stream = dialog.File.OpenRead()) {
    // Read the file
    SyntaxLanguageDefinitionSerializer serializer = new SyntaxLanguageDefinitionSerializer();
    this.LoadLanguage(serializer.LoadFromStream(stream));
}
That will work fine with Silverlight's security, and you can see it working in our SDI Editor demo. Note that adding debug breakpoints in VS before some of that code could reintroduce the "prepared by user" exception, so don't add breakpoints in there.


Actipro Software Support

Posted 14 years ago by Christel
Avatar
Ok, that provides really useful news.
I didn't have the opportuity to test that today, but I will do that as soon as possible.
Great thanks for this helpful news!!
But what I'm more interested in is, how can I load a langdef File from a directory of the end user without an OpenFileDialog and without loading it as an embedded resource?
The end user will modify it and it should be loaded without compiling the application after changing the langdef File. Is that possible?
Posted 14 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Sorry but I believe with normal Silverlight security settings, that is impossible as it must be user initiated. But that is the default settings.

Silverlight may allow you to run in higher access mode, but that is more of a Microsoft question so you may wish to post in their Silverlight forums to see if it's possible to access files without user initiation.


Actipro Software Support

The latest build of this product (v18.1 build 0233) was released 5 years ago, which was after the last post in this thread.