OVH Cloud OVH Cloud

Remoting & Exceptions

1 réponse
Avatar
Olivier Gaudefroy
Salut je suis en train de m'arracher les cheveux (enfin ce qu'il en reste
^^) sur un probleme qui j'imagine est tres simple en Remoting.
Je m'explique je suis incapable de récupérer une exception levée cotée
serveur sur un objet accédé a distance.

Voila un peu de code pour faire plus clair.

Coté serveur :
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Data;

namespace Server
{
class ChannelServer : MarshalByRefObject, IPlanetChannelServer
{
public void LaunchException()
{
throw new Exception("Error");
}
}

class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpChannel(999));
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
RemotingConfiguration.RegisterWellKnownServiceType(typeof(ChannelServer),
"Server", WellKnownObjectMode.Singleton);
Console.WriteLine("Ready");
Console.ReadLine();
}
}
}


Coté libraire :

namespace Server
{
public interface IChannelServer
{
void LaunchException();
}
}



Et enfin le Client :

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Server;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
MarshalByRefObject Obj =
(MarshalByRefObject)RemotingServices.Connect(typeof(Server.IChannelServer),
"tcp://localhost:999/Server");
IChannelServer channel_server = Obj as IChannelServer;
channel_server.LaunchException();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
}
}



Et donc a l'execution je me recoit une unhandled exception du coté du
serveur.

Ma question est donc comment faire en Remoting pour transmettre une
exception au client.

Merci d'avance

1 réponse

Avatar
Paul Bacelar
C'est un tantinet complexe mais l'article ci-dessous vous explique tout de
manière très claire, un régal ;-)

http://msdn.microsoft.com/msdnmag/issues/04/03/ExceptionsinCOM/default.aspx

P.S.: faire le rapprochement entre votre exception serveur et l'attribut
[Serializable] sans avoir de vue d'ensemble, c'est impossible.
--
Paul Bacelar


"Olivier Gaudefroy" wrote in message
news:43569497$0$7948$

Salut je suis en train de m'arracher les cheveux (enfin ce qu'il en reste
^^) sur un probleme qui j'imagine est tres simple en Remoting.
Je m'explique je suis incapable de récupérer une exception levée cotée
serveur sur un objet accédé a distance.

Voila un peu de code pour faire plus clair.

Coté serveur :
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting.Channels;
using System.Data;

namespace Server
{
class ChannelServer : MarshalByRefObject, IPlanetChannelServer
{
public void LaunchException()
{
throw new Exception("Error");
}
}

class Program
{
static void Main(string[] args)
{
ChannelServices.RegisterChannel(new TcpChannel(999));
RemotingConfiguration.CustomErrorsMode CustomErrorsModes.Off;



RemotingConfiguration.RegisterWellKnownServiceType(typeof(ChannelServer),
"Server", WellKnownObjectMode.Singleton);
Console.WriteLine("Ready");
Console.ReadLine();
}
}
}


Coté libraire :

namespace Server
{
public interface IChannelServer
{
void LaunchException();
}
}



Et enfin le Client :

using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using Server;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
TcpChannel channel = new TcpChannel();
ChannelServices.RegisterChannel(channel);
MarshalByRefObject Obj >


(MarshalByRefObject)RemotingServices.Connect(typeof(Server.IChannelServer),
"tcp://localhost:999/Server");
IChannelServer channel_server = Obj as IChannelServer;
channel_server.LaunchException();
}
catch(Exception e)
{
Console.Error.WriteLine(e.Message);
}
}
}
}



Et donc a l'execution je me recoit une unhandled exception du coté du
serveur.

Ma question est donc comment faire en Remoting pour transmettre une
exception au client.

Merci d'avance