View Full Version : ContextRegistry.GetContext() is null
I am new to Sprint.net framework. I am trying to use new enhanced clientProxyFactory in asp.net application. Here is my web.config
<configuration>
<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 type="Spring.Context.Support.WebApplicationContext, Spring.Core">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<!--
<object id="HelloWorld" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ProxyClass"><value>spring.clientProxy.HelloWorld2, sprintTest</value></property>
<property name="ServiceInterface"><value>SpringNetService.IHelloWorld, SpringNetService</value></property>
</object>
-->
<object id="MyService" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ServiceUrl" value="http://localhost/SpringNetService/HelloWorld2.asmx"/>
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, IHelloWorld"/>
</object>
</objects>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
</system.web>
</configuration>
My application virtual dir is sprintTest. I have a web page onload event
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
IApplicationContext ctx = ContextRegistry.GetContext();
object obj= ctx.GetObject ("MyService");
}
When I run it, it get the following:
Server Error in '/springTest' Application.
--------------------------------------------------------------------------------
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: Error creating context.
Source Error:
[No relevant source lines]
Source File: none Line: 0
Then I change my web.config to use sprintTest instead of spring. It bypass the error. But I got null object reference to ctx object. Basically, it did get the context object. What did I missing, any help will be appreciate.
Tony
Bruno Baia
03-10-2006, 05:24 PM
Hi Tony,
In web applications you need to use WebApplicationContext.Current :
IApplicationContext ctx = WebApplicationContext.Current
Anyway, in web applications, you should use dependency injection for your web pages :
Definie your page in the config file :
<object type="~/Default.aspx">
<property name="HelloWorldService" ref="MyService" />
</object>
Then in the code behind, create a HelloWorldService property :
private SpringNetService.IHelloWorld helloWorldService;
public SpringNetService.IHelloWorld HelloWorldService
{
set { helloWorldService = value; }
}
Dependency injection will make the work for you ! no need to retrieve context manually.
-Bruno
Thank you Bruno.
I tried your solution. Somehow, I still got an error.
Here is my web.config
<configuration>
<configSections>
<sectionGroup name="springTest">
<section name="context" type="Spring.Context.Support.ContextHandler, Spring.Core" />
<section name="objects" type="Spring.Context.Support.DefaultSectionHandler, Spring.Core" />
</sectionGroup>
</configSections>
<springTest>
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://springTest/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<object id="MyService" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ServiceUrl" value="http://localhost/SpringNetService/HelloWorld2.asmx"/>
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, IHelloWorld"/>
</object>
<object type="~/WebForm1.aspx">
<property name="HelloWorldService" value="MyService" />
</object>
</objects>
</springTest>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
......
In my webForm1.aspx,
protected System.Web.UI.WebControls.Label lblHelloWorld;
private SpringNetService.IHelloWorld helloWorldService;
public SpringNetService.IHelloWorld HelloWorldService
{
set { helloWorldService = value; }
get {return helloWorldService;}
}
private void Page_Load(objectsender,System.EventArgs e)
{
// Put user code to initialize the pagehere
this.lblHelloWorld.Text=HelloWorldService.DisplayT ext().ToString();
}
The error message is:
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 68: string pageName = WebUtils.GetPageName(url);
Line 69: IApplicationContext appContext = WebApplicationContext.Current;
Line 70:
Line 71: if (appContext.ContainsObjectDefinition(pageName))
Line 72: {
Source File: C:\SpringNet\src\Spring\Spring.Web\Web\Support\Pag eHandlerFactory.cs Line: 70
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Spring.Web.Support.PageHandlerFactory.GetHandler(H ttpContext context, String requestType, String url, String path) in C:\SpringNet\src\Spring\Spring.Web\Web\Support\Pag eHandlerFactory.cs:70
System.Web.HttpApplication.MapHttpHandler(HttpCont ext context, String requestType, String path, String pathTranslated, Boolean useAppConfig) +699
System.Web.MapHandlerExecutionStep.System.Web.Http Application+IExecutionStep.Execute() +95
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean& completedSynchronously) +173
What did I missing? I have two questions about the web.config
1. <sectionGroup name="spring">
I have to change <sectionGroup name="springTest"> instead of spring since the sprintTest is my virtual directory. Is that sectionGroup=your virtual directory name always true?
2. Do i have to put <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
in order for the context to work. How do i use the web service in other class file which is not part of the context.
Thanks for your help
Bruno Baia
03-13-2006, 05:21 PM
Hi Tony,
there was an error in my previous post :
<object type="~/Default.aspx">
<property name="HelloWorldService" value="MyService" />
</object>
it's not "value=" but "ref=" :
<object type="~/Default.aspx">
<property name="HelloWorldService" ref="MyService" />
</object>
Sorry, my fault. didn't test before :oops:
Otherwise, your code seems fine.
1. <sectionGroup name="spring">
I have to change <sectionGroup name="springTest"> instead of spring since the sprintTest is my virtual directory. Is that sectionGroup=your virtual directory name always true?
sectionGroup have nothing to do with your virtual directory.
It's just the name of your section in your config file, u can let "spring".
Don't forget to change the section name and the ressource url too if you change it :
<resource uri="config://springTest/objects" />
2. Do i have to put <add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
in order for the context to work. How do i use the web service in other class file which is not part of the context.
This line is required to enable dependency injection for web pages.
-Bruno
I changed from value to ref in web.config. I still get the same error.
I mentioned early in my post I have to keep :
<sectionGroup name="springTest">
as it is (springTest is my virtual directory). If i changed to spring instead of springTest like the following:
<configuration>
<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 type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<object id="MyService" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ServiceUrl" value="http://localhost/SpringNetService/HelloWorld2.asmx"/>
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, IHelloWorld"/>
</object>
<object type="~/WebForm1.aspx">
<property name="HelloWorldService" ref="MyService" />
</object>
</objects>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
I got the following error:
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: No constructor with string[] argument found for context type [WebApplicationContext]
Source Error:
[No relevant source lines]
Source File: none Line: 0
I must miss something. I don't have master page. Only one page, one web.config. This is the simplest web application I am testing how to use WS without client generated proxy.
Thanks again for your help.
Tony
Bruno Baia
03-13-2006, 08:20 PM
Hmm...
keep this configuration with "spring" as sectionGroup and try to copy paste the stack trace if u can.
Anyway, tell me if i'm wrong :
You have 2 web application :
"/SpringNetService" which host your web service.
"/SpringTest" which use the web service and contains one page : "WebForm1.aspx"
I see a type "SpringNetService.IHelloWorld, IHelloWorld", so you should have a dll file named IHelloWorld.dll in your bin directory.
-Bruno
Aleks Seovic
03-13-2006, 08:28 PM
Actually, the problem is in the fact that you changed configuration element from 'spring' to 'springTest'. Section group name of 'spring' is not only recommended, it is required -- framework internally loads different configuration section using keys such as "spring/context", "spring/typeConverters", etc.
Now, I don't understand why you are running into issues with that setup (and virtual directory name shouldn't have anything to do with it, afaik). One thing I'd try is getting rid of all xsi-related definitions in objects tag and leaving it at just '<objects xmlns="http://www.springframework.net">' -- not sure if there is an older version of the XML schema at the URL specified and if that's causing problems.
Also, you might want to try moving root context definitions to an external configuration file and referencing them:
<context type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="~/Config/Services.xml" />
<resource uri="~/Config/Web.xml" />
</context>
Hope this helps,
Aleks
Thanks for quick response.
I have mistake in my web.config in the following line
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, IHelloWorld"/>
Instead it suppose to have :
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, SpringNetService"/>
After i made the change. I still have the same error
I do have 2 projects. One is http://localhost/sprintTest/webForm1.aspx
The other is the service layer http://localhost/sprintNetService/HelloWorld2.asmx
Here are the configuration for both of them.
On service layer web config looks like this:
<configuration>
<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 type="Spring.Context.Support.XmlApplicationContext, Spring.Core">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.net
http://www.springframework.net/xsd/spring-objects.xsd">
<object id="HelloWorld2" type="SpringNetService.HelloWorldService, SpringNetService">
<property name="Message">
<value>Hello, World!</value>
</property>
</object>
<object id="HelloWorld1Exporter" type="Spring.Web.Services.WebServiceExporter, Spring.Web">
<property name="TargetName"><value>HelloWorld2</value></property>
<!--
<property name="Interfaces">
<list>
<value>SpringNetService.IHelloWorld, SpringNetService</value>
</list>
</property>
-->
<property name="Namespace"><value>http://xo.com/services</value></property>
<property name="Description"><value>My exported HelloWorld web service</value></property>
<property name="Methods">
<dictionary>
<entry key="DisplayText">
<object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
<property name="Description">
<value>My Spring-configured HelloWorld method.</value>
</property>
<property name="MessageName">
<value>MyHelloWorld</value>
</property>
</object>
</entry>
<entry key="DisplayText2">
<object type="System.Web.Services.WebMethodAttribute, System.Web.Services">
<property name="Description">
<value>My Spring-configured HelloWorld method.</value>
</property>
<property name="MessageName">
<value>MyHelloWorld2</value>
</property>
</object>
</entry>
</dictionary>
</property>
</object>
<object name="HelloWorld2" type="SpringNetService.HelloWorldService, SpringNetService" />
</objects>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
....
In my service layer, I have one cs file called HelloWorld.cs
using System;
using System.Web.Services;
namespace SpringNetService
{
/// <summary>
/// Summary description for iHelloWorld.
/// </summary>
public interface IHelloWorld
{
string DisplayText();
string DisplayText2();
}
//[WebService(Namespace="http://myCompany/services")]
public class HelloWorldService : IHelloWorld
{
private string message;
public string Message
{
set { message = value; }
}
//[WebMethod]
public string DisplayText()
{
return this.message;
}
public string DisplayText2()
{
return this.message;
}
}
}
It worked fine when I hit http://localhost/HelloWorld2.asmx. It returns the hello world message back to me.
Now, I create a web form under http://localhost/sprintTest/webForm1.aspx
and trying to consume the web service
Here is the web.confg
<configuration>
<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 type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="config://spring/objects" />
</context>
<objects xmlns="http://www.springframework.net">
<object id="MyService" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ServiceUrl" value="http://localhost/SpringNetService/HelloWorld2.asmx"/>
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, SpringNetService"/>
</object>
<object type="~/WebForm1.aspx">
<property name="HelloWorldService" ref="MyService" />
</object>
</objects>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
...
In our code behine webForm1.aspx.cs
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label lblHelloWorld;
private SpringNetService.IHelloWorld helloWorldService;
public SpringNetService.IHelloWorld HelloWorldService
{
set { helloWorldService = value; }
get {return helloWorldService;}
}
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
this.lblHelloWorld.Text=HelloWorldService.DisplayT ext().ToString();
}
}
When i run the webForm1.aspx. I got
Server Error in '/springTest' Application.
--------------------------------------------------------------------------------
Configuration Error
Description: An error occurred during the processing of a configuration file required to service this request. Please review the specific error details below and modify your configuration file appropriately.
Parser Error Message: No constructor with string[] argument found for context type [WebApplicationContext]
Source Error:
[No relevant source lines]
Source File: none Line: 0
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
I tried to get around it with change the sectiongroup to springTest, then, it bypass that, but, i got error message like the following.
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 68: string pageName = WebUtils.GetPageName(url);
Line 69: IApplicationContext appContext = WebApplicationContext.Current;
Line 70:
Line 71: if (appContext.ContainsObjectDefinition(pageName))
Line 72: {
Source File: C:\SpringNet\src\Spring\Spring.Web\Web\Support\Pag eHandlerFactory.cs Line: 70
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
Spring.Web.Support.PageHandlerFactory.GetHandler(H ttpContext context, String requestType, String url, String path) in C:\SpringNet\src\Spring\Spring.Web\Web\Support\Pag eHandlerFactory.cs:70
System.Web.HttpApplication.MapHttpHandler(HttpCont ext context, String requestType, String path, String pathTranslated, Boolean useAppConfig) +699
System.Web.MapHandlerExecutionStep.System.Web.Http Application+IExecutionStep.Execute() +95
System.Web.HttpApplication.ExecuteStep(IExecutionS tep step, Boolean& completedSynchronously) +173
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.2032; ASP.NET Version:1.1.4322.2032
Thanks for all your support.
Tony
Aleks Seovic
03-13-2006, 09:07 PM
Hi Tony,
It looks like we posted at the same time so I'm not sure if you saw my response above. Please try suggested approach out and let me know if anything changes.
- Aleks
I tried your suggestion, and nothing changed.
Here is my web.config now
<configuration>
<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 type="Spring.Context.Support.WebApplicationContext, Spring.Web">
<resource uri="~/Config/WS.xml" />
</context>
</spring>
<system.web>
<httpHandlers>
<add verb="*" path="*.aspx" type="Spring.Web.Support.PageHandlerFactory, Spring.Web" />
<add verb="*" path="*.asmx" type="Spring.Web.Services.WebServiceHandlerFactory, Spring.Web" />
</httpHandlers>
...
I have file under config/WS.xml look like this:
<?xml version="1.0" encoding="utf-8" ?>
<objects xmlns="http://www.springframework.net">
<object id="MyService" type="Spring.Web.Services.WebServiceClientFactory, Spring.Services">
<property name="ServiceUrl" value="http://localhost/SpringNetService/HelloWorld2.asmx"/>
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, SpringNetService"/>
</object>
<object type="~/WebForm1.aspx">
<property name="HelloWorldService" ref="MyService" />
</object>
</objects>
Thanks for your help.
Tony
Bruno Baia
03-13-2006, 09:28 PM
<property name="ServiceInterface" value="SpringNetService.IHelloWorld, SpringNetService"/>
What is the assembly SpringNetService.dll ?
- a class library that is referenced by both web application and contains ur interface definition.
- assembly of your service web application
Have you a SpringNetService.dll file in the bin directory of your client web application ?
-Bruno
Yes. I do have the SprintNetService.dll in the client bin directory
Tony
Bruno Baia
03-14-2006, 05:53 PM
To be continued...
http://forum.springframework.net/viewtopic.php?p=1989
vBulletin® v3.7.3, Copyright ©2000-2008, Jelsoft Enterprises Ltd.