Forum Moderators: open

Message Too Old, No Replies

Using .NET Remoting with ASP.NET

         

thumper

5:06 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



I want my webapplication to access a .Net remote object. The problem is where do I store the reference to this object?

Now I use the following code in the method where I need to access the server object:

MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect(typeof(Remotetest.Interfacename), "http://localhost:61061/endpointname");
try {
remoteobject = obj as Remotetest.Interfacename;
//Call methods on remoteobject...
}

This works fine, but I want the reference to be shared across the application, so that all asp.net pages can use this object. I've tried placing it in the Page_Load method of an asp page, but then I only get something like "Couldn't find instance" when I try accessing it in other methods than Page_Load.

Does anyone know where I place this code, and how I use this object throughout the web application? Do I have to set this up in a configuration file like web.config, and if so how do I do that?

TheNige

8:19 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



an easy way would be to make a class that has a function that returns an instance of this remote object. Then you could call it all throughout your program.


function GetRemoteObject() as RemoteObject <--Not sure what data type to return here

MarshalByRefObject obj = (MarshalByRefObject)RemotingServices.Connect(typeof(Remotetest.Interfacename), "http://localhost:61061/endpointname");
remoteobject = obj as Remotetest.Interfacename;

return remoteobject;

End function

and of course, don't mine my VB.Net/C# coding mess in the example. You should get the idea.

Then in your code just make a call to the function that will return the object and you can then call the methods of the remote object. Put the function in a class some place then you just need to import the namespace to the page you are using it on.

thumper

11:10 pm on Oct 8, 2004 (gmt 0)

10+ Year Member



It works now. I made a static method in a .cs file in the same namespace as the aspx-files:

public static Kalkulator getServerObject() {
MarshalByRefObject obj = (MarshalByRefObject) RemotingServices.Connect(typeof(Interfacename), "http://localhost:61061/endpoint");
try {
remoteobject = (Interfacename)obj;
}
return remoteobject;
}

Although this works, I have to reconnect to this everytime any user needs to get data from the server, which is quite ineffecient isn't it? On the server side there is no problem because I have an application running, which manages the connection.

I still want to be able to use just one instance of the remote object throughout the entire webapplication. But maybe this isn't possible, or maybe it's not even a clever solution?

Xoc

6:26 am on Oct 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can store it in an Application variable.

Application.Lock()
Application("SaveRef") = ...
Application.Unlock()

Or, if you can recreate it on a whim, you can store it in the cache.

thumper

8:41 am on Oct 9, 2004 (gmt 0)

10+ Year Member



Sweet! Exactly what I wanted.

Thanks!