Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Implementing IDisposable in a class
Message
De
11/02/2014 19:51:31
 
 
À
11/02/2014 16:39:18
Information générale
Forum:
ASP.NET
Catégorie:
Code, syntaxe and commandes
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Divers
Thread ID:
01594093
Message ID:
01594109
Vues:
56
I have done a lot of benchmarks today and here is the result.

One specific robot has to communicate with a Web Service which requires a SOAP SVC approach.

So, basically, in the robot, I have code like this:
                    ' Create the object
                    Using loSOAPSVC = New Framework.SOAPSVC(oApp)

                        ' Initialization
                        loSOAPSVC.cUrl = Trim(lcURL)
                        loSOAPSVC.nBasicHttpSecurityMode = 1
                        loSOAPSVC.AddHTTPHeader("certificate", lcCertificate)

                        ' If we cannot initialize
                        If Not loSOAPSVC.Initialize() Then

                        End If

                        ' Bind the mode and the URL
                        Using loMyService = New MyService.MyServiceSoapClient(loSOAPSVC.oBasicHttpBinding, loSOAPSVC.oEndpointAddress)

                            ' Add the hook to intercept the envelope
                            loMyService.Endpoint.Behaviors.Add(New Framework.CustomBehavior(loSOAPSVC))

                            ' Now that our object is created, we can call the framework AttachHTTPHeader method to add the proper header
                            loSOAPSVC.oOperationContextScope = New System.ServiceModel.OperationContextScope(loMyService.InnerChannel)

                            ' If we cannot attach the HTTP header
                            If Not loSOAPSVC.AttachHTTPHeader() Then

                            End If

                            Try

                                ' Send the assignment
                                lcXML = loClaimService.SaveClaim(loXml.cXml)
The robot runs 24/7 and sends thousands of transactions. Thus, this code is executed a lot. I have a lot of robots and never had a problem with memory before. However, in this one, something is not released from memory and I cannot find exactly what it is and I am not sure if I can control it.

If I run this code but include a return after this statement:
                        Using loMyService = New MyService.MyServiceSoapClient(loSOAPSVC.oBasicHttpBinding, loSOAPSVC.oEndpointAddress)
...this will not cause any problem in memory.

So, the problem relies when this code is being executed:
                            ' Add the hook to intercept the envelope
                            loMyService.Endpoint.Behaviors.Add(New Framework.CustomBehavior(loSOAPSVC))

                            ' Now that our object is created, we can call the framework AttachHTTPHeader method to add the proper header
                            loSOAPSVC.oOperationContextScope = New System.ServiceModel.OperationContextScope(loMyService.InnerChannel)

                            ' If we cannot attach the HTTP header
                            If Not loSOAPSVC.AttachHTTPHeader() Then

                            End If

                            Try

                                ' Send the assignment
                                lcXML = loClaimService.SaveClaim(loXml.cXml)
So, it seems whenever an interaction is done at the SOAP SVC level that something is not released from memory.

If someone is wondering about the SOAPSVC class, here is the code:
Imports System.ServiceModel
Imports System.ServiceModel.Dispatcher
Imports System.ServiceModel.Configuration

Public Class SOAPSVC
    Implements IDisposable

    Public cMessage As String = ""
    Public cNamespace As String = ""
    Public cSOAPMessageReceived As String = ""
    Public cSOAPMessageSent As String = ""
    Public cUrl As String = ""
    Public lSuccess As Boolean = False
    Public nBasicHttpSecurityMode As Integer = 0
    Public oBasicHttpBinding As System.ServiceModel.BasicHttpBinding = Nothing
    Public oEndpointAddress As System.ServiceModel.EndpointAddress = Nothing
    Public oMessageHeader As Collection = New Collection
    Public oOperationContextScope As OperationContextScope = Nothing
    Private oApp As Framework.App = Nothing
    Private oHTTPHeader As Collection = New Collection
    Private oProcess As Framework.LXProcess = Nothing

    ' This is when we access the data provider in desktop mode
    Sub New(ByVal toApplication As Framework.App)
        oApp = toApplication
    End Sub

    ' This is when we access the data provider in a Web mode
    Public Sub New(ByVal toProcess As Framework.LXProcess)
        oProcess = toProcess
        oApp = oProcess.oApp
    End Sub

    ' Initialize
    Public Function Initialize() As Boolean

        ' Reset the values
        cMessage = ""
        cSOAPMessageReceived = ""
        cSOAPMessageSent = ""
        lSuccess = False

        ' Initialization
        oBasicHttpBinding = New System.ServiceModel.BasicHttpBinding

        ' Set the URL
        oEndpointAddress = New System.ServiceModel.EndpointAddress(cUrl)

        ' Set the mode
        Select Case nBasicHttpSecurityMode

            ' HTTPS
            Case 1
                oBasicHttpBinding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport

        End Select

        lSuccess = True

        Return lSuccess
    End Function

    ' Attach the HTTP header
    Public Function AttachHTTPHeader() As Boolean
        Dim lcName As String = ""
        Dim lcValue As String = ""
        Dim loObject(2) As Object
        Dim loHttpRequestMessageProperty As System.ServiceModel.Channels.HttpRequestMessageProperty = New System.ServiceModel.Channels.HttpRequestMessageProperty()

        ' Reset the values
        lSuccess = False

        ' If we have at least one HTTP header
        If oHTTPHeader.Count > 0 Then

            ' For each header
            For Each loObject In oHTTPHeader

                ' Initialization
                lcName = loObject(1)
                lcValue = loObject(2)

                ' Add the header
                loHttpRequestMessageProperty.Headers(lcName) = lcValue

            Next

            ' Set the header
            OperationContext.Current.OutgoingMessageProperties(System.ServiceModel.Channels.HttpRequestMessageProperty.Name) = loHttpRequestMessageProperty

        End If

        ' Reset the values
        oHTTPHeader.Clear()

        lSuccess = True

        Return lSuccess
    End Function

    ' Reset the values
    Public Function Reset() As Boolean

        ' Reset the values
        oHTTPHeader.Clear()
        oMessageHeader.Clear()

        Return True
    End Function

    ' Add a message header
    ' expC1 Header
    ' expO1 Value
    Public Function AddMessageHeader(ByVal tcHeader As String, ByVal tcValue As String) As Boolean
        Dim loObject(2) As Object

        ' Initialization
        loObject(1) = tcHeader
        loObject(2) = tcValue

        oMessageHeader.Add(loObject)

        Return True
    End Function

    ' Add a HTTP header
    ' expC1 Header
    ' expO1 Value
    Public Function AddHTTPHeader(ByVal tcHeader As String, ByVal tcValue As String) As Boolean
        Dim loObject(2) As Object

        ' Initialization
        loObject(1) = tcHeader
        loObject(2) = tcValue

        oHTTPHeader.Add(loObject)

        Return True
    End Function

    Public Sub Dispose() Implements IDisposable.Dispose
    End Sub

