Application object in dll library?
-
I'm building this class library which will be referenced from my ASP.NET code. The class library code need to access information stored in application variables. Any suggestion? QUESTION 1: My take at it: string sConn = oHttpContext.Current.Application["sConn"]; But I'm not quite sure how to retrieve reference to the HttpContext from code in class library... Constructor for HttpContext requires a HttpWorkerRequest, which is of type System.Web.Hosting.SimpleWorkerRequest. First, I don't know how to get at it. QUESTION 2: Can I do this instead: string sConn = ((HttpContext) this.context).Application["sConn"]; myaspxpage.aspx namespace .. { Page_Load(..) { someName.MyComponent oManager = new someName.MyComponent(this.Request); //QUESTION: Is it necessary for me to pass WorkerRequest object as argument to constructor? Also, is "this.Request" of type "WorkerRequest"? I prefer not to supply Request/Reponse object to component constructor - first it's cleaner, secondly, the component will run in process of the ASPNET application, we can "probably" retrieve the objects from library code, right? //This is yet another alternative, equally clumsy: someName.MyComponent oManager(this.Request, this.Response); } } Signature of HttpContext is: public HttpContext(HttpRequest, HttpResponse); public HttpContext(HttpWorkerRequest); HttpWorkerRequest is of type: System.Web.Hosting.SimpleWorkerRequest HttpResponse is of type: System.Web.HttpResponse HttpRequest is of type: System.Web.HttpRequest Also, why "Response" or "Request" object, shouldn't a HttpContext be associated with a "HttpApplication" object instead? For instance, I don't see Application object is related to Response or Request object in any way. Thanks!
-
I'm building this class library which will be referenced from my ASP.NET code. The class library code need to access information stored in application variables. Any suggestion? QUESTION 1: My take at it: string sConn = oHttpContext.Current.Application["sConn"]; But I'm not quite sure how to retrieve reference to the HttpContext from code in class library... Constructor for HttpContext requires a HttpWorkerRequest, which is of type System.Web.Hosting.SimpleWorkerRequest. First, I don't know how to get at it. QUESTION 2: Can I do this instead: string sConn = ((HttpContext) this.context).Application["sConn"]; myaspxpage.aspx namespace .. { Page_Load(..) { someName.MyComponent oManager = new someName.MyComponent(this.Request); //QUESTION: Is it necessary for me to pass WorkerRequest object as argument to constructor? Also, is "this.Request" of type "WorkerRequest"? I prefer not to supply Request/Reponse object to component constructor - first it's cleaner, secondly, the component will run in process of the ASPNET application, we can "probably" retrieve the objects from library code, right? //This is yet another alternative, equally clumsy: someName.MyComponent oManager(this.Request, this.Response); } } Signature of HttpContext is: public HttpContext(HttpRequest, HttpResponse); public HttpContext(HttpWorkerRequest); HttpWorkerRequest is of type: System.Web.Hosting.SimpleWorkerRequest HttpResponse is of type: System.Web.HttpResponse HttpRequest is of type: System.Web.HttpRequest Also, why "Response" or "Request" object, shouldn't a HttpContext be associated with a "HttpApplication" object instead? For instance, I don't see Application object is related to Response or Request object in any way. Thanks!
You can't create an
HttpContext
that represents a request - ASP.NET does this. If you need to get this for your application, you can either create anIHttpModule
orIHttpHandler
that gives you theHttpContext
for a request, from which you can get the intrinsic objects (Request, Response, Server, User) which can get you theHttpApplication
from the context. TheApplication
property is there so you can access it from either modules or handlers. This is all covered pretty well in the SDK documentation for these interfaces and in several topics for create modules and handlers.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----