PDA

View Full Version : 'Error instantiating context' when called from COMInterop


shanecarr
01-04-2006, 05:17 PM
I am relatively new to the spring framework so appologies if I'm missing something too obvious here.

I have configured an app.exe.config file in such a way that I can successfully use springs object factory, provided that the call to the C# code is not made through COMInterop.

Calling via COMInterop results in an 'Error instantiating context' error. An error message of this nature is usually the result of the 'objects' xml file not being in the application run folder. I have ruled this out as being the source of the problem by putting the objects in the app.exe.config file (as opposed to referencing a separate file). As I've said, I have tested this successfully by NOT calling through COMInterop.

Testing through COMInterop has revealed that the app.config.exe file is visible and can be accessed from my C# code. It's the spring framework that appears to have visibility issues to the config file, and I assume at the call to Spring.Core's 'GetContext'.

Does anyone have any idea what might be causing this and how I might fix the problem. I haven't read any reports of a similar issue and have put in nearly 12 hrs trying to research and resolve it. My best guess is it might be some kind of permissions issue but I'm not that familiar with 'permissions' and how they are implemented in this context, etc.

Snippets from the app.exe.config file I'm using are as follows:

<sectionGroup name="spring">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core"/>
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>

<spring>
<context>

<resource uri="config://spring/objects"/>
</context>

<objects xmlns="http://www.springframework.net">


<object id="AggregationSection" factory-method="Instance" singleton="true" type="GED.EnerPrise.ETRM.Aggregation.Configuration.Aggre gationSection, GED.EnerPrise.ETRM.Aggregation" />
</objects>
</spring>


Hope someone can help, this is a show stopper for me.

Thanks

Shane

Aleks Seovic
01-04-2006, 06:19 PM
Can you explain a bit more how you are calling this through COM Interop?

If you have a non-.NET client application that uses COMInterop to talk to .NET libraries, .NET configuration file that is used to configure Spring context will be completely ignored.

You can still instantiate the context, but you will have to do it manually, passing name of the file containing definitions as a parameter to the constructor (this should be Spring config file containing *only* object definitions, such as your original spring-configration.xml):


IApplicationContext ctx = new XmlApplicationContext("spring-configration.xml");


I hope this helps, and if you can give us more detail on your specific usage scenario, I might be able to give you a more complete answer.

Regards,

Aleks

shanecarr
01-05-2006, 12:01 PM
Thanks, that worked. .

Yes, it was a non .net client calling .net library via ComInterop. However, I don't understand why spring.Core should have a problem reading the app.exe.config file via ContextRegistry.GetContext.

Aleks Seovic
01-05-2006, 04:52 PM
Because ContextRegistry relies on .NET configuration framework to read application config file, and .NET configuration framework relies on, well, application being a .NET app :)

Keep in mind that primary target for Spring.NET are greenfield .NET apps. The scenario you are using it in is very rare and there might be a few things that won't work, such as configuration related code. Much more common scenario, and the one where you wouldn't have similar issues, is to have a new .NET client app use legacy COM libraries through Interop, which works just fine using standard configuration.

However, as you can see, there is still a way to create a Spring context and get objects from it, you just have to do it in a more manual fashion.

Regards,

Aleks

shanecarr
01-09-2006, 04:38 PM
Okay, thanks. However, during my testing I put some debug code in to my C# app (before any calls to spring framework) that read a .net config file using .net configuration framework. And that worked fine. This is my point. Why can my C# app access a .net config file when called via COM interop but spring can't.

I now have a further problem, related to your fix for the above issue, which has only just started to happen and I have no idea why. It also only happens when the call is coming in to the C# code from COMInterop. At the point of executing the line
applicationContext = new XmlApplicationContext("spring-configuration.xml");
I get the error message: 'The type initializer for "Spring.Context.Support.AbstractApplicationContext' threw and exception".

Have you come across this before or have any ideas as to why it might be happening? I have also tried hard-coding the path in as well but still the same. Like I said, last week when I tried this out it was working fine.

Thanks

Shane