I got an C# exe which writes log4net logs. If I run this exe directly, the logging works ok.
However, if I call the exe from a F# script (with extension fsx), I have the error
log4net:ERROR Failed to find configuration section 'log4net' in the application'
s .config file. Check your .config file for the <log4net> and <configSections> e
lements. The configuration section should look like: <section name="log4net" typ
e="log4net.Config.Log4NetConfigurationSectionHandler,log4net" />
anyone can help? thanks a lot
You should insert configuration of this exe file to configuration file of your f# application or initialize logger in code. I am not sure that when you run f# script there are config file.
You can try to set config to configfile of your c# exe with code:
AppDomain.CurrentDomain.SetData("APP_CONFIG_FILE", "c:/test.config);
When you call your C# .exe from F# Interactive, the current working directory -- which is where .NET will try to load the .config file from -- is the directory where F# Interactive is installed.
In your F# interactive script, do something like this:
// Change the current directory to the directory where the
// C# assembly was loaded from.
System.Environment.CurrentDirectory <-
// TODO : Change 'MyType' to any public type from your C# assembly
typeof<FSharpx.Text.Lexing.Position>.Assembly.Location
EDIT : On second thought, I don't know if the above code will work -- it assumes the .config file will be lazily loaded (i.e., on-demand). If the CLR loads the .config file at the same time the .exe is loaded, you'll need to do something like this instead:
// Change the current directory *before* referencing the C# assembly.
System.Environment.CurrentDirectory <- #"C:\blah";;
// Reference the C# assembly
#r #"C:\blah\foobar.exe";;
Related
I am using electron-edge-js to call a C# DLL.
Within the DLL, the App.config file is called out for server connection information.
return (ServiceDetailConfiguration)ConfigurationManager.GetSection(ServiceDetailConfiguration.ServiceDetailConfigurationConst);
In my js file calling the dll, I can successfully call out to the dll, and process information. That is, until I hit the above line, I get a null exception thrown as it can't find the app.config.
I've tried the below:
Load WCF app.config bindings in edge.js app
working with electron-edge-js for existing dll with app.config
Without much luck.
In my package.json file, the name is "firstelectronapp".
When i build the file, I output it as "test.exe"
So i've attempted these files in the same root folder as the test.exe file with no luck:
first.exe.config
firstelectronapp.exe.config
test.exe.config
node.exe.config
Is there something I'm missing? I wrote a quick C# app with an app.config that calls the dll, and when stepping through it I can tell that's what the issue is.
The config file should be renamed electron.exe.config and placed in the same directory as electron.exe - i.e. node_modules\electron\dist.
I just ran into this when needing to add a bindingRedirect. Putting it in the above file solved it.
Also, if you're using electron-builder for packaging the app, you can easily include the config file in the packaged application by adding it to the extraFiles collection in the build configuration, for instance:
"extraFiles": [
{
"from": "app.config",
"to": "my-electron-app-name.exe.config"
}
]
I am writing a DLL which is loaded by a third party application (The FitNesse framework's test runner, the DLL is a test fixture).
The DLL has its own App.config file (say MyDll.dll.config), and I can tell FitNesse to load that App.config file.
But here's the problem: The App.config file contains custom config section handler, like this:
<configuration>
<configSections>
<sectionGroup name="myGroup">
<section name="MySection" type="MyNamespace.MyHandler.MySection, MyNamespace.MyHandler"/>
...
</sectionGroup>
</configSections>
...
</configuration>
Whenever the App.config file is read, the I get an exception saying that the MyNamespace.MyHandler assembly cannot be found, although it sits in the same folder as the MyDll.dll.config file being read:
System.TypeInitializationException:
The type initializer for
'MyNamespace.MyHandler.MySection'
threw an exception. --->
System.Configuration.ConfigurationErrorsException:
An error occurred creating the
configuration section handler for
myGroup/MySection: Could not load file
or assembly 'MyNamespace.MyHandler' or
one of its dependencies. The system
cannot find the file specified
I can see that the system looks for this file in the same folder where the executable that is loading my DLL is located. However, I do not want to copy my files into this third party directory or vice versa.
Is there a way to specify where the system should look for the DLLs to interpret the App.config file? A general solution or a FitNesse-specific solution would both work for me.
Thanks a lot in advance for any help!
You could try using the runtime section as explained here:
http://kbalertz.com/897297/consume-assemblies-located-folder-different-application-folder-Visual-Basic.aspx
Here's another solution we found ourselves. While the solution presented by Gregor S. indeed seems to enable us to read the App.config, we run into other problems regarding the working directory later. So what we are doing now, is adding the FitNesse Runner application as a link to our project, so that it is picked up from its original location and copied to our test application's folder before it is executed.
It's not nice, but it works.
I have an executable that consumes DLL’s through MEF. I am successfully loading each DLL’s config file's appsettings keys using
var appConfig = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
return appConfig.AppSettings.Settings["Version"].Value;
Now I want to make it so the DLL allows for adhoc items in the DLL’s config file.
So I added this to the config file
<configSections>
<section name="AdHocRules" type="BusinessRules.AdHocConfiguration, BusinessRules" />
</configSections>
<AdHocRules BaseRuleNumber="ConfigEx1" RuleName="Config Example" EffectiveDate="5/1/2010" Value="Example" IsValid="true"/>
And I created a class to read the above. And when I run this in a test console app that is not consuming the DLL – so everything is complied together and a single app config everything works fine
BUT – I want to use the DLL’s config file and I keep getting an error
Unable to cast object of type
'System.Configuration.DefaultSection'
to type
'BusinessRules.AdHocConfiguration
This is not working; - it's throwing the above
var cm = ConfigurationManager.OpenExeConfiguration(Assembly.GetExecutingAssembly().Location);
AdHocConfiguration adhoc = (AdHocConfiguration)cm.GetSection("AdHocRules");
And this is code – adhoc is null because it is not loading from the correct config file
AdHocConfiguration adhoc = (AdHocConfiguration)ConfigurationManager.GetSection("AdHocRules");
BusinessRules.Rule r = new BusinessRules.Rule();
r.BaseRuleNumber = adhoc.baserulenumber;
r.RuleName = adhoc.rulename;
r.EffectiveDate = adhoc.effectivedate;
r.Value = adhoc.value;
Any ideas?
In order to use the OpenExeConfiguration method, the configuration files for the other DLLs need to reside in the same directory as the executable file as they mention in the MSDN Reference.
You might need a post-build event to move the config files, but it does work.
Also you might want to use Assembly.GetAssembly(some type loaded by MEF).Location; rather than Assembly.GetExecutingAssembly().Location, depending on how you are using it.
I have a sample project where I load parts using MEF, and read their config files.
Let me know if you still have trouble
I have a config file in my C# class library called MyLibrary.config, in vs 2008.
I created another project, say a simple console app, add reference by "Browsing" the MyLibrary.dll in the bin directory of the class library project, and when I compile, the MyLibrary.config is not including in the bin directory of the output in the console app.
How can I set it so I can include it when I reference the dll?
Cheers
You can't. Your console application is expecting to find a config file with prefix the same as the name as the console application (MyConsoleApplication.exe -> MyConsoleApplication.exe.config.).
In some situations you can share a config file by using the file attribute on the appSettings element:
<appSettings
file="path">
</appSettings>
Note that path is relative to the executing assembly.
As a side note, DLLs do not even use the config file that you've defined in the project. Again, configuration information is read from the a config file with prefix the same as the executing assembly. Thus, even when MyLibrary.dll tries to yank configuration information out of a config file, it will be reading the config file for the executing assembly, not MyLibrary.dll.config.
For more on how config files work, see MSDN.
The standard way to use a config file is to have it the same as the executable, adding a reference to a dll will not include its config file and as far as I know dll's don't load config files on their own, rather they rely on the executable that reference them.
Beyond not being able to do this, I would also advise against this approach.
Rather than trying to tighly couple your settings to the DLL library, consider more of a "Dependency Injection" type approach - i.e. where you pass in the value dependencies (i.e. settings) to the code you are calling (i.e. the library).
The advantage in this is you are not tied to a single method of storing settings. You can then use a database, different config file formats... even hard-coding. It also makes unit testing easier by removing the external file dependency.
There are different ways to implement this, however one example is to pass the configuration settings into the constructor of the class(s) that uses them.
I have a .NET 3.5 class library I built that reads an App.config file for values it needs. It can pull the config values just fine when I test it in Visual Studio. To test it, I just change the project to a console application and execute a method call.
I have the need to call this class library from many other .NET programs, and I want the class library to be self sufficient (I should be able to call it from any other program, and it should use its own config file, not know about any calling config file etc.).
I can add a reference to the dll (since I am still development I am using VS 2008, haven't thrown anything into the GAC yet) but the App.config that the class library is reading is from the calling program's App.config, not the class library's App.config.
The class library dll has it's config file in the same directory, so it should be able to find it just fine, and the calling application is named differently. I am using the standard key value pairs in the App.config (e.g. name of config file myClassLibrary.dll.config) and getting values out with the following line of code:
String myVal = ConfigurationSettings.AppSettings["myConfigSetting"];
Does anyone know how to fix this?
An app domain in C# can have only one assembly level app.config file. See here on MSDN. An executable will always start up an AppDomain and by default look for a config file with name: EXECUTABLE_NAME.config. For example, SampleApp01.exe will look for SampleApp01.exe.config as its configuration file.
you can place your configs in the machine.config file inside the framework folder by this way you can globally use your configuration in all .Net applications running in that machine,
I believe app.config will always be used by the executable. Just drop it in that directory.
They would do that to ensure the dll can be shared and not have to share the same .config file.
You might be able to create a link from the executable .config file
<appSettings configSource="\lib\app.config">
Or change its name, i don't understand how you can have both app.config files in the same directory..don't they have the same name?
<appSettings configSource="\lib.app.config">
I can't find a way to avoid getting the app.config for the calling dll/exe etc. The only way I have found is to use a hardcoded path and load it that way. Here is code I am using to do that:
using System.Configuration;
...
public static KeyValueConfigurationCollection getAppSettingsFromAppConfig(String appConfigPath) {
ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
fileMap.ExeConfigFilename = appConfigPath;
Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
AppSettingsSection section = config.AppSettings;
KeyValueConfigurationCollection appsettings = section.Settings;
return appsettings;
}
You then have a collection of KeyValueConfigurationElement, which you can use .Value to get the string from config file with.