DevT
DevT

Reputation: 4933

Windows service not getting values from App.config

I created simple windows service as below:

   private void TraceService(string content)
        {
            try
            {
                string path = ConfigurationManager.AppSettings["TextLocation"];

                FileStream fs = new FileStream(@path, FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter sw = new StreamWriter(fs);             
                sw.BaseStream.Seek(0, SeekOrigin.End);                
                sw.WriteLine(content);         
                sw.Flush();             
                sw.Close();
            }
            catch (Exception ex)
            {
                FileStream fs = new FileStream(@"D:\Error.txt", FileMode.OpenOrCreate, FileAccess.Write);            
                StreamWriter sw = new StreamWriter(fs);              
                sw.BaseStream.Seek(0, SeekOrigin.End);               
                sw.WriteLine(ex.Message);
                sw.Flush();               
                sw.Close();
            }
        }

if I run this service in VS command prompt using Installutil then it gets the value from app.config. but i need to create installer using wixInstaller. when i run the installer and start service it throws an exception and message saved as "Path cannot be null." What is the reason for this? How can i solve this?

EDIT: My App.config as below:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="TextLocation" value="d:\ScheduledServiceNew.txt"/>
  </appSettings>
</configuration>

my Wix Installer File :

<?xml version="1.0" encoding="utf-8"?>
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi" xmlns:util="http://schemas.microsoft.com/wix/UtilExtension">
    <Product Id="5AC767F2-4F21-45DF-9DAB-C013C62B2B67" Name="Installer" Language="1033" Version="1.0.0.0" Manufacturer="TRT " UpgradeCode="3fd83540-23f6-45f4-bb1c-e77c12725680">
        <Package InstallerVersion="200" Compressed="yes" />
        <Media Id="1" Cabinet="SampleApp.cab" EmbedCab="yes" />
        <Directory Id="TARGETDIR" Name="SourceDir">
            <Directory Id="ProgramFilesFolder">
                <Directory Id="INSTALLFOLDER" Name="Installer">
                    <Directory Id="TestService" Name="TestService">
                        <Component Id="MainExecutable" Guid="CE30D5D8-3211-4890-A9DC-0C315C15B39E">
                            <File Id="testServiceexe" Name="TestInstallerProject.exe" DiskId="1" KeyPath="yes" Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe" />
                            <File Id="App.config" Name="App.config" Source="..\TestInstallerProject\App.config" />
                            <ServiceInstall Name="ScheduledService" Type="ownProcess" Start="auto" ErrorControl="ignore" Id="ServiceInstaller" DisplayName="ScheduledService" Account="[SERVICEACCOUNT]" Password="[SERVICEACCOUNTPASSWORD]">
                                <util:ServiceConfig FirstFailureActionType="restart" SecondFailureActionType="restart" ThirdFailureActionType="restart" ResetPeriodInDays="1" RestartServiceDelayInSeconds="1" />
                            </ServiceInstall>
                            <ServiceControl Id="StartService" Name="ScheduledService" Start="install" Stop="both" Remove="uninstall" Wait="no" />
                            <RemoveFolder Id="INSTALLDIR" On="uninstall" />
                        </Component>
                    </Directory>
                </Directory>
            </Directory>
        </Directory>
        <Feature Id="Complete" Title="Installer" Level="1">
            <ComponentRef Id="MainExecutable" />
        </Feature>
        <UI />
    </Product>
</Wix>

Upvotes: 3

Views: 6862

Answers (1)

ChaosPandion
ChaosPandion

Reputation: 78262

I believe the problem is with the name of the config file. App.config may be correct within the context of your solution but the resulting exe will expect the name TestInstallerProject.exe.config. A solution is not clear to me at this point but I will do some reading.

The solution may be as simple as changing the Source property to this: Source="..\TestInstallerProject\bin\Debug\TestInstallerProject.exe.config"

Upvotes: 5

Related Questions