Forum Moderators: open
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?
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.
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?