How do I check if class lib is being used by web or windows app?

  • Thread starter Thread starter rgliane
  • Start date Start date
R

rgliane

[Didn't get an official response from MS...so am reposting after setting up
alias]

I have a method in a common class library that reads the application config
file. If the library is used by a Windows app, it should use
ConfigurationManager to read the app.config file. If the library is used by
a Web app, it should use WebConfigurationManager to read the web.config file.


Is there a programmatic way for the class lib to determine what kind of app
is running? Or is there another approach to handle this kind of thing?
 
Hi,

the following code should give you answer if your Assembly is used in Web or Win environment.
using System;

using System.Web;

namespace ClassLibrary1

{

public class Class1

{

public Class1() { }

public bool IsWebApp(){

return (HttpContext.Current != null);

}

}

}



Hope this helps. Best regards,
DLM@bypsoft
Cross database solutions
Compare databases for free - SQL Server, MySQL and Oracle
 
Thanks for DLM's input.

Hi rgliane,

I agree with DLM that you can use the "HttpContext.Current" property to
check whether the current application runtime is in ASP.NET web application
or not. For ASP.NET web application the HttpContext.Current represent the
HttpContext instance attached to the current workerthread. While for
non-ASP.NET application, this property is Null.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
Reply-To: "dlm@bypsoft" <[email protected]>
From: "dlm@bypsoft" <[email protected]>
References: <[email protected]>
Subject: Re: How do I check if class lib is being used by web or windows app?
Date: Fri, 29 Feb 2008 23:33:40 +0100

Hi,
the following code should give you answer if your Assembly is used in Web or Win environment.
using System;
using System.Web;
namespace ClassLibrary1
{
public class Class1
{
public Class1() { }
public bool IsWebApp(){
return (HttpContext.Current != null);
}
}
}
Hope this helps. Best regards,
DLM@bypsoft
Cross database solutions
Compare databases for free - SQL Server, MySQL and Oracle
[Didn't get an official response from MS...so am reposting after setting up
alias]

I have a method in a common class library that reads the application config
file. If the library is used by a Windows app, it should use
ConfigurationManager to read the app.config file. If the library is used by
a Web app, it should use WebConfigurationManager to read the web.config file.


Is there a programmatic way for the class lib to determine what kind of app
is running? Or is there another approach to handle this kind of thing?
 
Hi rgliane,

Do you have any further questions on this?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.



--------------------
From: (e-mail address removed) ("Steven Cheng")
Organization: Microsoft
Date: Mon, 03 Mar 2008 01:33:22 GMT
Subject: Re: How do I check if class lib is being used by web or windows app?


Thanks for DLM's input.

Hi rgliane,

I agree with DLM that you can use the "HttpContext.Current" property to
check whether the current application runtime is in ASP.NET web application
or not. For ASP.NET web application the HttpContext.Current represent the
HttpContext instance attached to the current workerthread. While for
non-ASP.NET application, this property is Null.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
Reply-To: "dlm@bypsoft" <[email protected]>
From: "dlm@bypsoft" <[email protected]>
References: <[email protected]>
Subject: Re: How do I check if class lib is being used by web or windows app?
Date: Fri, 29 Feb 2008 23:33:40 +0100

Hi,
the following code should give you answer if your Assembly is used in Web or Win environment.
using System;
using System.Web;
namespace ClassLibrary1
{
public class Class1
{
public Class1() { }
public bool IsWebApp(){
return (HttpContext.Current != null);
}
}
}
Hope this helps. Best regards,
DLM@bypsoft
Cross database solutions
Compare databases for free - SQL Server, MySQL and Oracle
[Didn't get an official response from MS...so am reposting after
setting
up
alias]

I have a method in a common class library that reads the application config
file. If the library is used by a Windows app, it should use
ConfigurationManager to read the app.config file. If the library is used by
a Web app, it should use WebConfigurationManager to read the web.config file.


Is there a programmatic way for the class lib to determine what kind of app
is running? Or is there another approach to handle this kind of thing?
 
Back
Top