简体   繁体   English

在C#中,类如何以在ASP.Net和非Web应用程序下均可工作的方式完成其自己的应用程序终结处理?

[英]In C#, how can a class do its own application-end finalization in a way that works under both ASP.Net and non-web applications?

I have a library class used in ASP.Net and non-web applications that needs end-of-application finalization. 我有一个在ASP.Net和非Web应用程序中使用的库类,它们需要对应用程序结束进行终结处理。

Within my project's library, I want the class to do its own end-of-application finalization without requiring a developer to add a call to Global.Application_End or AppDomain.CurrentDomain.ProcessExit. 在我的项目的库中,我希望类进行自己的应用程序终结处理,而无需开发人员添加对Global.Application_End或AppDomain.CurrentDomain.ProcessExit的调用。

How can this be done? 如何才能做到这一点?

Additional background/rationale: 其他背景/理由:

Even if I were to consider those options, it appears that AppDomain.CurrentDomain.ProcessExit does not get called when an ASP .Net application is stopped in IIS. 即使我考虑这些选项,当ASP .Net应用程序在IIS中停止时,AppDomain.CurrentDomain.ProcessExit似乎也没有被调用。 And Global.Application_End is specific to ASP .Net. 并且Global.Application_End特定于ASP .Net。 So, neither option is compatible with both situations. 因此,这两种选择都不兼容。

If it helps to have a specific example of why this might be needed... In this particular case, it uses SqlDependency which (as I understand it) requires a call to SqlDependency.Start for initialization and a call to SqlDependency.Stop before application termination. 如果有一个为什么可能需要这样做的特定示例有帮助...在这种特殊情况下,它使用SqlDependency,据我所知,它需要调用SqlDependency.Start进行初始化,并需要在应用之前调用SqlDependency.Stop。终止。

(see http://msdn.microsoft.com/en-us/library/ms172133.aspx ) (请参阅http://msdn.microsoft.com/en-us/library/ms172133.aspx

It appears that a simulated static fianalizer described in this SO answer, https://stackoverflow.com/a/256278/449837 , works in both web and non-web applications. 看来,此SO答案https://stackoverflow.com/a/256278/449837中描述的模拟静态fianalizer在Web和非Web应用程序中均可使用。

Here's a sample that I have working: 这是我正在工作的示例:

public class ClassThatNeedsStaticFinalizer {

    // ... other class properties, methods, etc ...     

    #region Static Finalizer Emulation

    static readonly Finalizer finalizer = new Finalizer();
    private sealed class Finalizer
    {
        ~Finalizer()
        {
             //  ... Do final stuff here ...
        }
    }

    #endregion
}

(NOTE: For some code scratchpad environments like LinqPad, ~Finalizer may not fire until the scratchpad app itself closes) (注意:对于某些代码便签本环境,例如LinqPad,在便签本应用程序本身关闭之前,〜Finalizer可能不会触发)

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

相关问题 如何创建一个在ASP.NET和非ASP.NET应用程序中工作的类库? - How do I make a class library that works in ASP.NET and non-ASP.NET applications? 如何创建一个可以生成自己的字段和新面板的ASP.NET C#? - How create an ASP.NET C# that can generate its own field and and new panels? 需要示例应用程序才能使用c#在asp.net中开发自己的网站和应用程序 - Need sample Applications to develop my own site and application in asp.net using c# 在asp.net web forms c#应用程序中保存时如何在文件名的末尾添加增量数字? - How to add incremental numbers to the end of a filename when saving in asp.net web forms c# application? 在自己的Web应用程序中的ASP.NET身份验证 - Asp.net authentication in its own web application 是否可以在C#ASP.NET Web应用程序中执行此操作? - Is it possible to do this in a C# ASP.NET Web Application? C#ASP.NET无法重新加载Web应用程序 - C# ASP.NET can not reload web application 有没有办法在不使用前端javascript的情况下使用c#使用RestAPI登录到Asp.net MVC Web应用程序? - Is there any way to consume RestAPI login to Asp.net MVC web application with c# without using front-end javascript? 异步如何在ASP.NET Web应用程序中工作? - How async works in ASP.NET web applications? 如何在ASP.NET C#Web应用程序中维护库存? - How to maintain stock in asp.net C# web application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM