Jump to content
Xtreme .Net Talk

Error: Object reference not set to an instance of an object


Recommended Posts

Posted

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!

Posted

I found the solution!!!

 

In the .NET documentation I found the following:

 

SingleCall: Every incoming message is serviced by a new object instance

Singleton: Every incoming message is serviced by the same object instance

 

So, I changed the instruction:

 

RemotingConfiguration.RegisterWellKnownServiceType( _

GetType(RemoteComponent.RemoteClass), _

"RemoteComponent.RemoteClass", _

WellKnownObjectMode.SingleCall)

 

for the instruction:

 

RemotingConfiguration.RegisterWellKnownServiceType( _

GetType(RemoteComponent.RemoteClass), _

"RemoteComponent.RemoteClass", _

WellKnownObjectMode.
Singleton
)

 

With Singleton the variable oTransaction_m doesn't death and I can to access this variable in the method RunSQL...

 

But, I have a question.... Is there any problem with Singleton? What are the advantages or disadvantages in Singleton???

 

Thanks for your help!!!

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...