简体   繁体   English

如何在Windows应用程序和Web窗体应用程序中使用dll,即使它引用Web内容(如nUnit)?

[英]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. 我在dll(DesiredApp.dll)中有一个Web窗体应用程序,其中包含一些我想在其他应用程序(例如Windows或控制台应用程序)中使用的类(DesiredClass.cs)。 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). 我创建了一个IsWeb()函数来验证它是Web应用程序还是Windows应用程序(它实际上测试HttpContext.Current是否为null)。 So I could (i guessed) use http stuff such as session, request, response only if they are available. 因此,我(我猜想)只能在会话可用时使用诸如会话,请求,响应之类的http东西。 When I try to add a reference to DesiredApp.dll in my WindowsFormsClient or ConsoleClient it doesn't compile. 当我尝试在WindowsFormsClient或ConsoleClient中添加对DesiredApp.dll的引用时,它不会编译。 I can't add the reference. 我无法添加参考。 But the nUnit tests in DesiredApp.dll works in nUnit, even it is a Windows Application. 但是DesiredApp.dll中的nUnit测试即使在Windows应用程序中也可以在nUnit中使用。 How can nUnit run tests in dll's that are WebForms based? nUnit如何在基于WebForms的dll中运行测试?

How can I do such a thing in my Windows/Console App? 如何在Windows /控制台应用程序中执行此操作?

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? 如何仅在http应用程序中编写使用http东西(如HttpContext,Session,Request,Response)的库,而在Windows应用程序中却不使用它们?

I found it. 我找到了。 I was traing to compile the application using .net framework 4 client profile. 我正在训练使用.net framework 4客户端配置文件编译应用程序。

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. 如果使用完整版本的.net框架(转到项目属性/应用程序/目标框架并将其设置为.net框架4,请不要使用客户端配置文件),则可以使用System.Web dll编译Windows应用程序。

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 如果您想知道自己是在Windows应用程序中还是在Web应用程序中运行库,并且想知道是否可以使用Session,请执行

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. 您可以构建一个包含需要进行引用的类库,然后将其公开给GAC。 then you can both use them in others .net application solution easyly. 那么您都可以轻松地在其他.net应用程序解决方案中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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