Hi, I am using .NET Remoting for database access.
I have three components:
1. A class library 'RemoteComponent.dll':
Imports System
Imports System.Data.SqlClient
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
<Serializable()> _
Public Class RemoteClass
Inherits MarshalByRefObject
Private oConnection_m As SqlConnection
Private oTransaction_m As SqlTransaction
Private Function ConnectionString() As String
Return "user id=sa;password=;Database=Northwind;Server=localhost;Connect Timeout=30"
End Function
Public Sub BeginTransaction()
Dim oCnn As SqlConnection
Dim oTrx As SqlTransaction
oCnn = New SqlConnection(ConnectionString)
If oCnn.State = ConnectionState.Closed Then
oCnn.Open()
End If
oTrx = oCnn.BeginTransaction
oConnection_m = oCnn
oTransaction_m = oTrx
End Sub
Public Sub CommitTransaction()
oTransaction_m.Commit()
oConnection_m.Close()
oTransaction_m.Dispose()
oTransaction_m = Nothing
oConnection_m.Dispose()
oConnection_m = Nothing
End Sub
Public Overloads Function RunSQL( _
ByVal cSQL As String, _
ByVal cTableName As String) As DataSet
Dim oCmd As SqlCommand = New SqlCommand
Dim oCnn As SqlConnection = Nothing
Dim oAdp As SqlDataAdapter = New SqlDataAdapter
Dim oDs As DataSet = New DataSet
Try
oCnn = oTransaction_m.Connection
With oCmd
.Connection = oCnn
.CommandText = cSQL
.CommandType = CommandType.Text
.Transaction = oTransaction_m
End With
With oAdp
.SelectCommand = oCmd
.Fill(oDs, cTableName)
End With
If IsNothing(oTransaction_m) Then
If Not oCnn Is Nothing And oCnn.State <> ConnectionState.Closed Then
oCnn.Close()
End If
End If
Return oDs
Catch ex As Exception
Throw New System.Exception(ex.Message)
End Try
End Function
End Class
2. A console application 'Listener.exe':
Imports System
imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class Listener
Public Shared Function Main(ByVal args() As String) As Integer
Dim channel As TcpChannel = New TcpChannel(8080)
ChannelServices.RegisterChannel(channel)
RemotingConfiguration.RegisterWellKnownServiceType( _
GetType(RemoteComponent.RemoteClass), _
"RemoteComponent.RemoteClass", _
WellKnownObjectMode.SingleCall)
System.Console.WriteLine("Press the enter key to exit...")
System.Console.ReadLine()
Return 0
End Function
End Class
3. A windows application client.exe:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim chan As TcpChannel = New TcpChannel
ChannelServices.RegisterChannel(chan)
Dim oRemoteClass As RemoteComponent.RemoteClass = CType(Activator.GetObject( _
GetType(RemoteComponent.RemoteClass), _
"tcp://localhost:8080/RemoteComponent.RemoteClass"), RemoteComponent.RemoteClass)
Dim cSQL As String
Dim dsData As DataSet
Dim nI As Integer
cSQL = " select *"
cSQL = cSQL & " from customers"
oRemoteClass.BeginTransaction()
dsData = oRemoteClass.RunSQL(cSQL, "Customers")
oRemoteClass.CommitTransaction()
ChannelServices.UnregisterChannel(chan)
oRemoteClass = Nothing
End Sub
The application client calls the method BeginTransaction that inicialize the private variable oTransaction_m, but when the application client call the method RunSQL the variable oTransaction_m is nothing...
Why the variable oTransaction_m is nothing???
I need test transactions with .NET Remoting!!!
Thanks for your help!
I have three components:
1. A class library 'RemoteComponent.dll':
Imports System
Imports System.Data.SqlClient
Imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
<Serializable()> _
Public Class RemoteClass
Inherits MarshalByRefObject
Private oConnection_m As SqlConnection
Private oTransaction_m As SqlTransaction
Private Function ConnectionString() As String
Return "user id=sa;password=;Database=Northwind;Server=localhost;Connect Timeout=30"
End Function
Public Sub BeginTransaction()
Dim oCnn As SqlConnection
Dim oTrx As SqlTransaction
oCnn = New SqlConnection(ConnectionString)
If oCnn.State = ConnectionState.Closed Then
oCnn.Open()
End If
oTrx = oCnn.BeginTransaction
oConnection_m = oCnn
oTransaction_m = oTrx
End Sub
Public Sub CommitTransaction()
oTransaction_m.Commit()
oConnection_m.Close()
oTransaction_m.Dispose()
oTransaction_m = Nothing
oConnection_m.Dispose()
oConnection_m = Nothing
End Sub
Public Overloads Function RunSQL( _
ByVal cSQL As String, _
ByVal cTableName As String) As DataSet
Dim oCmd As SqlCommand = New SqlCommand
Dim oCnn As SqlConnection = Nothing
Dim oAdp As SqlDataAdapter = New SqlDataAdapter
Dim oDs As DataSet = New DataSet
Try
oCnn = oTransaction_m.Connection
With oCmd
.Connection = oCnn
.CommandText = cSQL
.CommandType = CommandType.Text
.Transaction = oTransaction_m
End With
With oAdp
.SelectCommand = oCmd
.Fill(oDs, cTableName)
End With
If IsNothing(oTransaction_m) Then
If Not oCnn Is Nothing And oCnn.State <> ConnectionState.Closed Then
oCnn.Close()
End If
End If
Return oDs
Catch ex As Exception
Throw New System.Exception(ex.Message)
End Try
End Function
End Class
2. A console application 'Listener.exe':
Imports System
imports System.Runtime.Remoting
Imports System.Runtime.Remoting.Channels
Imports System.Runtime.Remoting.Channels.Tcp
Imports System.Runtime.Remoting.Channels.Http
Public Class Listener
Public Shared Function Main(ByVal args() As String) As Integer
Dim channel As TcpChannel = New TcpChannel(8080)
ChannelServices.RegisterChannel(channel)
RemotingConfiguration.RegisterWellKnownServiceType( _
GetType(RemoteComponent.RemoteClass), _
"RemoteComponent.RemoteClass", _
WellKnownObjectMode.SingleCall)
System.Console.WriteLine("Press the enter key to exit...")
System.Console.ReadLine()
Return 0
End Function
End Class
3. A windows application client.exe:
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim chan As TcpChannel = New TcpChannel
ChannelServices.RegisterChannel(chan)
Dim oRemoteClass As RemoteComponent.RemoteClass = CType(Activator.GetObject( _
GetType(RemoteComponent.RemoteClass), _
"tcp://localhost:8080/RemoteComponent.RemoteClass"), RemoteComponent.RemoteClass)
Dim cSQL As String
Dim dsData As DataSet
Dim nI As Integer
cSQL = " select *"
cSQL = cSQL & " from customers"
oRemoteClass.BeginTransaction()
dsData = oRemoteClass.RunSQL(cSQL, "Customers")
oRemoteClass.CommitTransaction()
ChannelServices.UnregisterChannel(chan)
oRemoteClass = Nothing
End Sub
The application client calls the method BeginTransaction that inicialize the private variable oTransaction_m, but when the application client call the method RunSQL the variable oTransaction_m is nothing...
Why the variable oTransaction_m is nothing???
I need test transactions with .NET Remoting!!!
Thanks for your help!