Tuesday, September 06, 2011

"The specified metadata path is not valid." error in using Windows Service and Entity Framework

I have project (Windows Service) that use Entity Framework model as external DLL.
I cannot use a metadada as embedded resource and must define connection string in config file of Windows Service. In the Debug mode I got right result, I created setup project and installed service in directory but a immediately I saw the next error:

 The specified metadata path is not valid. A valid path must be either an existing directory, an existing file with extension '.csdl', '.ssdl', or '.msl', or a URI that identifies an embedded resource.
I'll not tell you what I not tried  and a solution was found after deep research in order to understand where is run Windows Service. Bad news! Windows Service not run in installed directory therefore all path's to files in config file must be define as physical path otherwise application cannot find it. Windows service use relative path different to installation directory.

Where you use Entity Framework you must to define row as this one:
...metadata=.\ServiceModel.csdl|.\ServiceModel.ssdl|.\ServiceModel.msl...
In order to not change path to physical path just add next row in event OnStart in Windows Service:
System.IO.Directory.SetCurrentDirectory(System.AppDomain.CurrentDomain.BaseDirectory);

This is a way to define relative path of Service as installed directory.

Enjoy!

kick it on DotNetKicks.com