End Class

Public Class MyMessageInspector
    Implements IClientMessageInspector

    Private oSOAPSVC As SOAPSVC = Nothing

    Public Sub New(ByVal toSOAPSVC As SOAPSVC)
        oSOAPSVC = toSOAPSVC
    End Sub

    Public Sub AfterReceiveReply(ByRef toMessage As System.ServiceModel.Channels.Message, toCorrelationState As Object) Implements IClientMessageInspector.AfterReceiveReply

        ' SOAP message
        oSOAPSVC.cSOAPMessageReceived = toMessage.ToString

    End Sub

    Public Function BeforeSendRequest(ByRef toMessage As System.ServiceModel.Channels.Message, toIClientChannel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
        If False Then
            Dim lcName As String = ""
            Dim lcValue As String = ""
            Dim loMessageHeader As MessageHeader(Of String) = Nothing
            Dim loObject(2) As Object
            Dim loUntyped As Object = Nothing

            ' toMessage.Headers.Add(untyped)

            ' If we have at least one message header
            If oSOAPSVC.oMessageHeader.Count > 0 Then

                ' For each header
                For Each loObject In oSOAPSVC.oMessageHeader

                    ' Initialization
                    lcName = loObject(1)
                    lcValue = loObject(2)

                    ' Add the header
                    loMessageHeader = New MessageHeader(Of String)(lcValue)
                    loUntyped = loMessageHeader.GetUntypedHeader(lcName, oSOAPSVC.cNamespace)

                Next

            End If

        End If

        ' Reset the values
        oSOAPSVC.oMessageHeader.Clear()

        ' SOAP message
        oSOAPSVC.cSOAPMessageSent = toMessage.ToString

        Return Nothing
    End Function

    Public Function AfterReceiveRequest(ByRef toMessage As Message, toIClientChannel As IClientChannel, _
     toInstanceContext As InstanceContext) As Object
        Dim loMessage As Message = toMessage

        ' Request

        Return loMessage
    End Function

    Public Sub BeforeSendReply(ByRef toMessage As Message, toCorrelationState As Object)
        Dim loMessage As Message = toMessage

        ' Reply

    End Sub

End Class

Public Class CustomBehavior
    Inherits BehaviorExtensionElement

    Implements IEndpointBehavior

    Private oSOAPSVC As SOAPSVC = Nothing

    Public Sub New(ByVal toSOAPSVC As SOAPSVC)
        oSOAPSVC = toSOAPSVC
    End Sub

    Public Sub AddBindingParameters(toServiceEndpoint As System.ServiceModel.Description.ServiceEndpoint, _
     toBindingParameterCollection As System.ServiceModel.Channels.BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
    End Sub

    Public Sub ApplyClientBehavior(toServiceEndpoint As System.ServiceModel.Description.ServiceEndpoint, toClientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        toClientRuntime.MessageInspectors.Add(New MyMessageInspector(oSOAPSVC))
    End Sub

    Public Sub ApplyDispatchBehavior(toServiceEndpoint As System.ServiceModel.Description.ServiceEndpoint, _
     toEndpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
    End Sub

    Public Sub Validate(toServiceEndpoint As System.ServiceModel.Description.ServiceEndpoint) Implements IEndpointBehavior.Validate
    End Sub

    Public Overrides ReadOnly Property BehaviorType() As Type
        Get
            Return GetType(CustomBehavior)
        End Get
    End Property

    Protected Overrides Function CreateBehavior() As Object
        Return New CustomBehavior(oSOAPSVC)
    End Function

End Class
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform