AssemblyReferences locks referenced assemblies

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 7 years ago by Andreas
Version: 12.1.0560
Avatar

Folks,

As part of a new project we're working on, we have a script engine that uses the SyntaxEditor as its editor (and debug front-end.) To get IntelliSense for the stuff that we add to the language (C#) we add a reference to a custom library (shown below)

projectAssembly.AssemblyReferences.AddMsCorLib();
projectAssembly.AssemblyReferences.Add("Crosshairs.Windows.SuiteVariables"); 

 The projectAssembly is the a CSharpProjectAssembly which is hooked up as shown below

projectAssembly = new CSharpProjectAssembly("CrosshairsTestCase");
var language = new CSharpSyntaxLanguage();
language.RegisterProjectAssembly(projectAssembly);

The custom assembly (Crosshairs.Windows.SuiteVariables) is modifiable by the user, that is - the code that generates the assembly is recompilable in order to add new properties to the assembly. However, as soon as the editor has been instantiated and the references added, it is impossible to recompile the library.

I've tried working around it with ReflectionOnlyLoad and then using the resulting assembly as reference but to no avail. What is the appropriate way of handling this? Do I have to create a new appdomain and run the editor there in order to force an unload?

Edit: I should add, I've also tried (of course) clearing the AssemblyReferences container but that made no difference  

Regards,
Andreas

[Modified 7 years ago]

Comments (5)

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Andreas,

We use reflection-only loads on the assemblies when you call our Add methods that take a path or name.  However I do believe that still does lock the file for some reason.  I believe I've read that you can do this instead, which may not lock the file:

var bytes = File.ReadAllBytes(path); 
var assembly = Assembly.ReflectionOnlyLoad(bytes); 

Then call our Add method overload that accepts the Assembly.  Note that when you refresh, you probably will want to Remove it from the FileBasedAssemblyRepository too in between your Remove/Add for the project assembly references.  It should see a different assembly modification date and overwrite the cached data.


Actipro Software Support

Posted 7 years ago by Andreas
Avatar

Hi!

Thanks for the answer - it did in fact get me a little bit closer to a solution, but not quite :)  The editor itself behaves much better now and the previous problem with file locking is gone.

Now I'm facing a different (but related) problem where I'm unable to do a ReflectionOnlyLoad more than once on the same assembly. It appears that the only solution that I've found is to use a new appdomain but I don't think that'll work well with your editor?

Btw, if you've seen it before and know a solution, here is the exception info

API restriction: The assembly '' has already loaded from a different location. It cannot be loaded from a new location within the same appdomain.

Now, there is one other question I have - the assembly in question is compiled in runtime from generated code. Is it possible to tell the editor to use the generated code as basis for IntelliSense as opposed to referencing the assembly directly? I don't want to expose the file to the user, but rather let the editor use the file behind the scenes (instead of using the assembly.)

Thanks!

Andreas 

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Andreas,

This Connect post may help you out.  Microsoft included a sample in it.

How are you generating the code and what kind of object do you have if you don't want to use the assembly?  Unfortunately we rely on being able to use reflection to populate our assembly cache data so at this time an assembly is probably required.


Actipro Software Support

Posted 7 years ago by Andreas
Avatar

Hi,

Thanks for the link - unfortunately the issue was not resolved there either.

Regarding the generated code, we have a placeholder .cs file (as an embedded resource in our product) which gets populated with real code based on user interaction. This is then compiled during runtime and an assembly is generated. This assembly is then referenced in the SyntaxEditor.

So the thing is, we have access to the full source and the compiled assembly- and I was hoping it would be possible to use that source file as a basis for intellisense in the editor...I take it that's not possible?

In other words, the behavior I'm looking for is similar to that of Visual Studio. If I have a project with two files, one with a class definition and one in which the class is used, the autocomplete / intellisense in Visual Studio parses the class source and provides autocompletion without the need for reflection or having compiled code.

Thanks,
Andreas 

Answer - Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hi Andreas,

Oh well in that case you could just make another IProjectAssembly and reference that from your main IProjectAssembly.  IProjectAssembly can have references to any other IAssembly, including other IProjectAssembly instances or IBinaryAssembly instances.  I think that would be ideal based on your scenario.  Think of each IProjectAssembly as a VS project in a VS solution.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.