Hi everybody,
I'm trying to create a very simple remoting application and I'm trying to do
it using "Marshal By Value" (using MarshalByRef it works fine).
the problem is: I get this exception:
Trying to create a proxy to an unbound type.
at System.Runtime.Remoting.RemotingServices.Unmarshal(Type classToProxy,
String url, Object data)
at System.Activator.GetObject(Type type, String url, Object state)
at System.Activator.GetObject(Type type, String url)
at RemotableClientNamespace.RemotableClient.Main(String[] args) in
c:\........
Does anyone can help?
thank you in advance
Alexj73
Here is the code:
Host code:
using RemotableTypeNamespace;
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels.Http;
namespace remoteHOST
{
class remoteHostClass
{
[STAThread]
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpChannel(7567));
WellKnownServiceTypeEntry info_service_to_register = new
WellKnownServiceTypeEntry(typeof(RemoteClass),"RemoteClass",
WellKnownObjectMode.SingleCall);
string result = RemotingConfiguration.ApplicationName = "Remote Server";
RemotingConfiguration.RegisterWellKnownServiceType(info_service_to_register);
Console.WriteLine("{0}: listening for requests.\n\nPress Enter to
exit...", result);
Console.ReadLine();
}
}
}
Servant code:
using System;
using System.Threading;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace RemotableTypeNamespace
{
[Serializable]
public class RemoteClass : ISerializable
{
int x;
public RemoteClass() { this.x=1; }
public int GetX() { Console.WriteLine("Will return ... {0}", this.x);
return this.x; }
void ISerializable.GetObjectData(SerializationInfo info, StreamingContext
context) { info.AddValue("x", x); }
}
}
client code
namespace RemotableClientNamespace
{
class RemotableClient
{
[STAThread]
static void Main(string[] args)
{
TcpChannel c = new TcpChannel();
ChannelServices.RegisterChannel(c);
RemoteClass remoteObject;
try
{
remoteObject = (RemoteClass)Activator.GetObject(typeof(RemoteClass),
"http://localhost:7567/RemoteClass");
remoteObject.GetX();
}
catch(Exception e)
{
ChannelServices.UnregisterChannel(c);
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}
}
}
Chris Yager - 25 Nov 2005 15:22 GMT
In order to create an instance of an object on a remote machine, the object
that you create must derive from MarshalByRefObject. You can use that
object to return another object by value but the initial object has to be a
MarshalByRef object.
It sounds like you need to employ some form of a factory design pattern. So
your server should expose a method called "GetSomething" (or whatever) that
returns the [Serializable] class. The class is created on the remote
machine and then handed to the client machine once its created. The code
might look like:
[Serializable]
public class User
{
private string _strPassword;
private string _strUserID;
private string _strUserName;
public string Password
{
get { return _strPassword; }
set { _strPassword = value; }
}
public string UserID
{
get { return _strUserID; }
set { _strUserID = value; }
}
public string UserName
{
get { return _strUserName; }
set { _strUserName = value; }
}
}
public class Server:MarshalByRefObject
{
public User GetUser(string UserName)
{
User objUser=new User();
objUser.UserName=UserName;
return objUser;
}
}
When implemented, the Server can return a user object created on the remote
machine. You can pass any constructors that might be needed in the method
call on the remoted object.
-chris
> Hi everybody,
>
[quoted text clipped - 100 lines]
> }
> }
Venerable - 28 Nov 2005 16:16 GMT
Hoping this might help.....
Marshal-by-Value Objects:::
Marshal-by-value objects should implement the ISerializable interface or
should be marked with the SerializableAttribute attribute so that the
remoting system can serialize these objects automatically. When the client
calls a method on marshal-by-value objects, the remoting system creates a
copy of these objects and passes the copy to the client application domain.
After the client application receives the copy, the copy in the client
application domain handles any method call. In addition, when
marshal-by-value objects are passed as arguments, a copy of the object is
passed to the method.
To improve performance and reduce processing time, move the complete state
of the object and its functionality to the target application domain. Using
marshal-by-value objects reduces time- and resource-consuming trips across
network, process, and application domain boundaries. You also use
marshal-by-value objects directly from within the original application domain
of the object. In this case, access is efficient because marshaling does not
take place.
Marshal-by-Reference Objects:::
Marshal-by-reference objects are remotable objects that extend the
System.?Marshal-ByRefObject class. When a client creates an instance of a
marshal-by-reference object in its own application domain, the .NET Remoting
infrastructure creates a proxy object in the caller application domain that
represents the marshal-by-?reference object and returns a reference of that
proxy to the caller. The client then makes method calls to the proxy object.
The remoting system marshals those calls, returns them to the server
application domain, and invokes the call on the actual object.
----------
Tapasvi - MCSD for Microsoft.NET
Sr. Architect .NET - Unisys LLC & RouteServices.com
> In order to create an instance of an object on a remote machine, the object
> that you create must derive from MarshalByRefObject. You can use that
[quoted text clipped - 152 lines]
> > }
> > }