PDA

View Full Version : Obtaining bean definitions from a stream


emacinty
01-30-2005, 12:37 AM
Hello!

My assembly uses the XmlApplicationContext along with a location of
the style "assembly://" in order to get bean definitions from an
embedded resource. That all works fine when my assembly is used
from a pure .Net application.

However, when my assembly is used via COM Interop from a Word
AddIn, my assembly can't be resolved:
"Unable to load assembly [<name of assembly>]"
This is nested within the following, thrown on constructing the context:
"Exception has been thrown by the target of an invocation."

There are not normally any problems with my assembly in terms
of .Net finding it, so I suspect this may be a side affect of Interop
and the fact that I don't use the GAC - just a guess.

I'd be more than happy to look up the resource myself and pass
in a stream - does Spring.Net let me do this? The context above
only seems to support resources which can be addressed via a
string location.

thanks

Ewan MacIntyre

PS I'm using the code from CVS from 2005-01-28.

Aleks Seovic
01-30-2005, 08:53 AM
Hi Ewan,

Can you load assembly directly using Assembly.LoadWithPartialName? If you can get that to work AssemblyResource should work as well, as long as you pass it the same assembly name as the first part of the URI.

There is also InputStreamResource in CVS, but none of the application contexts support it as of now. If you can get away with object factory instead od context, you should be able to load it like this:



using Spring.Objects;
using Spring.Objects.Factory.Support;
using Spring.Objects.Factory.Xml;
using Spring.Core.IO;

public IListableObjectFactory CreateObjectFactory(InputStream configStream)
{
DefaultListableObjectFactory objectFactory = new DefaultListableObjectFactory();
XmlObjectDefinitionReader objectDefinitionReader = new XmlObjectDefinitionReader(objectFactory);
objectDefinitionReader.LoadObjectDefinitions(new InputStreamResource(configStream));
objectFactory.PreInstantiateSingletons(); // optional
return objectFactory;
}



Hope this helps.

-- Aleks

emacinty
01-30-2005, 02:09 PM
Aleks,

You were right on both issues: the situation where the resource cannot be found, is also one in
which LoadWithPartialName does not work for my assembly. Also, using an object factory
and a stream worked.

However, perhaps not surprisingly, my object definitions don't work now - presumably because
the type definitions mean LoadWithPartialName is used there again. (My pure .Net tests
which don't involve interop work fine.)

Does this mean that Spring.Net only allows classes to be created from assemblies which
are either in the GAC or in the application directory?

thanks

Ewan

Anonymous
01-30-2005, 04:46 PM
Hi,

I've discovered that if I omit the assembly name from name "type"
attribute values in my object definitions, it works. The debugger
wins again... ;-)

Downside is that this will be slightly inefficient, since it iterates through
all assemblies to do it.

It would be good to be able to hint to Spring.Net about where it can find
assemblies, similar to the namespace config I vaguely recall from the
XmlSerializer.

regards

Ewan

Mark Pollack
01-31-2005, 11:14 PM
We will look into this some more, I've made a JIRA issue.

Cheers,
Mark

Aleks Seovic
01-31-2005, 11:34 PM
Have you tried using codebase hints to specify location of your assembly? I am reluctant to reinvent the wheel and replace .Net assembly resolver with our own.

http://www.informit.com/articles/article.asp?p=30601&seqNum=6 has an excellent description of how .Net assembly resolver works. It looks like codebase hint does require that assembly has a strong name, though. It's not clear whether LoadWithPartial name uses resolver either, we should probably test that and maybe switch to Load if it doesn't.

Regards,

Aleks

emacinty
02-01-2005, 09:12 AM
Thanks, I'll give the codebase idea a try.

Ewan