English 中文(简体)
GetEntryAssembly (). CodeBase 排除单位测试 [重复]
原标题:Assembly.GetEntryAssembly().CodeBase precludes unit testing [duplicate]
This question already has answers here:
Closed 11 years ago.

Possible Duplicate:
.NET NUnit test - Assembly.GetEntryAssembly() is null

我在撰写一个日志库。 我希望图书馆默认情况下会写入到为此应用程序命名的共同应用程序数据文件夹中的目录。 例如, 如果应用程序名为“ MyApplication.exe ”, 我想要在“ C: programDataMyApplication” 中保存的数据 。

我用这个代码来构造路径 :

   private static string loggingDataPath = 
      Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) +
      Path.DirectorySeparatorChar + 
      Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase) +
      Path.DirectorySeparatorChar;

这和预期的完全一样,有一个问题,我无法测试图书馆!

当我尝试运行单位测试时, 它们都以系统. NullReference Exception 失败。 如果我用字符串来替换“ Assembly. GetEntryAssembly (. CodeBase) ” 调用单位测试, 单位测试再次正常运行 。

我想我知道为什么会发生这种情况,但我不知道如何解决这个问题,我希望有人能指引我走上正义的道路。

蒂娅!

更新( 5 - 24 - 12 ) : 我不是要尝试单位测试“ logDataPath ” 的内容 。 只要存在“ Committee. GetEntryAssembly (. CodeBase) ”, 就会导致所有单位的测试都与上述例外不符 。 请注意, “ logDataPath” 是静态的( 因为它必须是静态的库 ) 。

最佳回答

忽略先前答案的圣洁建议, 只回答我的问题, 这就是我如何解决问题的:

   public static string LoggingDataPath { 
      get { 
         return loggingDataPath.Length > 0 ? loggingDataPath : 
         Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments) + 
         Path.DirectorySeparatorChar + 
         Path.GetFileNameWithoutExtension(Assembly.GetEntryAssembly().CodeBase) + 
         Path.DirectorySeparatorChar; 
      } 

      set { loggingDataPath = value; } 
   } 

此解决方案避免初始化对静态类第一个访问的日志DataPath, 从而避免了对组群的呼叫。 GetEntry Asssembly (). CodeBase () 。

问题回答

不仅单位测试会引起问题。

鉴于GettryAssembly () 如果从一个未经管理的应用程序 < /a > 装入管理下的组装, 并且< a href=> Codebase可以包含从因特网下载的组装的URL, 并且不设置从GAC 装入的组装件, 我将避免为通用的伐木图书馆尝试这种做法。

如果这不足以说服您的话,其他问题是 (a) 非特权用户将无法进入共同应用数据库的书写权限,以及 (b) 您申请中试图写入同一日志文件的多个实例将是一个问题。

相反,我会定义日志文件的位置配置 。

你建议我放在哪里 以避免这个问题?

如我所说,我会用配置来定义它(例如在 appsetting in app. config 中设置一个应用程序 ) 。 这是最灵活的。 如果您想将其置于 CommonApplicationData 下, 您可以在阅读配置文件时使用 Environment. Expand EnvironmentalVables 方法来扩展环境变量。 例如 :

<appSettings>
    <add key="logFile" value="%ALLUSERSPROFILE%MyAppMyLogFile.log" />
    ...
</appSettings>

您仍必须解决让非特权用户访问的问题, 并避免在多次访问中出现争议。 您说您的原始伐木图书馆支持同时访问, 但请注意这可能会有潜在性能成本, 取决于您的伐木方式 。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签