Hallo,
ich habe ein Problem beim Remoting. Ich habe für Server und Client ein Interface
**Interface 😗*
namespace Interface
{
public class Interfaces
{
public interface IClient
{
bool _Ping();
}
public interface IServer
{
Guid _login(string Hostname, string Usernamem, Interfaces.IClient IClient);
}
}
}
Und habe ein Server und ClientObject
ClientObject
namespace CHAT
{
public class ClientObject : MarshalByRefObject, Interfaces.IClient
{
public bool _Ping()
{
return true;
}
}
}
Jetzt möchte ich auf dem Server verbinden und eine Referenz auf mein ClientObject mitgeben. Diese übergebe ich mit der _login function. Dort bekomm ich auch den Fehler.
class ClientConnection
{
private IChannel iChannel;
private Interfaces.IServer Server;
private ClientObject thisClient;
public void OpenConnection()
{
iChannel = new TcpChannel(0);
ChannelServices.RegisterChannel(iChannel, true);
thisClient = new ClientObject();
Server = (Interfaces.IServer)Activator.GetObject(typeof(Interfaces.IServer), "tcp://" + IP + ":" + PORT + "/chat.rem");
Server._login(SystemInformation.ComputerName, SystemInformation.UserName, thisClient); //Fehler wird hier geworfen
}
public void CloseConnection()
{
ChannelServices.UnregisterChannel(iChannel);
}
}
Fehlermeldung :
Der Argumentetyp System.MarshalByRefObject konnte nicht in den Parametertyp Interface.Interfaces+IClient konvertiert werden.
private static TcpChannel tcpChannel;
public static TcpChannel _TcpChannel
{
get { return (tcpChannel); }
}
public static void OpenServer(int port)
{
IDictionary ConnectionProperties = new Hashtable();
ConnectionProperties["port"] = Properties.Settings.Default.ConnectionPort;
BinaryServerFormatterSinkProvider serverSinkProvider = new BinaryServerFormatterSinkProvider();
serverSinkProvider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
tcpChannel = new TcpChannel(ConnectionProperties, null, serverSinkProvider);
ChannelServices.RegisterChannel(tcpChannel, true);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ServerObject), "chat.rem", WellKnownObjectMode.Singleton);
}
public static void CloseConnection()
{
ChannelServices.UnregisterChannel(_TcpChannel);
}
class ServerObject : MarshalByRefObject, Interfaces.IServer
{
public static Dictionary<Guid, ClientInfomation> Clients = new Dictionary<Guid, ClientInfomation>();
public static ServerObject _server;
private ServerObject()
{
ServerObject._server = this;
}
public Guid _login(string Hostname, string Username, Interfaces.IClient IClient)
{
Guid guid = new Guid();
ClientInfomation clientInfomation = new ClientInfomation();
clientInfomation.HostName = Hostname;
clientInfomation.UserName = Username;
clientInfomation.IClient = IClient;
Clients.Add(guid, clientInfomation);
return guid;
}
}
Problem gelöst 👍
Der Assemblyname muss beim Server der selbe wie bei dem Client sein. Da sonst der Server das ClientObject nicht finden kann.
Vielleicht dieses: "Zeiger auf Funktionen an Server geben.", ganz schlechte Idee das...