PDA

View Full Version : Configuring Spring for use in Web Project referenced assembly



jimbobdog
08-01-2006, 04:39 PM
I'm new to Spring and ASP.NET 2.0 (just to make things difficult)

The question I have is how do I configure an ASP.NET 2.0 web application that references an assembly that uses Spring? There is no requirement to dependency inject or leverage anything from Spring in the web pages/project (yet) - all I want is the underlying assembly to get access to the main context and objects configuration I have.

I have this working perfectly in a console application and the assembly picks up the context & objects configuration no problem. We are now using this assembly (a data access wrapper) in a web project and configured the web.config with the section group and spring section from the app.config we have.

It all looks great except it cannot load the objects configuration file for a child context (using resource uri="file://Config/data.providers.config"). The relative path defined in the resource uri seems to get appended to the current folder which is SYSTEM32\INETSRV...(obviously where the web application process is being executed from - I understand this).

Having looked at this forum there is a reference to Spring.Web and I notice a new style context type of "Spring.Context.Support.WebApplicationContext" - I assume that this is a web application aware context loader implementation and uses Server.MapPath and other logic to load a relatively pathed configuration file correctly into a web application - is this correct?

Now, do I find the Spring.Web stuff in Spring.Net 1.1 Preview 2 and is the minimum change I need to do to enable the configuration file to be loaded correctly is change the context type to "Spring.Context.Support.WebApplicationContext" and set the resource uri to "~/config/data.providers.config"?

All I need is to get the objects configuration accessible to the referenced sub assembly in a web application. I don't need to acces any Spring stuff directly in the web project itself. What do I need to do?

Thanks for any help you guys can provide!

James

Erich Eichinger
08-02-2006, 02:02 PM
Hi,


... change the context type to "Spring.Context.Support.WebApplicationContext" and set the resource uri to "~/config/data.providers.config"?


That's exactly what you have to do.

As a side-note:
If you really never ever use the context on your .aspx pages, it should be enough to just change the resource uri "file://config/data.providers.config" to "web://~config/data.providers.config".

br,
Erich

Aleks Seovic
08-02-2006, 02:06 PM
Unfortunately, the latest web stuff is not in P2 release. You will have to download one of the nightly builds in order to get it.

Once you do that, the change is basically as you described: specify Spring.Context.Support.WebApplicationContext as context type and change your resource paths to use web resource syntax. Relative paths will work as well, just don't use the file:// protocol prefix.

Regards,

Aleks

jimbobdog
08-02-2006, 04:23 PM
Hi guys - thanks for the quick responses!

I'm still having a little trouble here as it seems that a child context definition is not being picked up. The main context (config://spring/objects) appears as a location but the context named "data.providers" is being ignored...this is my configuration in web.config,

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


<spring>
<context>
<resource uri="config://spring/objects"/>
<context name="data.providers" type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="web://~/config/data.providers.config"/>
</context>
</context>
<objects xmlns="http://www.springframework.net">
<description>Application specific objects go here</description>
</objects>
</spring>

I am loading the main context in my wrappers static constructor with,

IApplicationContext appContext = Spring.Context.Support.ContextRegistry.GetContext( );

And accessing the child context with,

IApplicationContext ctx = Spring.Context.Support.ContextRegistry.GetContext("data.providers");
// ctx why is null when used in a web application??
return ctx.GetObject(objectName);

This works fine in a console application but GetContext("data.providers") is returning null when used from my web application. I've tried a few ideas but nothing seems to get the child context loaded and available via GetContext().

I have checked that the file is there in the code with,

string path = Server.MapPath("~/config/data.providers.config");
System.Diagnostics.Debug.Assert(System.IO.File.Exi sts(path));

which is called immediately before I call my wrapper that uses Spring. If the resource uri of a child context is invalid (for whatever reason) does Spring ignore this context section?

Thanks again in advance for any help and guidance you can provide...Spring seriously rocks and you guys do an amazing job!

Erich Eichinger
08-04-2006, 06:37 AM
Hi jimbobdog,

are you using the latest nightly build?

jimbobdog
08-04-2006, 09:18 AM
Hi Erich, yeah I pulled down Spring.NET-20060801-2206.zip and recompiled against this to pull in the Spring.Web references. I see there is a couple of more recent builds - I'll try the last one of those.

I did think that the problem is because I was placing the data.providers.config file in a sub folder (no web.config in this folder) so tried placing the file in the project root but that didn't work. I didn't try adding a child web.config to the "Config" sub folder.

I also tried various combinations of the context type...on the main context alone, on the child context...on both..but that didn't work.

Right now we have moved the objects configured in this file into the main context objects as a work around. I've debugged through some of the context registry stuff but not seriously had a look at tracking down through the context configuration handlers. I can take a look at Spring.Context.Support.ContextHandler but I don't have much time though so if you can point in the right direction that would help...I could also zip the project and upload to my web space if that helps you to repro?

Regards,

James

Erich Eichinger
08-04-2006, 10:05 AM
Hi,

I checked the sources now: "Explicitly" nested contexts are not supported in web-applications atm - the "name"-attribute is ignored in webapps and the virtualpath will be taken as the context's name instead.

I'm not sure if we ever will support this, since this results in mixing 2 strategies of nestings contexts in webapps. In webapps context nesting is done by placing new web.config in virtual directories.

Thus you'll have to add your "dataproviders.config" to the root context.

Hope this helps,
Erich

Bruno Baia
08-07-2006, 09:01 AM
Hi,

there is a lot of confusion about web application configuration, you should take a look to the SpringAir sample as an guidance for configuration.

In the root Web.config, configure all common objects of your application :


<spring>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="~/Config/Aspects.xml"/>
<resource uri="~/Config/Web.xml"/>
<resource uri="~/Config/Services.xml"/>
<resource uri="~/Config/Dao.xml"/>
<resource uri="~/Config/Test/Services.xml"/>
<resource uri="~/Config/Test/Dao.xml"/>
</context>
</spring>


Then you can add a Web.config in each sub directories to configure pages, usercontrols, etc....


-Bruno