使用XML System.IO.FileLoadException

时间:2017-03-06 10:09:55

标签: .net vb.net dll

我在引用dll的VB.NET服务上遇到了问题。我使用Windows Form Application与DLL在同一文件夹上运行良好。更具体地说,库有两个文件“library.dll”和“library.xml”。 xml似乎有dll所需的文本。当我在Visual Studio上删除DLL的引用时,该服务运行良好。但是当我添加library.dll来引用它时根本不起作用。几乎是referencing a dll in a windows service问题,也没有解决方案。

我甚至无法输入OnStart功能,因此无法记录任何内容。以下是我在事件日志中获得的信息:

Application: MyService.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileLoadException
Stack:
   at MyService.MyService..ctor()
   at MyService.MyService.Main()

编辑:我的代码

Imports System.Threading
Imports FieldTalk
Imports System.IO
Imports System.Web.Script.Serialization

Public Class OPCModbusService
    Dim is_stopping = False

    Dim threadConfigurationChanged As Thread

    Dim tags As New Dictionary(Of String, Tag)
    Dim tags_group As New List(Of Dictionary(Of String, Object))
    Dim tags_ReadTime As Date
    Dim PLC_Address As New Dictionary(Of String, String)
    Dim PLC_Address_ReadTime As Date
    Dim MAC_address As String = ""
    Dim MAC_Address_ReadTime As Date

    Dim serializer As New JavaScriptSerializer()

    Public mbusProtocol As New Dictionary(Of String, MbusTcpMasterProtocol)

    Sub New()
        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        If Not EventLog.SourceExists(EventLog1.Source) Then
            EventLog.CreateEventSource(EventLog1.Source, EventLog1.Log)
        End If
    End Sub

    Private Function openProtocol(address As String) As Boolean
        Dim result As Integer
        result = mbusProtocol(address).openProtocol(PLC_Address(address))
        If (result <> BusProtocolErrors.FTALK_SUCCESS) Then
            EventLog1.WriteEntry("Error opening protocol: " + BusProtocolErrors.getBusProtocolErrorText(result))
            Return False
        End If
        Return True
    End Function

    Private Sub closeProtocol(address As String)
        Try
            mbusProtocol(address).closeProtocol()
        Catch ex As Exception

        End Try
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
        ' First we try to read configuration files
        EventLog1.WriteEntry("We are reading")
        ' READ SOME Config

        ' READING CONFIG ENDS
        ' Then we start with threads
        Try
            threadConfigurationChanged = New Thread(AddressOf checkChangeInConfig)
        Catch ex As Exception
            EventLog1.WriteEntry("Cannot open read of configuration change thread!!!" & vbNewLine & ex.Message)
            'Exit Sub
        End Try
        ' INITIALIZING SERVICES ENDS
    End Sub

    Private Sub checkChangeInConfig()
        While True
            Dim macFileInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(My.Application.Info.DirectoryPath + "\\config\\mac.txt")
            Dim serverFileInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(My.Application.Info.DirectoryPath + "\\config\\server.txt")
            Dim tagFileInfo As FileInfo = My.Computer.FileSystem.GetFileInfo(My.Application.Info.DirectoryPath + "\\config\\tags.csv")
            If macFileInfo.LastWriteTime > MAC_Address_ReadTime Then
                reloadMac()
            End If
            If is_stopping Then Exit Sub
            If serverFileInfo.LastWriteTime > PLC_Address_ReadTime Then
                reloadServer()
            End If
            If is_stopping Then Exit Sub
            If tagFileInfo.LastWriteTime > tags_ReadTime Then
                reloadTags()
                ' We may need to take care of extra things here
            End If
            If is_stopping Then Exit Sub
            For i = 0 To 10
                Thread.Sleep(100)
                If is_stopping Then Exit Sub
            Next
        End While
    End Sub

    Protected Overrides Sub OnStop()
        ' Yeah Yeah! We stop threads here
        is_stopping = True
        Try
            threadConfigurationChanged.Abort()
        Catch ex As Exception

        End Try
    End Sub

End Class

请忽略那些config\文件,这些文件在没有库中导入的情况下运行良好

1 个答案:

答案 0 :(得分:0)

我使用的库是在2.0.50727版本上编译的,经过进一步调查,我发现https://stackoverflow.com/a/25538155/5072839这个解决方案。现在应用程序似乎工作得很好。即在App.config上添加

<startup useLegacyV2RuntimeActivationPolicy="true" />