简体   繁体   中英

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 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.

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM