栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

我正在运行服务吗

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

我正在运行服务吗

像Ash一样,我将所有实际的处理代码编写在单独的类库程序集中,然后由Windows Service可执行文件以及控制台应用程序引用。

但是,有时候了解类库是否在服务可执行文件或控制台应用程序的上下文中运行很有用。我这样做的方法是反思托管应用程序的基类。(对VB表示抱歉,但是我认为可以很容易地对C#进行以下修饰):

Public Class ExecutionContext    ''' <summary>    ''' Gets a value indicating whether the application is a windows service.    ''' </summary>    ''' <value>    ''' <c>true</c> if this instance is service; otherwise, <c>false</c>.    ''' </value>    Public Shared Readonly Property IsService() As Boolean        Get ' Determining whether or not the host application is a service is ' an expensive operation (it uses reflection), so we cache the ' result of the first call to this method so that we don't have to ' recalculate it every call. ' If we have not already determined whether or not the application ' is running as a service... If IsNothing(_isService) Then     ' Get details of the host assembly.     Dim entryAssembly As Reflection.Assembly = Reflection.Assembly.GetEntryAssembly     ' Get the method that was called to enter the host assembly.     Dim entryPoint As System.Reflection.MethodInfo = entryAssembly.EntryPoint     ' If the base type of the host assembly inherits from the     ' "Servicebase" class, it must be a windows service. We store     ' the result ready for the next caller of this method.     _isService = (entryPoint.ReflectedType.baseType.FullName = "System.ServiceProcess.Servicebase") End If ' Return the cached result. Return CBool(_isService)        End Get    End Property    Private Shared _isService As Nullable(Of Boolean) = Nothing#End RegionEnd Class


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/576063.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号