Re: Singleton Objects

From: Ken Kolda (ken.kolda_at_elliemae-nospamplease.com)
Date: 11/01/04


Date: Mon, 1 Nov 2004 14:59:21 -0800

The declaration of the <wellknown> type in your client and server config
files are wrong. The format of the type attribute is:

<wellknown type="Namespace.ClassName, AssemblyName" ... />

So, yours should look like:

<wellknown type="RemoteType.clsRemoteType, RemoteType" ... />

Ken

"pSm" <pSm@discussions.microsoft.com> wrote in message
news:7DF86AC8-663D-4AAE-8A45-68A2BF8DE4B6@microsoft.com...
> Ken,
> Here's the server 'Type' -->
> --------------------------------------------------------------------------
------------------
> Imports System.Diagnostics
> Public Class clsRemoteType
> Inherits MarshalByRefObject
>
> Private arrIDs(10) As String
> Private Shared lastid As String
>
> Private Shared message As String
> Private count As Integer = 0
> Public evt As New System.Diagnostics.EventLog("RemoteLog.txt")
>
> Public Sub clsRemoteType()
>
> evt.WriteEntry("APP", "Remote Type Created")
> End Sub
>
> Protected Overrides Sub finalize()
> evt.WriteEntry("APP", "Remote Type Destroyed" +
CStr(DateTime.Now))
> End Sub
>
> Public Sub send(ByVal id As String, ByVal msg As String)
> Dim i As Integer
> Dim blnFound As Boolean
> For i = 0 To count
> If (arrIDs(i) = id) Then
> 'Found - Already present no need to add
> blnFound = True
> Exit For
> End If
> Next
>
> If (Not blnFound) Then
> arrIDs(count) = id
> count = count + 1
> End If
> lastid = id
> message = msg
>
> evt.WriteEntry("APP", "Message written -" + message +
> CStr(DateTime.Now))
> End Sub
>
> Public Function receive() As String
> Return (lastid + " : " + message)
> End Function
> End Class
> --------------------------------------------------------------------------
------------------
> The Host -->
>
> Imports System
> Imports System.Runtime.Remoting
> Module Module1
>
> Sub Main()
> Try
> RemotingConfiguration.Configure("../RemotingHost.exe.config")
> Console.Write("Waiting for Connections to arrive --")
>
> Console.ReadLine()
>
> Catch ex As Exception
> Console.Write("Exception Occured..")
> End Try
> End Sub
>
> End Module
>
> --------------------------------------------------------------------------
-----------------
>
> The Server 'Config'
>
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
> <system.runtime.remoting>
> <application>
> <service>
> <wellknown
> mode="Singleton"
> type="clsRemoteType, RemoteType.clsRemoteType"
> objectUri="RemoteTypeURI.rem"
> />
> </service>
> <channels>
> <channel ref="http" port="8989"/>
> </channels>
> </application>
> </system.runtime.remoting>
> </configuration>
>
> --------------------------------------------------------------------------
--------------------
> The client -->
>
> Code when a message is sent :
>
> Private Sub btnSend_Click(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles btnSend.Click
> Dim obj As New RemoteType.clsRemoteType
> obj.send("ABC", txtMsg.Text)
> End Sub
>
> Code that polls ( a timer is used)
>
> Private Sub tmPollMsg_Tick(ByVal sender As System.Object, ByVal e As
> System.EventArgs) Handles tmPollMsg.Tick
> Dim obj As New RemoteType.clsRemoteType
> Dim str As String
> str = obj.receive()
> If Trim(str) <> ":" And lastmessage <> str Then
> lblMsg.Text = lblMsg.Text + str + vbCrLf
> lastmessage = str
> End If
> End Sub
> --------------------------------------------------------------------------
----------------
> Client Config -->
> <?xml version="1.0" encoding="utf-8" ?>
> <configuration>
> <system.runtime.remoting>
> <application>
> <client>
> <wellknown
> type="clsRemoteType, RemoteType.clsRemoteType"
> url="http://FLL020361:8989/RemoteTypeURI.rem"
> />
> </client>
> </application>
> </system.runtime.remoting>
> </configuration>
>
> ---------------------------***************--------------------------------
-----
>
> Thanks
>
>
> "Ken Kolda" wrote:
>
> > If the class is exposed as a singleton, the Message value should be
shared
> > across all clients. The fact that you're seeing the object destroyed
after
> > each call from a client implies this is SingleCall instead of Singleton
> > (although even then I wouldn't expect the finalizer to run right away
unless
> > your forcing garbage collection). You want to post some code if you
still
> > have issues.
> >
> > Ken
> >
> >
> > "pSm" <pSm@discussions.microsoft.com> wrote in message
> > news:B31F2E03-2A5C-4E32-B9EC-FC7EB29E84D4@microsoft.com...
> > > Hi Sam,
> > > Thanks for the post , but I am not really looking on how to
> > implement
> > > the chat program, but understanding/clearing my remoting concepts.
> > >
> > > To re-iterate, I would like to know if my understanding that if the
object
> > > is singleton, and the message property is shared, every user would
have
> > > access to the same message value, is correct or not ?
> > >
> > > Thanks,
> > >
> > >
> > > "Sam Santiago" wrote:
> > >
> > > > Here's a remoting Chat example that uses events to accomplish what
you
> > want.
> > > > Also, if you are seeing the finalizer run each time you might have
> > defined a
> > > > SingleCall SAO vs. a Singleton SAO. Check out this example for more
> > info:
> > > >
> > > > http://support.microsoft.com/default.aspx?scid=kb;en-us;312114
> > > >
> > > > Thanks,
> > > >
> > > > Sam
> > > >
> > > > --
> > > > _______________________________
> > > > Sam Santiago
> > > > ssantiago@n0spam-SoftiTechture.com
> > > > http://www.SoftiTechture.com
> > > > _______________________________
> > > > "pSm" <pSm@discussions.microsoft.com> wrote in message
> > > > news:1DD1BB89-DC6E-422A-A8E7-C0BB2F05A273@microsoft.com...
> > > > > Hi,
> > > > > I was trying to write a basic chat client/server. I used remoting
for
> > > > this.
> > > > > The server type uses a shared property message to keep track of
the
> > last
> > > > sent
> > > > > message. All the clients poll the server every 10ms to see if a
new
> > > > message
> > > > > is arrived.
> > > > >
> > > > > The server type is singleton. My guess was since the object is
> > singleton
> > > > and
> > > > > message is shared, the last mesage posted by anyone would be
visible
> > to
> > > > > anybody else.
> > > > >
> > > > > For example, say A says 'Hi' . When B polls for the message, he
should
> > see
> > > > > the 'Hi'. But what I see is, every client is seeing only the
message
> > that
> > > > he
> > > > > has posted and nothing else. So, if A says 'Hi' and B says 'Bye',
A
> > only
> > > > sees
> > > > > 'Hi' and B only sees 'Bye'. Can anyone help me understand what's
wrong
> > ?
> > > > >
> > > > > Second question is, after I saw that this was happening, I tried
> > finding
> > > > out
> > > > > if the object is getting destroyed. So I put in a finalizer and
some
> > logs
> > > > in
> > > > > it. I see the finalizer run after every call from the client.Why
is
> > this
> > > > so ?
> > > >
> > > >
> > > >
> >
> >
> >



Relevant Pages

  • MAPI Emails from Access
    ... I realize this code is quite long, but could someone take a look at the sub ... Private Const mcERR_DOH = vbObjectError + 10000 ... Private mstStatus As String ... Dim db As Database, rs As Recordset ...
    (microsoft.public.access.formscoding)
  • Re: set fontsize with variable
    ... Public Function MsgBoxDLL(Optional strPrompt As String, ... Private m_Prompt As String ... Public Property Let Prompt ... Private Sub cmdButton1_Click ...
    (microsoft.public.vb.general.discussion)
  • Re: Is there a way to prevent a RichTextBox from scrolling?
    ... Private _isRegex As Boolean ... Public Sub New(ByVal thispattern As String, ... Dim entry As tDict ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: FileSystemWatcher advice required please
    ... Private ArchiveImport As String ... Private FilesToProcess As ProcessFiles ... Public Sub Main ... Dim NoVersion As New Collection ...
    (microsoft.public.dotnet.framework)
  • More Readable
    ... Public Class Client ... Private FChatter As Chatter ... Public Property WaitingTriggerNo() As Integer ... Private Sub WaitingTriggerThreadSpin() ...
    (microsoft.public.dotnet.framework.remoting)