Jonathan Jenne 6c9fc1c9e6 jj
2019-01-11 16:05:39 +01:00

121 lines
4.8 KiB
VB.net

Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.EDMIFileOps.EDMIServiceReference
Imports System.ServiceModel
Imports System.IO
Public Class FileOp
Private _logger As Logger
Private _logConfig As LogConfig
Private _channelFactory As ChannelFactory(Of IEDMServiceChannel)
Private _channel As IEDMServiceChannel
Public Sub New(LogConfig As LogConfig, EDMI_ServiceAdress As String)
_logger = LogConfig.GetLogger()
_logConfig = LogConfig
Try
Dim binding As New NetTcpBinding()
binding.Security.Mode = SecurityMode.Transport
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
binding.MaxReceivedMessageSize = 2147483647
binding.MaxBufferSize = 2147483647
binding.MaxBufferPoolSize = 2147483647
binding.MaxConnections = 10000
binding.ReaderQuotas.MaxArrayLength = 2147483647
binding.ReaderQuotas.MaxStringContentLength = 2147483647
Dim endpointAddress = New EndpointAddress(EDMI_ServiceAdress)
_channelFactory = New ChannelFactory(Of IEDMServiceChannel)(binding, endpointAddress)
Connect2NetService()
Catch ex As Exception
_logger.Error(ex)
End Try
End Sub
Private Function Connect2NetService()
Try
_channel = Nothing
_channel = _channelFactory.CreateChannel()
_logger.Info("Successfully connected to EDM_Network Service")
AddHandler _channel.Faulted, AddressOf Reconnect
_channel.Open()
Return True
Catch ex As Exception
_logger.Error(ex)
Return True
End Try
End Function
Private Sub Reconnect()
_channel.Abort()
Connect2NetService()
End Sub
Public Async Function New_EDMI_File(oFILENAME As String, oUserName As String) As Task(Of String)
Try
Dim oFileGUID = Await New_EDMIFile_CreateContainer(oFILENAME)
Dim oFileRecordID = Nothing
If Not IsNothing(oFileGUID) Then
Dim oSQL = $"SELECT FNEDMI_SET_RECORD('TBEDMI_ADRESSE','{oUserName}',FALSE,NULL,'','{oFileGUID}') FROM rdb$database;"
oFileRecordID = Await New_EDMIFile_CreateDB_Record(oSQL)
End If
Return oFileRecordID
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Private Async Function New_EDMIFile_CreateContainer(oFILENAME As String) As Task(Of String)
Try
Dim sw As New Stopwatch()
sw.Start()
Dim oFileContents = File.ReadAllBytes(oFILENAME)
Dim oExtension As String = New FileInfo(oFILENAME).Extension.Substring(1)
Dim oFileGUID = Await _channel.CreateFileAsync(oFileContents, oExtension)
sw.Stop()
_logger.Info($"File successfully transferred - stopwatch: {sw.Elapsed.ToString}")
Return oFileGUID
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Private Async Function New_EDMIFile_CreateDB_Record(FBCommand As String) As Task(Of String)
Try
Dim oTimeTotal As TimeSpan
Dim oStopwatch As New Stopwatch()
oStopwatch.Start()
Dim oRecord_ID As String
Dim oRequestName = Await _channel.CreateDatabaseRequestAsync("CreateEDMFileRecord", True)
Dim oResult = Await _channel.ReturnScalarAsync(FBCommand)
oTimeTotal = oStopwatch.Elapsed
oStopwatch.Reset()
Await _channel.CloseDatabaseRequestAsync()
If Not oResult.OK Then
_logger.Warn($"Unexpected error while executing command: {oResult.ErrorMessage}")
Else
oRecord_ID = oResult.Scalar
_logger.Debug($"SCALAR (SERVICE) {FBCommand} - TIME: {(oTimeTotal.ToString)}")
End If
Return oRecord_ID
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
Public Async Function Load_EDMIFile_2TempPath(oEDMIFile_GUID As String) As Task(Of String)
Try
Dim oResult As EDMIServiceReference.ContainerResult = Await _channel.GetFileAsync(oEDMIFile_GUID)
Dim oTempPath = Path.Combine(Path.GetTempPath(), "EDMI_FileContainer")
Directory.CreateDirectory(oTempPath)
Dim oFilePath = Path.Combine(oTempPath, $"{oResult.Container.FileId}.{oResult.Container.Extension}")
File.WriteAllBytes(oFilePath, oResult.Container.Contents)
' Process.Start(oTempPath)
Return oTempPath
Catch ex As Exception
_logger.Error(ex)
Return Nothing
End Try
End Function
End Class