2017-03-06 34 views
0

我有一個VB.NET服務引用DLL的問題。我在Windows窗體應用程序和DLL在同一文件夾上運行良好。更具體地說,該庫有兩個文件「library.dll」和「library.xml」。該XML似乎有文本所需的DLL。當我在Visual Studio中刪除DLL的引用時,該服務運行良好。但是,當我添加library.dll引用它根本不起作用。幾乎和referencing a dll in a windows service的問題一樣,也沒有解決辦法。VB.NET服務引用DLL的XML System.IO.FileLoadException

我甚至無法輸入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\文件,那些在圖書館沒有進口運作良好

+0

「library.xml」通常不包含使用.dll所需的任何數據 - 它只包含成員的文檔。如果無法加載引用的dll,則會顯示錯誤消息*「無法在此加載文件或程序集庫程序集名稱,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = 31bf3856ad364e35'或其某個依賴項。可能你在服務構造函數中得到了這個異常。你將不得不顯示你的代碼。 – Filburt

+0

每個請求添加代碼 – Techjail

回答

0

圖書館我使用的版本是2.0.50727,經進一步調查,我找到了https://stackoverflow.com/a/25538155/5072839這個解決方案。而現在,這個應用程序似乎完美運行。即在App.config上添加

<startup useLegacyV2RuntimeActivationPolicy="true" />