How to use a DLL in the Windows application and the Web forms application even if it refers to Web elements (like nUnit)?

advertisements

I have a web forms application in a dll (DesiredApp.dll) that contains some classes (DesiredClass.cs) I'd like to use in other applications, such as windows or console applications. I created a function IsWeb() to verify if it is a web application or a windows application (it actually tests if HttpContext.Current is null). So I could (i guessed) use http stuff such as session, request, response only if they are available. When I try to add a reference to DesiredApp.dll in my WindowsFormsClient or ConsoleClient it doesn't compile. I can't add the reference. But the nUnit tests in DesiredApp.dll works in nUnit, even it is a Windows Application. How can nUnit run tests in dll's that are WebForms based?

How can I do such a thing in my Windows/Console App?

How can I write libraries that use http stuff (like HttpContext, Session, Request, Response) only when they are in http applications, but doesn't use then when are in windows application?


I found it. I was traing to compile the application using .net framework 4 client profile.

If you use the complete version of .net framework (go to project properties / application / target framework and set to .net framework 4, don't use client profile) you are able to compile windows application using System.Web dll's.

If you want do know if you are running a library in an windows application or a web application and want to know if it is possible to use Session, do

public virtual bool IsWeb()
{
    return (System.Web.HttpContext.Current != null);
}

public virtual bool IsSessionEnabled()
{
    return IsWeb() && (System.Web.HttpContext.Current.Session != null)
}