创建 DirectoryCatalog 时 C# MEF 挂起
我遇到了 MEF 的问题,它在创建 DirectoryCatalog 时挂起。我有一个 Windows 窗体应用程序,我测试了 MEF 功能,并且它运行没有问题。但是,当我在 Windows 服务中运行相同的代码时,它会挂起:
_catalog = new DirectoryCatalog(assemblyBaseDirectory);
//Here is the full code block.
var codeBaseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
if (codeBaseDir != null) {
assemblyBaseDirectory = new Uri(codeBaseDir).AbsolutePath;
Logger.Info("Creating Directory Catalog for " + assemblyBaseDirectory);
_catalog = new DirectoryCatalog(assemblyBaseDirectory);
Logger.Info("Directory Catalog created!");
}
我也没有收到异常。我输入日志并发现第二个 Logger.Info 行永远不会被调用。
更新:
当我从我的服务中调用此代码时,我确定我的路径没有返回相同的路径。它将目录路径格式化为“C:/Program%20Files/My%20Service”。我不知道为什么它没有为我的 win 表单应用程序执行此操作。
I'm running into a problem with MEF where it's hanging when creating the DirectoryCatalog. I have a Windows Forms application that I test the MEF functionality, and it works without a problem. However, when I run the same code in a Windows Service, it hangs on the line:
_catalog = new DirectoryCatalog(assemblyBaseDirectory);
//Here is the full code block.
var codeBaseDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);
if (codeBaseDir != null) {
assemblyBaseDirectory = new Uri(codeBaseDir).AbsolutePath;
Logger.Info("Creating Directory Catalog for " + assemblyBaseDirectory);
_catalog = new DirectoryCatalog(assemblyBaseDirectory);
Logger.Info("Directory Catalog created!");
}
I don't get an exception either. I put the logging in and found the the second Logger.Info line never gets called.
UPDATE:
I determined that my path was not returned the same when I called this code from my service. It was formatting the directory path as "C:/Program%20Files/My%20Service". I'm not sure why it wasn't doing this for my win forms app.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您首先需要了解的是运行 Windows 应用程序(作为您自己)和 Windows 服务(作为什么?)之间的差异。另外,Windows 服务的启动目录是
C:\Windows\System32
,这是svchost.exe
运行的位置。我在使用服务时解析启动路径的方法是包装对Uri
类的调用以获取本地路径:您能否验证服务用户是否有权访问同一路径?
I think the first thing you need to look at is the differences between running a Windows application (as yourself), and then a Windows service (as what?). Also, the startup directory for windows services is
C:\Windows\System32
which is wheresvchost.exe
runs from. The way I resolve startup paths when using services, is to wrap a call to theUri
class to grab the local path:Can you verify that the service user has access to the same path?