I have a Windows Forms application that I have been trying to upgrade to .NET Framework 4.8. It runs fine when debugging it in visual studio, but when I run it as task on our server it gets the following error:
Application: DirectoryLister.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.Configuration.ConfigurationErrorsException
at System.Configuration.BaseConfigurationRecord.EvaluateOne(System.String[], System.Configuration.SectionInput, Boolean, System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object)
at System.Configuration.BaseConfigurationRecord.Evaluate(System.Configuration.FactoryRecord, System.Configuration.SectionRecord, System.Object, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSectionRecursive(System.String, Boolean, Boolean, Boolean, Boolean, System.Object ByRef, System.Object ByRef)
at System.Configuration.BaseConfigurationRecord.GetSection(System.String)
at System.Configuration.ConfigurationManager.get_ConnectionStrings()
at DirectoryLister.Form1..ctor()
Exception Info: System.InvalidOperationException
at DirectoryLister.My.MyProject+MyForms.Create__Instance__[[System.__Canon, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]](System.__Canon)
at DirectoryLister.My.MyProject+MyForms.get_Form1()
at DirectoryLister.My.MyApplication.OnCreateMainForm()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(System.String[])
at DirectoryLister.My.MyApplication.Main(System.String[])
Here is the config file for it:
<?xml version="1.0"?>
<configuration >
<connectionStrings>
<add name="LBConn" connectionString="Data Source=DEVSQL2;Initial Catalog=LiveBait;Integrated Security=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="FMS" value="\\VMFMS\FMS\" />
<add key="FILEMOD" value="C:\Program Files (x86)\American Share Insurance\Application Modification App" />
<add key="FMS_REPORTS" value="\\VMFMS\FMS_REPORTS\" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
</appSettings>
<system.diagnostics>
<sources>
<!-- This section defines the logging configuration for My.Application.Log -->
<source name="DefaultSource" switchName="DefaultSwitch">
<listeners>
<add name="FileLog" />
<!-- Uncomment the below section to write to the Application Event Log -->
<!--<add name="EventLog"/>-->
</listeners>
</source>
</sources>
<switches>
<add name="DefaultSwitch" value="Information" />
</switches>
<sharedListeners>
<add name="FileLog" type="Microsoft.VisualBasic.Logging.FileLogTraceListener, Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" initializeData="FileLogWriter" />
<!-- Uncomment the below section and replace APPLICATION_NAME with the name of your application to write to the Application Event Log -->
<!--<add name="EventLog" type="System.Diagnostics.EventLogTraceListener" initializeData="APPLICATION_NAME"/> -->
</sharedListeners>
</system.diagnostics>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<system.web>
<membership defaultProvider="ClientAuthenticationershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>
</system.web>
</configuration>
And here is the code of the Form1.vb that is being launched:
Imports System
Imports System.IO
Imports System.Text
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Public Class Form1
'Private _stringBuilder As StringBuilder = New StringBuilder
Dim ocmd As SqlCommand
Dim reader As SqlDataReader
Dim oConn As New SqlConnection(ConfigurationManager.ConnectionStrings("LBConn").ToString)
Dim dModifiedStart As Date = DateAdd(DateInterval.Day, -1, Now()).ToString("MM/dd/yyyy 00:00")
Dim dModifiedEnd As Date = DateAdd(DateInterval.Day, -1, Now()).ToString("MM/dd/yyyy 23:59:59")
Dim strLastModified As String
Dim strFileLocation As String
Dim strFileName As String
Dim strFileExtension As String
Dim list As New List(Of String)
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Application.DoEvents()
Dim fs As New FileStream(System.Environment.CurrentDirectory & "\Running.txt", FileMode.Create, FileAccess.Write)
'declaring a FileStream and creating a word document file named file with
'access mode of writing
Dim s As New StreamWriter(fs)
'creating a new StreamWriter and passing the filestream object fs as argument
s.BaseStream.Seek(0, SeekOrigin.End)
'the seek method is used to move the cursor to next position to avoid text to be
'overwritten
s.WriteLine("This is program is currently running.")
'writing text to the newly created file
s.Close()
'closing the file
'Added: July 11, 2013
'Parse Exclusion file so we know which file extentions that must be ignored
'The purpose of putting it in a text file is we can then change the text file without changing the EXE code
Using File = System.IO.File.OpenText(System.Environment.CurrentDirectory & "\FileExclusion.txt")
While Not File.endofstream
Dim line As String = File.readline()
list.Add(line)
End While
End Using
oConn.Open()
For Each appSetting As String In ConfigurationManager.AppSettings.AllKeys
Dim appSettingValue = ConfigurationManager.AppSettings.Get(appSetting)
'Pass in the directory name and the site which we are monitoring
PrintFiles(appSettingValue, appSetting)
Next
Catch ex As Exception
Call WriteLog("Main_Load: " & ex.Message)
Finally
oConn.Close()
If File.Exists("Running.txt") Then
File.Delete("Running.txt")
End If
Call WriteLog("---------------------------------------------")
End Try
Me.Close()
End Sub
Private Function PrintFiles(ByVal root As String, ByVal site As String) As String
'Write all the files in this folder
For Each fileName As String In Directory.GetFiles(root)
'MsgBox(Path.GetExtension(fileName).ToString)
strFileExtension = Path.GetExtension(fileName).ToString
If (Not Path.GetFullPath(fileName) Like "*_vti*" And _
Not Path.GetFullPath(fileName) Like "*_private" And _
Not Path.GetFullPath(fileName) Like "*pdf" And _
Not Path.GetFullPath(fileName) Like "*NoteFiles" And _
Not Path.GetFullPath(fileName) Like "*$tf" And _
Not Path.GetFullPath(fileName) Like "*cfxtemp") Then
If Not list.Contains(strFileExtension) Then
strLastModified = System.IO.File.GetLastWriteTime(fileName).ToString("MM/dd/yyyy hh:mm tt")
'check to see if the file has changed the previous day if so then add to database
If DateDiff(DateInterval.Day, CDate(strLastModified), dModifiedStart) >= 0 _
And DateDiff(DateInterval.Day, CDate(strLastModified), dModifiedEnd) <= 0 Then
If site = "FMS" Then
If Path.GetDirectoryName(fileName).Substring(0, 2) = "\\" Then
strFileLocation = Replace(Path.GetDirectoryName(fileName).ToString, "\\", "", 1, 1)
strFileLocation = Replace(strFileLocation, "VMFMS\FMS", "T:\")
strFileLocation = Replace(strFileLocation, "\\", "\")
End If
Else
strFileLocation = Path.GetDirectoryName(fileName)
End If
strFileName = Path.GetFileName(fileName)
'We do not need to grab the log file and add it as it will be changing nightly
If Not strFileName = "DirectoryLister.exe_LogFile.txt" Then
'add record to the database
Dim ocmd As New SqlCommand("sp_apFileChanges", oConn)
ocmd.CommandType = CommandType.StoredProcedure
With ocmd.Parameters
.Add(New SqlParameter("@FileName", strFileName))
.Add(New SqlParameter("@FileLocation", strFileLocation))
.Add(New SqlParameter("@ModifiedDate", strLastModified))
.Add(New SqlParameter("@Site", site))
End With
ocmd.ExecuteNonQuery()
End If
End If
End If
End If
Next
'If there are directories in this folder process them
For Each foundDir As String In Directory.GetDirectories(root)
Try
PrintFiles(foundDir, site)
Catch ex As Exception
Call WriteLog("PrintFiles: " & ex.Message)
End Try
Next
Return "Done"
End Function
Public Sub WriteLog(ByVal sLogEntry As String)
Dim objStreamWriter As StreamWriter
'Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = New StreamWriter(Application.ExecutablePath & "_LogFile.txt", True)
'Write a line of text.
objStreamWriter.WriteLine(Date.Now.ToLongDateString & vbTab & Date.Now.ToLongTimeString & vbTab & sLogEntry)
'Close the file.
objStreamWriter.Close()
End Sub
End Class
The user running the tasks has full permissions on everything for the installed directory.
Any ideas on what could be happening?
To help debug it I moved the declaration of the connection string to my Form1_Load sub and caught the exception. The actual error was that the connection string was already added, in this case from a machine.config file on the server.