开发者

C# MEF Hangs when creating DirectoryCatalog

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 ser开发者_StackOverflow社区vice. 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.


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