EDMIService: Move service configuration to code

This commit is contained in:
Jonathan Jenne 2020-12-09 16:35:54 +01:00
parent 1e5a05832d
commit b89ca3aa5a
6 changed files with 116 additions and 12 deletions

View File

@ -7,8 +7,8 @@ Public Class Channel
' https://social.msdn.microsoft.com/Forums/vstudio/en-US/d6e234d3-942f-4e9d-8470-32618d3f3212/maxbufferpoolsize-vs-maxbuffersize?forum=wcf ' https://social.msdn.microsoft.com/Forums/vstudio/en-US/d6e234d3-942f-4e9d-8470-32618d3f3212/maxbufferpoolsize-vs-maxbuffersize?forum=wcf
Public Const MAX_RECEIVED_MESSAGE_SIZE = 2147483647 ' 1GB Public Const MAX_RECEIVED_MESSAGE_SIZE = 2147483647 ' 1GB
Public Const MAX_BUFFER_SIZE = 2147483647 ' 10MB Public Const MAX_BUFFER_SIZE = 104857600 ' 100MB
Public Const MAX_BUFFER_POOL_SIZE = 2147483647 ' 40MB Public Const MAX_BUFFER_POOL_SIZE = 1048576 ' 1MB
Public Const MAX_CONNECTIONS = 500 Public Const MAX_CONNECTIONS = 500
Public Const MAX_ARRAY_LENGTH = 2147483647 Public Const MAX_ARRAY_LENGTH = 2147483647
@ -19,7 +19,6 @@ Public Class Channel
.MaxReceivedMessageSize = MAX_RECEIVED_MESSAGE_SIZE, .MaxReceivedMessageSize = MAX_RECEIVED_MESSAGE_SIZE,
.MaxBufferSize = MAX_BUFFER_SIZE, .MaxBufferSize = MAX_BUFFER_SIZE,
.MaxBufferPoolSize = MAX_BUFFER_POOL_SIZE, .MaxBufferPoolSize = MAX_BUFFER_POOL_SIZE,
.MaxConnections = MAX_CONNECTIONS,
.TransferMode = TransferMode.Streamed, .TransferMode = TransferMode.Streamed,
.Security = New NetTcpSecurity() With { .Security = New NetTcpSecurity() With {
.Mode = SecurityMode.Transport, .Mode = SecurityMode.Transport,

View File

@ -25,8 +25,19 @@
</diagnostics> </diagnostics>
<bindings> <bindings>
<netTcpBinding> <netTcpBinding>
<binding name="tcpBinding" sendTimeout="00:10:00" transferMode="Streamed" maxBufferSize="2147483647" maxBufferPoolSize="2147483647" maxReceivedMessageSize="2147483647"> <binding name="tcpBinding"
<readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" /> transferMode="Streamed"
sendTimeout="00:10:00"
receiveTimeout="00:10:00"
maxBufferSize="2147483647"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas
maxDepth="32"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="Transport"> <security mode="Transport">
<transport clientCredentialType="Windows" /> <transport clientCredentialType="Windows" />
</security> </security>

View File

@ -1,12 +1,14 @@
Imports DigitalData.Modules.Database Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Filesystem Imports DigitalData.Modules.Filesystem
Imports DigitalData.Modules.language Imports DigitalData.Modules.Language
Imports DigitalData.Modules Imports DigitalData.Modules
Imports System.IO Imports System.IO
Imports System.ServiceModel Imports System.ServiceModel
Imports System.Data.SqlClient Imports System.Data.SqlClient
Imports DigitalData.Services.EDMIService.Results Imports DigitalData.Services.EDMIService.Results
Imports System.ServiceModel.Description
Imports System.ServiceModel.Channels
<ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)> <ServiceBehavior(InstanceContextMode:=InstanceContextMode.PerSession)>
Public Class EDMIService Public Class EDMIService
@ -27,6 +29,18 @@ Public Class EDMIService
Private ReadOnly _debug As Boolean = False Private ReadOnly _debug As Boolean = False
Private ReadOnly _username As String Private ReadOnly _username As String
Public Shared Sub Configure(Config As ServiceConfiguration)
Dim oBaseAddress = Config.BaseAddresses.Item(0)
Dim oBinding = EDMI.API.Channel.GetBinding()
Dim oAddress = New EndpointAddress(oBaseAddress)
' See: https://stackoverflow.com/questions/42327988/addserviceendpoint-throws-key-is-null
Dim oDescription = ContractDescription.GetContract(GetType(IEDMIService), GetType(EDMIService))
Dim oEndpoint As New ServiceEndpoint(oDescription, oBinding, oAddress)
Config.AddServiceEndpoint(oEndpoint)
Config.Description.Behaviors.Add(New ServiceDebugBehavior With {.IncludeExceptionDetailInFaults = True})
End Sub
Public Sub New() Public Sub New()
Dim oOperationContext As OperationContext = OperationContext.Current Dim oOperationContext As OperationContext = OperationContext.Current
Dim oInstanceContext As InstanceContext = oOperationContext.InstanceContext Dim oInstanceContext As InstanceContext = oOperationContext.InstanceContext

View File

@ -134,6 +134,7 @@
<Compile Include="Scheduler.vb" /> <Compile Include="Scheduler.vb" />
<Compile Include="Scheduler\DatatableJob.vb" /> <Compile Include="Scheduler\DatatableJob.vb" />
<Compile Include="Scheduler\JobListener.vb" /> <Compile Include="Scheduler\JobListener.vb" />
<Compile Include="ServiceHost.vb" />
<Compile Include="WindowsService.vb"> <Compile Include="WindowsService.vb">
<SubType>Component</SubType> <SubType>Component</SubType>
</Compile> </Compile>

View File

@ -0,0 +1,65 @@
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Description
Public Class ServiceHost(Of T)
Inherits ServiceHost
Public Sub New(baseAddresses As Uri())
MyBase.New(GetType(T), baseAddresses)
End Sub
Public Sub EnableMetadataExchange(ByVal Optional EnableHttpGet As Boolean = True)
If State = CommunicationState.Opened Then
Throw New InvalidOperationException("Host is already opened")
End If
Dim oMetadataBehavior As ServiceMetadataBehavior = Description.Behaviors.Find(Of ServiceMetadataBehavior)()
If oMetadataBehavior Is Nothing Then
oMetadataBehavior = New ServiceMetadataBehavior()
Description.Behaviors.Add(oMetadataBehavior)
If BaseAddresses.Any(Function(uri) uri.Scheme = "http") Then
oMetadataBehavior.HttpGetEnabled = EnableHttpGet
Else
oMetadataBehavior.HttpGetEnabled = False
End If
End If
AddAllMexEndPoints()
End Sub
Public ReadOnly Property HasMexEndpoint As Boolean
Get
Return Description.Endpoints.Any(Function(endpoint) endpoint.Contract.ContractType = GetType(IMetadataExchange))
End Get
End Property
Public Sub AddAllMexEndPoints()
Debug.Assert(HasMexEndpoint = False)
For Each baseAddress As Uri In BaseAddresses
Dim oBinding As Binding = Nothing
Select Case baseAddress.Scheme
Case "net.tcp"
oBinding = MetadataExchangeBindings.CreateMexTcpBinding()
Exit Select
Case "net.pipe"
oBinding = MetadataExchangeBindings.CreateMexNamedPipeBinding()
Exit Select
Case "http"
oBinding = MetadataExchangeBindings.CreateMexHttpBinding()
Exit Select
Case "https"
oBinding = MetadataExchangeBindings.CreateMexHttpsBinding()
Exit Select
End Select
If oBinding IsNot Nothing Then
AddServiceEndpoint(GetType(IMetadataExchange), oBinding, "mex")
End If
Next
End Sub
End Class

View File

@ -5,11 +5,12 @@ Imports DigitalData.Modules.Database
Imports DigitalData.Modules Imports DigitalData.Modules
Imports System.ServiceModel.Description Imports System.ServiceModel.Description
Imports DigitalData.Modules.Config Imports DigitalData.Modules.Config
Imports System.ServiceModel.Channels
Public Class WindowsService Public Class WindowsService
Inherits ServiceBase Inherits ServiceBase
Private _ServiceHost As ServiceHost Private _ServiceHost As ServiceHost(Of EDMIService)
Private _LogConfig As LogConfig Private _LogConfig As LogConfig
Private _Logger As Logger Private _Logger As Logger
@ -37,7 +38,7 @@ Public Class WindowsService
Try Try
Dim oServicePath As String = AppDomain.CurrentDomain.BaseDirectory Dim oServicePath As String = AppDomain.CurrentDomain.BaseDirectory
_LogConfig = New LogConfig(LogConfig.PathType.CustomPath, oServicePath) _LogConfig = New LogConfig(LogConfig.PathType.CustomPath, IO.Path.Combine(oServicePath, "Log"))
_LogConfig.Debug = True _LogConfig.Debug = True
_Logger = _LogConfig.GetLogger() _Logger = _LogConfig.GetLogger()
_Logger.Info("Service {0} is starting...", SERVICE_DISPLAY_NAME) _Logger.Info("Service {0} is starting...", SERVICE_DISPLAY_NAME)
@ -79,14 +80,27 @@ Public Class WindowsService
EDMIService.Scheduler = _Scheduler EDMIService.Scheduler = _Scheduler
_Logger.Debug("Starting WCF ServiceHost") _Logger.Debug("Starting WCF ServiceHost")
_ServiceHost = New ServiceHost(GetType(EDMIService))
Dim oBaseAddresses() As Uri = {New Uri("net.tcp://localhost:9000/DigitalData/Services/Main")}
_ServiceHost = New ServiceHost(Of EDMIService)(oBaseAddresses)
_ServiceHost.EnableMetadataExchange(False)
_Logger.Debug("Listing Endpoints:")
For Each oEndpoint In _ServiceHost.Description.Endpoints
_Logger.Debug("Name: {0}", oEndpoint.Name)
_Logger.Debug("Address: {0}", oEndpoint.Address.ToString)
_Logger.Debug("Listen Uri: {0}", oEndpoint.ListenUri.AbsoluteUri)
_Logger.Debug("Binding: {0}", oEndpoint.Binding.Name)
_Logger.Debug("Contract: {0}", oEndpoint.Contract.Name)
Next
_ServiceHost.Open() _ServiceHost.Open()
_Logger.Info("WCF ServiceHost started") _Logger.Info("WCF ServiceHost started")
_Logger.Info("Service {0} successfully started", SERVICE_DISPLAY_NAME) _Logger.Info("Service {0} successfully started", SERVICE_DISPLAY_NAME)
Catch ex As Exception Catch ex As Exception
_Logger.Error(ex, "Failed to start the service host!") _Logger.Warn("Unexpected Error while starting the service: {0}", ex.Message)
_Logger.Error(ex)
GracefullyStop() GracefullyStop()
End Try End Try
End Sub End Sub