problem with .net remoting - SQL Server

jdccr

Member
Joined
Jul 28, 2004
Messages
14
Hi, I am using .NET Remoting for database connection.

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

Public Class RemoteClass
Inherits MarshalByRefObject

Public Function ConnectionString() As String
Return "user id=sa;password=;Database=Northwind;Server=localhost;Connect Timeout=30"
End Function

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 = New SqlConnection(ConnectionString)

With oCmd
.Connection = oCnn
.CommandText = cSQL
.CommandType = CommandType.Text
End With

With oAdp
.SelectCommand = oCmd
.Fill(oDs, cTableName)
End With

If Not oCnn Is Nothing And oCnn.State <> ConnectionState.Closed Then
oCnn.Close()
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://omega:8080/RemoteComponent.RemoteClass"), RemoteComponent.RemoteClass)

If oRemoteClass Is Nothing Then
System.Console.WriteLine("Error: unable to locate server")
Else
Dim cSQL As String
Dim dsData As DataSet

cSQL = " select *"
cSQL = cSQL & " from customers"

dsData = oRemoteClass.RunSQL(cSQL, "Customers")

With Me.ComboBox
.ValueMember = dsData.Tables("Customers").Columns("CustomerId").ColumnName
.DisplayMember = dsData.Tables("Customers").Columns("CompanyName").ColumnName
.DataSource = dsData.Tables("Customers").DefaultView
End With

End If
ChannelServices.UnregisterChannel(chan)
End Sub


I execute two or more instance of my client. In the database I see only one session (commant sp_who in SQL Server) for the NorthWind database.

I need one session for each instance of my client application!!!

Thanks for your help!!!
 
Its probably because the local database only sees the local class instance. It does not know requests are being initiated from a remote client.
 
you are only seeing a single instance because you are using a single call and the call is happening in a single thread.
it opens . . . it closes. only one session at a time is being created.
 
I have a doubt

Ok, you are right, but I have a doubt:

If I dont use .NET Remoting and build the instance of RemoteClass in the following form:

Dim oRemoteClass As New RemoteComponent.RemoteClass

and If I execute two or more instances of my client application I see one session for each instance of my client application in the database.

The question is: Is there any way to obtain this behavior with .NET Remoting???


Joe Mamma said:
you are only seeing a single instance because you are using a single call and the call is happening in a single thread.
it opens . . . it closes. only one session at a time is being created.
 
Back
Top