Re: Remoting Singleton Issue



Hi Palvinder,

Application.Run is a blocking call. You'll notice that if you add a
breakpoint to the line in which you perform remoting configuration on the
client that it doesn't get executed until after you exit the application, by
closing the ClientObject Form, for instance.

Try the following instead:

[STAThread]
static void Main()
{
RemotingConfiguration.Configure(@"ClientApplication.exe.config");
Application.Run(new ClientObject());
}

RemoteObject proxy = new RemoteObject();

In your example this code is just creating an instance of the object on the
client, which is why you keep getting 0 and don't need an instance of the
server running. Adding the configuration as above should fix the problem by
telling the remoting framework to connect to the configured URI for the
server-activated object before code with the "new" keyword is used to try to
create an instance.

Everything else seems fine :)

BTW, you might want to post Remoting questions to the
microsoft.public.dotnet.framework.remoting newsgroup in the future.

--
Dave Sexton

"Palvinder Singh" <palvindersingh@xxxxxxxxx> wrote in message
news:1165055169.095631.57280@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Thanks for your reply,

i have posted my code as i am positive i am missing something along the
way. How do i know if my config files are correct and that the
application is getting there values?

a)i use a property and a method to retrieve the values, also commented
out 'InitializeLifetimeService' as it had no effect;

using System;
/// <summary>
/// Remotable class
/// </summary>
public class RemoteObject : MarshalByRefObject
{
private int savedNumber;
public int MyNumber
{
get{ return savedNumber; }
set{ savedNumber = value; }
}

public RemoteObject() {}

//public override Object InitializeLifetimeService()
//{
// Allow this object to live "forever"
//return null;
//}

public int GetNumber()
{
return savedNumber;
}
}


b) i use a config file to set the channels etc, is this okay?

using System;
using System.Runtime.Remoting;
/// <summary>
/// Server, component host
/// </summary>
class HostObject
{
[STAThread]
static void Main(string[] args)
{
//register remotable classes
RemotingConfiguration.Configure(@"ComponentHost.exe.config");

Console.WriteLine("press any key to quit...");
Console.ReadLine();
}
}

and its config file;

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<!--Define remotable object-->
<service>
<wellknown
mode = "Singleton"
type = "RemotableObjects.RemoteObject,
RemotableObjects"
objectUri = "RemoteObject">
</wellknown>
</service>

<!--Define the protocol used for network acess (tcp or
http)-->
<channels>
<channel ref = "tcp" port = "9090" />
</channels>
</application>
</system.runtime.remoting>
</configuration>


c) Where u see '.....' i have remove commonly seen code in forms. Also
where i define 'proxy', i have added a reference to my remotableobject
dll , is this correct?

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.Remoting;


namespace Client
{
public class ClientObject:System.Windows.Forms.Form
{
RemoteObject proxy = new RemoteObject();

private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button2;


private System.ComponentModel.Container components = null;

public ClientObject()
{.....}

protected override void Dispose( bool disposing )
{.....}

//#region Windows Form Designer generated code.....

[STAThread]
static void Main()
{
Application.Run(new ClientObject());

RemotingConfiguration.Configure(@"ClientApplication.exe.config");

}

private void button1_Click(object sender, System.EventArgs e)
{
int clientNumber = proxy.GetNumber();
textBox1.Text = clientNumber.ToString();
}

private void button2_Click(object sender, System.EventArgs e)
{
proxy.MyNumber = int.Parse(textBox1.Text);
textBox1.Text = "set complete";
}
}
}

and finally the config for the client

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.runtime.remoting>
<application>
<!--Define the object this application will use
remotely.-->
<client>
<wellknown
type = "RemotableObjects.RemoteObject,
RemotableObjects"
url = "tcp://sony:9090/RemoteObject">
</wellknown>
</client>

<!--Define the protocol used for network acess (tcp or
http)-->
<channels>
<channel ref = "tcp" port = "0" />
</channels>
</application>
</system.runtime.remoting>
</configuration>


Regards

Palvinder



.