Why is this class library dll not getting information from app.config - dll

I am developing a custom HttpHandler, to do so I write a C# Class Library and compile to DLL.
As part of this I have some directory locations which I want not hard coded in the app, so i'm trying to put it in the app.config which I've used before.
Before this has worked by just going building the app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Share" value="C:\...\"/>
</appSettings>
</configuration>
And then obtaining this in code like:
var shareDirectory = ConfigurationManager.AppSettings["Share"];
But when I compile it and put it in the bin folder for the webservice, it keeps getting null for shareDirectory, probably because it can't find app.config. So how do I make sure this is included so I don't have to hard code my direcotry locations? I notice it will essentially after compiled we get the assembly.dll and the assembly.dll.config which is the app.config file, so it's definetly there in the bin folder!

That is because your web service uses web.config

You're probably confusing the scope of your class library.
Remember, your config, be it web.config, or app.config, is the config present in the HOSTING application. In this case your hosting application is the WebService, hosted of course by IIS, so your config file is the web.config.
If you had a console app which somehow used that class library (though probably not in an http handler context), then the config would be the app.config in the console app, not the app.config in your class library.

You need to put the configuration in your web.config file, not in assembly.dll.config: .NET does not (by design) read assembly.dll.config files.

Related

Could not find endpoint element in .Config File

Good day to all,
We've encountered some configuration problem while retrieving an end point from configuration file at creating a ChannelFactory object.
Could not find endpoint element with name 'no matter service'
and contract 'no matter contract' in the ServiceModel client configuration section. This might be
because no configuration file was found for your application, or
because no endpoint element matching this name could be found in the
client element.
The usual solution at this kind of exception is to add .config file to exe that runs this code.
In this case we're running this code at com+ so i'd created dllhost.exe.config file with all the endpoints.
The intresting thing is that i'm using this service also from a client application and there my wcf platform founds the same endpoint without any problem.
Other endpoints at dllhost.exe.config working fine i think.
By the way the problematic proxy that uses this service is customised Enterprise Library code.
While debugging the code it is defenetly runs under the dllhost process.
Is there some way to find out it what config file it looks for the endpoint?
Thank you.
Well, after a few days of checking and testing we'd found out 2 ways to solve the problem:
dllhost.exe.config - should not be edited/created/updated manually,but from MSI.Probably the reason is that is is placed at system32 directory and windows blocks it.
Better way - is to create for COM+ Service Application Root Directory and place there 2 files
ServiceName.config - Contains list of endpoints
ServiceName.manifest - Contains 3 rows
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
</assembly>

Using Configuration Manager

some of my web applications write to the disk on the web server. The paths change depending on location, prod and dev, etc. I used to store the paths in web.config under configuration / appSettings like:
<add key='PDFOutPutPath' value='C:\Temporary_Web_Files\PDFTempDocs\'/>
And then get them like this:
path = ConfigurationSettings.AppSettings('PDFOutPutPath')
Now in .Net 4, I get compile warnings about this being depreciated, so I found some instructions telling me to add a configuration file, move my values to it like so:
<configuration>
<appSetings>
<add key='PDFOutPutPath' value='C:\Temporary_Web_Files\PDFTempDocs\'/>
</appSettings>
</configuration>
and use configuration manager like so:
ConfigurationManager.AppSettings( 'PDFOutPutPath' )
However, this does not work. I'm not sure if I'm supposed to be using the configuration manager for this or not - If not, where do you put stuff like this? I have System.Configuration referenced, so this in not my issue.
So it appears the instructions you followed were a bit misleading.
You do not need a second file, you should delete the app.config file. You can place all of the configuration values in the web.config. Just make sure the config items are in the <appSetings> node. But you should continue to use the ConfigurationManager class within the code to access the values.

IIS WCF Service not using app.config

I have a .NET 3.5 WCF service hosted in IIS. The project service library has an app.config file with some configuration settings (Database connection strings, etc.). I deploy the project via a website project in Visual Studio, which generates a web.config to manage the endpoints. Is there a way I can put the app.config settings from my service library in to the web.config? The IIS hosted service seems to be using default values from the settings designer, and ignoring even an expliclty copied in app.config. I'm guessing this has something to do with the fact that a DLL can not utliize an app.config.
My service application is set up to pull the settings settings from the [MyAssembly].Properties.Settings.Default namespace.
Can you use external configuration files?
Your web.config:
<config>
...
<connectionStrings configSource="myConnections.config"></connectionStrings>
</config>
And then your external myConnections.config file:
<connectionStrings>
<add ... />
</connectionStrings>
You can have multiple external configuration files referenced from your main web.config file. See see this blog post for a nice explanation of how/why to do this.
I hope this helps!

how can i setup my nhibernate library to work in both a web and console application?

on the web application, I am using a NHibernate helper that looks up the session that was opened in a httpmodule (and committed there also).
<property name="current_session_context_class">web</property>
In the console application, what do I do?
Your options are: "call" & "thread_static". Have a look at this for more detailed explanation on all available contexts:
http://nhibernate.info/doc/nhibernate-reference/architecture.html#architecture-current-session
In order to have your library work for both a web and a console application you have two options:
Based on a application setting in the App.config and in the web.config have the Session Factory built accordingly by setting the current_session_context_class property manually in the code and remove it from the hibernate.cfg.xml file.
Include a in the web application's web.config and in the app.config of the console application. This way you can have the current_session_context_class property set to different values. If I am not mistaken the in the web.config and in the app.config overrides the values of the hibernate.cfg.xml. If I am wrong then you will just have to include in the web.config and the app.config the complete and remove the hibernate.cfg.xml file from your library.

How can I launch an executable from an IIS-hosted WCF service?

We have a WCF service that we recently switched from self-hosting to IIS-hosted. It needs to launch an executable using Process.Start(). This works fine when it's self-hosted, but when hosted in IIS, we get the error:
System.ComponentModel.Win32Exception: The system cannot find the file specified
We have the exe in both the bin directory and in the application root (next to the .svc file.) But, it can't seem to find the exe.
Any help would be appreciated. Thanks!
EDIT: I forgot to mention that we're launching using the following code:
Process.Start("LeakingWrapper.exe");
FURTHER INFO: Unfortunately, we don't have the option to switch the exe to a dll, because it is wrapping a third-party library that leaks memory. So, we have to put it into its own process to ensure our long-running WCF service doesn't leak!
Do you have the aspNetCompatibilityEnabled setting set to true? In that case, you'd have a HttpContext, which could try to use to call something like:
string exeFileName = HttpContext.Current.Server.MapPath("~/LeakingWrapper.exe")
Or: what if you specify the whole path to the EXE, e.g.
Process.Start("C:\yourServiceDir\bin\LeakingWrapper.exe")
Does that help at all??
Marc
marc_s answer is probably correct.
However, it could also be that the process cannont find the file because it does not have the rights to read the exe file.
To use the web service's path using HttpContext you must include the following line in your web service's web.config
<configuration>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>`