0

I have a problem on VB.NET service referencing a dll. I works well with Windows Form Application with the DLL on same folder. To be more specific the library has two files "library.dll" and "library.xml". The xml seem to have text that are required to the dll. When I remove the reference of DLL on Visual Studio, the service runs well. But when I add the library.dll to reference it doesn't work at all. Almost as referencing a dll in a windows service problem, and no solution there too.

I cannot enter even OnStart function so cannot log anything from my end. Here is what I get on event log:

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()

EDIT: My Code

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

Please ignore those config\ files, those are working well without the import in library

Community
  • 1
  • 1
Techjail
  • 1
  • 2
  • "library.xml" usually does not contain any data required to use the .dll - it only contains the documentation for the members. If a referenced dll cannot be loaded, the error message reads *"Could not load file or assembly 'library assembly name here, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies.".* More likely you are getting this exception in your services constructor. You will have to show your code. – Filburt Mar 06 '17 at 10:21
  • Added code per request – Techjail Mar 06 '17 at 10:33

1 Answers1

0

The library I was using was compiled on version 2.0.50727, on further investigation, I found https://stackoverflow.com/a/25538155/5072839 this solution. And now the application seem to be working perfectly. i.e. Added on App.config

<startup useLegacyV2RuntimeActivationPolicy="true" /> 
Community
  • 1
  • 1
Techjail
  • 1
  • 2