Dynamics 365 WebAPI helper Configuration class doesn't find config file - c#

I have a solution that outputs data from a WCF service and reads it from a Dynamics 365 WebAPI.
The main project is a WCF service that is hosted in IIS. While debugging I use IIS Express.
I have a data layer project (dll) that has the CRM WebAPI Helper Classes imported within the project.
When I launch the debugger (IISExpress), the data layer will use the WebAPI FileConfiguration helper class to try and read configuration from it. This step fails because it tries to find a config file with a name C:\Program Files (x86)\IIS Express\iisexpress.exe.config which doesn't exist. None of the project dlls exist in that directory.
I have created an IIS Virtual Directory and checked that the configuration points to the correct physical directory (where my solution is). I also tried incrementing the IISExpress port number by one so that it would create a new virtual directory.
My question is how to find the correct path for the data access dll regardless of the environment I'm in? The FileConfiguration helper class uses Directory.GetCurrentDirectory() and I'm not sure it it's always applicaple?
If I try to get the Assembly Path directly it will point to some temp directory that doesn't contain the config file.

I found out that I can get the path of the IIS application by using HttpRuntime.AppDomainAppPath.
How to get ASP.NET application path?

Related

Windows service running as local service - input files?

I have a windows service program running in C#. I have configured to run it as a local service with my config files stored in \bin\release\config\configvalues.txt. But, it does not recognize this file path and throws the "System.UnauthorizedAccessException".
I believe its looking for the files in the System32 folder and since it does not have the privilege, throws out the exception. For the workaround, my service is running with "local system" to recognize the System32\config folder. To run as a local service, which file path should the config folder be available?
Check the location of your project folder. Most likely, you'll find that it is inside your User profile folders, which the local service account does not have access to.
Deploy your project to a folder outside of any User profiles and you'll have better luck.

How come my Windows Service cannot access a folder with files in them?

I have setup a Console, Library, and Service project. The Library project loads up files in a folder and stores the text in variables.
The console project will then manipulate those variables. I am able to run the Console project successfully and it loads the files without a problem. The files are set to Copy Always in the DLL and are specified as Content for the Build Action.
When I try to run the exact same code in the service.
File.ReadAllText(#"MyFolder\SomeFile.txt");
It throws the following exception.
The type initializer for 'MyLibrary.Data.ReadFiles' threw an exception.
I am using the Setup Project type to create the installer. After the service installs the folder MyFolder does exist in the Application Folder of the service, as well as the files. What else could be causing my problem for the service not being able to read those files?
EDIT 1
public class ReadFiles {
public static string DataFile = File.ReadAllText(#"MyFolder\SomeFile.txt");
}
The service account that you're running the Windows service under, doesn't have rights to read from the location you're trying to access. The error you're seeing has to do with the fact that the code you showed likely exists in the construction of the type ReadFiles.
Was facing similar issue in one of the windows service for running node js application, for sending emails.
It was running fine when we run from the same directory, but running same application as windows service was having issues and couldn't send emails. Comment from #meanbunny did help, so adding it as an answer.
Windows service can't access the directory/files if they are mentioned in the code as relative path, better use the absolute path or be more flexible as #Mike mentioned in his comment to use app.config for each service.

Cannot use Server.MapPath to access a external file

I have a xml file which is in a folder in the solution. I tried to access it using Server.MapPath. It was working fine in a aspx page of a different project. When I tried to access the file in my class library project, I am not supposed to use Server.MapPath. So I tried with HttpContext.Current.Server.MapPath. Problem is this class library project is calling from a separate WCF service project, so current server is WCF service project's server. So it ended up with error path is not valid.
This is what I tried- HttpContext.Current.Server.MapPath("./folder/conf.xml.config")
Any solution?
System.Web is already imported.
You can create a virtual-directory inside the web-services. This virtual-directory will point to Physical location of "folder/conf.xml.config".
Once this is done, you can access it using your existing code like below...
Server.MapPath("folder/conf.xml.config")
Server.MapPath works only with files that are inside the website and is used by specifying a relative location:
string configFile = Server.MapPath("~/App_Data/config.xml.config");
If you want to access a file from some other location you will have to manually provide the absolute path to it:
string configFile = #"c:\work\some_folder\config.xml.config";

How to use ApplicationManager to load an object with out GAC'ing it?

I'm trying to use the Cassini webserver to create a deployment test host- a sort of embedded web server to isolate web server config issues from app deployment issues. Okay, so this line of code blows up until I put Cassini in the GAC:
// physical path points to the bin folder with the dll
_host = (Host)_appManager.CreateObject(appId, typeof(Host),
_virtualPath, _physicalPath,
false /*failIfExists*/);
The above fails with FileNotFoundException -- as if it is trying to find the Cassini.dll
Should I just be able to tell ApplicationManager where to look without taking the extreme step of putting it into the GAC?
I am using Cassini 2.1 and the source code is here.
I believe I had a similar problem and what I needed to do was put the searched-for DLLs into a bin subdirectory.
If the searched-for DLLs are located in the current directory, it's enough to create a junction point called "bin" that refers to the current directory.

C# Getting a string from one config file to be used by multiple projects

In my project I have a Windows Service and a WCF Service doing some actions on the same folder on a computer. The location of that folder will be given by the user within an app.config file that is included in the Windows Service project.
Now, I want the WCF Service to know the location of that folder (the folder given by the user), without the user having to type it into the WCF Service config file as well.
How would I go about doing that?
Thanks!
There are lots of possibilities here:
Add the path of the Windows Service's config file as a setting in the WCF service's config file, and use that path to read the file with an XML Reader.
Store the folder path in some centralized system, like a database or the registry, and have both services obtain the setting from that centralized placed.
Specify the path in each individual config file, but use a post build event to make sure they stay current. (For instance, maybe the post-build event retrieves the config setting from the DB and then writes it to the config file)
Have one service expose the configuration setting as a public service call. For instance, the WCF service could invoke a method on the Windows service to determine which path to process.
You can place it in the Machine.config, and both the WCF application and the Windows service can access it.
http://msdn.microsoft.com/en-us/library/ms229697(VS.71).aspx

Categories

Resources