【问题标题】:VB.NET service referencing dll with a XML System.IO.FileLoadExceptionVB.NET 服务引用带有 XML System.IO.FileLoadException 的 dll
【发布时间】:2017-07-26 03:44:12
【问题描述】:

我在引用 dll 的 VB.NET 服务上遇到问题。我在 Windows 窗体应用程序中与同一个文件夹上的 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\ 文件,这些文件在没有导入库的情况下运行良好

【问题讨论】:

  • "library.xml" 通常不包含使用 .dll 所需的任何数据 - 它仅包含成员的文档。如果无法加载引用的 dll,则错误消息显示为 “无法加载文件或程序集 '此处的库程序集名称,版本 = 1.0.0.0,文化 = 中性,PublicKeyToken = 31bf3856ad364e35'或其依赖项之一。”。 您更有可能在服务构造函数中遇到此异常。您必须出示您的代码。
  • 为每个请求添加代码

标签: .net vb.net dll


【解决方案1】:

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

<startup useLegacyV2RuntimeActivationPolicy="true" /> 

【讨论】:

    猜你喜欢
    • 2021-11-30
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-06
    • 1970-01-01
    • 2014-04-24
    相关资源
    最近更新 更多