创建 DirectoryCatalog 时 C# MEF 挂起

发布于 2024-12-05 10:56:48 字数 792 浏览 1 评论 0原文

我遇到了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

放肆 2024-12-12 10:56:48

我认为您首先需要了解的是运行 Windows 应用程序(作为您自己)和 Windows 服务(作为什么?)之间的差异。另外,Windows 服务的启动目录是 C:\Windows\System32,这是 svchost.exe 运行的位置。我在使用服务时解析启动路径的方法是包装对 Uri 类的调用以获取本地路径:

var path = new Uri(typeof(Something).Assembly.Location).LocalPath;
var catalog = new DirectoryCatalog(path);

您能否验证服务用户是否有权访问同一路径?

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 where svchost.exe runs from. The way I resolve startup paths when using services, is to wrap a call to the Uri class to grab the local path:

var path = new Uri(typeof(Something).Assembly.Location).LocalPath;
var catalog = new DirectoryCatalog(path);

Can you verify that the service user has access to the same path?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文