how do I access the application object within another assembly?

S

SteveS

Hello All. I have an asp.net application with 3 different assemblies. They
are like this:

1) Assembly: PublicSite (This contains the website UI)
Root namespace: PublicSite

2) Assembly: PublicSite.MyProfile.Business (This contains the business
rules)
Root namespace: PublicSite

3) Assembly: PublicSite.MyProfile.Data (This contains the data layer)
Root namespace: PublicSite

I also have my connection strings defined in the application object. So, in
the main assembly, I can access the connection strings by using
current.application("myConnection"). However, I need to access the
Application object in my Business assembly. How can I do that???

I tried to do this: Dim x as new System.Web.httpContext.Current, but that
didn't work. When I use intellisense, I get the following after System.Web:
{AspNetHostingPermision}, {AspNetHostingPermissionAttribute} and
{AspNetHostingPermissionLevel}

I believe that it's easy to make an argument to say that one should never
try to access the asp.net application object from within a business class,
but currently, we only use these objects for asp.net and not windows
applications. (not that I'm making excuses.....)

Thank you very much for your help!

SteveS
(e-mail address removed)
 
T

Tu-Thach

You should be able to use HttpContext.Current without
problems. Make sure you are not declaring this in your
class member declaration, but in your method.

Tu-Thach
-----Original Message-----
Hello All. I have an asp.net application with 3 different assemblies. They
are like this:

1) Assembly: PublicSite (This contains the website UI)
Root namespace: PublicSite

2) Assembly: PublicSite.MyProfile.Business (This contains the business
rules)
Root namespace: PublicSite

3) Assembly: PublicSite.MyProfile.Data (This contains the data layer)
Root namespace: PublicSite

I also have my connection strings defined in the application object. So, in
the main assembly, I can access the connection strings by using
current.application("myConnection"). However, I need to access the
Application object in my Business assembly. How can I do that???

I tried to do this: Dim x as new
System.Web.httpContext.Current, but that
didn't work. When I use intellisense, I get the following after System.Web:
{AspNetHostingPermision},
{AspNetHostingPermissionAttribute} and
 
S

SteveS

That didn't work. I get the error "HttpContext is not declared" when I
try this:
Dim x as string = HttpContext.Current.Application("xxx").tostring .
..or..
Dim x as string = System.Web.HttpContext.Current.Application("xxx").Tostring

Here is my function:

Public Shared Function GetOpenConnection(ByVal WhichServer as ServerType) as
SqlConnection
Dim conn as new SqlConnection
Select Case WhichServer
Case ServerType.TestServer
conn.ConnectionString =
HttpContext.Current.Application("MyConnectionString")
End Select
... more code...
End Function

Any ideas???

Thanks!
 
T

Tu-Thach

Did you import the namespace into your library? Did you
reference the System.Web.dll for your project?

Tu-Thach
 
H

Hermit Dave

Steve,

Even i am having a similar issue...
writing a custom assembly which does serialization and lot of other junk...
want to check whether user is authorised to use the page
that assembly will be reference from within a class in ASP.NET app.

I tried to put a reference for System.Web;
not helpful... doesnt let me get the context...
looked a bit further and read something bout IHttpModule and IHttpHandler..
saying need to implement them in order get access to current context..

woud appreciate some help from someone who's done it before...
 
H

Hermit Dave

Hello Steve,

Tried something similar in C# last evening... but it wouldnt let me compile it... having tried using System.web... didnt allow me to create the HttpConext...
finally i had put a similar post on security ng... and someone was kinda enough to suggest that i use
Principal from the Thread.CurrentPrincipal.IsInRole() to check the validaity for the call... have to try it later on during the evening or 2morrow...

Thread.CurrentPrincipal.IsInRole("role")

Happy holidays... and thanks,

HD

I got it to work. Here's what I did:

1) Make sure there is a reference set to System.Web
2) Import System.Web in your class module
3) Use this code in your function:
Dim objHTTPApp As System.Web.HttpApplication
dim strConnection as string
strConnection = objHTTPApp.Context.Current.Application("MyConnection").ToString

This worked perfectly for me.

However, I have one question, if someone would like to answer it....

I'm not completely sure why I had to Import System.Web. First, I tried this but it didn't work:
strconnection = System.Web.HttpApplication.Context.Current.Application("MyConnection").ToString
Why wouldn't this compile?

Happy Holidays,

SteveS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top