简体   繁体   中英

How to use a dll in both windows application and web forms application even if it references web stuff (like nUnit)?

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)
}

you can build a class library which contains you need to do references,then public them to GAC. then you can both use them in others .net application solution easyly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM