Passing custom exceptions server to client

K

Ken Allen

I have a .net client/server application using remoting, and I cannot get the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an assembly
common to the client and server components. The custom class merely defines
three (3) constructors -- the null constructor; one with a string parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I should
"turn on customErrors in the server's config file", but I do not have a
config file. Right now I do not want to deal with the config files (I have
too many other things to figure out).

The MSDN references the ability to perform configuration programmatically,
but there is no reference to how this customError value can be set that I
can find.

I added a debug statement to my code, and the values returned are False for
local calls and true for remote calls, but I can still find no reference on
how to programmatically enable the passing of the custom exception across
the remote procedure call.

Help!

-Ken
 
N

Nicholas Paldino [.NET/C# MVP]

Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.
 
K

Ken Allen

I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

Nicholas Paldino said:
Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have a .net client/server application using remoting, and I cannot get the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an assembly
common to the client and server components. The custom class merely defines
three (3) constructors -- the null constructor; one with a string parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I should
"turn on customErrors in the server's config file", but I do not have a
config file. Right now I do not want to deal with the config files (I have
too many other things to figure out).

The MSDN references the ability to perform configuration programmatically,
but there is no reference to how this customError value can be set that I
can find.

I added a debug statement to my code, and the values returned are False for
local calls and true for remote calls, but I can still find no reference on
how to programmatically enable the passing of the custom exception across
the remote procedure call.

Help!

-Ken
 
N

Nicholas Paldino [.NET/C# MVP]

Ken,

No, that should be it. You don't have any fields of a type not marked
as serializable in your exception, do you?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

message news:%[email protected]...
Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have a .net client/server application using remoting, and I cannot
get
the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an assembly
common to the client and server components. The custom class merely defines
three (3) constructors -- the null constructor; one with a string parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I should
"turn on customErrors in the server's config file", but I do not have a
config file. Right now I do not want to deal with the config files (I have
too many other things to figure out).

The MSDN references the ability to perform configuration programmatically,
but there is no reference to how this customError value can be set
that
I False
for reference
on
 
K

Ken Allen

Below is the definition I am using. As you can see, this is pretty
straight-forward.This definition exists in a common assembly; the server
(remote) code throws an exception of this type and that works (is displayed
in a console window and entered into the Application Event Log), but even
though the client has a Catch for this exception it is not caught!

In the client I Catch a System.Runtime.Remoting.RemotingException that
merely indicates that the "Server encountered an internal error." The
exception foes on to indicate "For more information, turn on customErrors in
the server's .config file." I do not have a config file, and I do not wish
to attempt to begin using one at this stage of the project (deadlines, you
know).

I checked out the customErrors setting in MSDN and it indicates that it
controls whether exception information is marshaled at all. There is even a
reference to a way to check this by invoking
"RemotingConfiguration.CustomErrorsEnabled" -- when I call this with the
parameter True (local), then False is returned; passing False (remote) as
the parameter results in a True return. The explanation is that True
indicates only filtered information is returned and False indicates that
complete information is returned. While I would like False to be returned in
both cases, this appears to be the desired response for remote connections.

Still, I get the generic reoting exception at the client executable rather
than the custom exception. This happens whether I copy the server component
to the server computer and execute it from the command line, or whether I
execute it from inside the IDE in debug mode (I declare the local directory
that contains the debug images as a share and specify the share as the
location of the debug target for the debugger).

Oddly enough, when debugging this on another developer system, things were a
bit different. I was unable to configure the remote debugging facilities
(another issue), so I copied all of the code (release build) local to that
test system and ran both the server and client executables from the same
system. In that case it seemed that the detailed errors from the server
component were being reported back to the client component and being
displayed on the screen!

As I have re-read the MSDN entry on the CustomErrorsEnabled method
repeatedly, it seems I missed on connotation. The return values of
True/False from this method are based on the specific setting of the 'mode'
attibute of the channel -- if this is set to 'remoteOnly' then the results I
am seeing indicate local callers get complete information and remote callers
receive filtered information.

The MSDN entry contains this entry: "The CustomErrorsEnabled method reflects
configuration settings that were either specified in the mode attribute of
the <customErrors> element of the application configuration file or set
programmatically when the server channel was configured. For more
information about configuration options, see Configuration." MSDN has some
references to being able to set configuration programmatically, but I cannot
figure out how to do this from the scant information available. There is no
reference specific to the setting of this 'mode' value for the channel that
I am opening for the connection. There is no exposed property of that name,
and it is not clear how to set this. The only reference is that the config
file should contain something like the following, but there is no reference
that I can find on how to control this setting programmatically -- the only
mechanism seems to be to read a config file!

<configuration>
<system.runtime.remoting>
<customErrors mode="off"/>
</system.runtime.remoting>
</configuration>

//----------------------------------------------------------------
[Serializable()]
public class MyCustomException : ApplicationException
{
public MyCustomException() { }

public MyCustomException(string message) : base(message) { }

public MyCustomException(string message, Exception inner) : base(message,
inner) { }
}
//----------------------------------------------------------------



Nicholas Paldino said:
Ken,

No, that should be it. You don't have any fields of a type not marked
as serializable in your exception, do you?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

message news:%[email protected]...
Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a .net client/server application using remoting, and I cannot get
the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an
assembly
common to the client and server components. The custom class merely
defines
three (3) constructors -- the null constructor; one with a string
parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I
should
"turn on customErrors in the server's config file", but I do not
have
a (I
have that
 
K

Ken Allen

I cresated a file named MyRemoting.config and placed the following text in
that file.

<configuration>
<system.runtime.remoting>
<customErrors mode="off"/>
</system.runtime.remoting>
</configuration>

I then modified modified my code to call the
[RemotingConfiguration.Configure] method before registering my object. Now
when I run the server and client components, the client receives a
System.Runtime.Serialization.SerializationException -- "The constructor to
deserialize an object of type MyCustomException was not found."

So now it seems that it is transmitting the custom exception, but I am not
receiving it. I shall keep digging.

Is there no way to achieve this without a config file?

-Ken

Nicholas Paldino said:
Ken,

No, that should be it. You don't have any fields of a type not marked
as serializable in your exception, do you?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

message news:%[email protected]...
Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a .net client/server application using remoting, and I cannot get
the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an
assembly
common to the client and server components. The custom class merely
defines
three (3) constructors -- the null constructor; one with a string
parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I
should
"turn on customErrors in the server's config file", but I do not
have
a (I
have that
 
K

Ken Allen

OK, so now I have now also changed the exception defintion to

public class MyCustomException : ApplicationException, ISerializable

and also added a public constructor

public MyCustomException(SerializationInfo info, StreamingContext
context) : base(info, context) { }

and I am now receiving my exception at the client!

The problem now (in addition to having this config file I do not want) is
that I cannot catch this exception explicity! In the following code, the
second instance always catches the exception, even though the display
indicates that it is a MyCustomException!

try
{
server.remote_method_call();
}
catch(MyCustomException exc1)
{
MessageBox.Show("MyCustom: " + exc1.ToString());
}
catch(Exception exc2)
{
MessageBox.Show("General Exception: " + exc2.ToString());
}

Argh!

-Ken

Nicholas Paldino said:
Ken,

No, that should be it. You don't have any fields of a type not marked
as serializable in your exception, do you?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

message news:%[email protected]...
Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a .net client/server application using remoting, and I cannot get
the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an
assembly
common to the client and server components. The custom class merely
defines
three (3) constructors -- the null constructor; one with a string
parameter;
and one with a string and innner exception parameter -- that simply invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I
should
"turn on customErrors in the server's config file", but I do not
have
a (I
have that
 
K

Ken Allen

Actually, it turns out that the 'type' of the exception that is deserialized
at the client is ApplicationException -- the base class of
MyCustomException! How do I convince the constructor to make this a
MyCustomException instance?

-Ken

Ken Allen said:
OK, so now I have now also changed the exception defintion to

public class MyCustomException : ApplicationException, ISerializable

and also added a public constructor

public MyCustomException(SerializationInfo info, StreamingContext
context) : base(info, context) { }

and I am now receiving my exception at the client!

The problem now (in addition to having this config file I do not want) is
that I cannot catch this exception explicity! In the following code, the
second instance always catches the exception, even though the display
indicates that it is a MyCustomException!

try
{
server.remote_method_call();
}
catch(MyCustomException exc1)
{
MessageBox.Show("MyCustom: " + exc1.ToString());
}
catch(Exception exc2)
{
MessageBox.Show("General Exception: " + exc2.ToString());
}

Argh!

-Ken

message news:[email protected]...
Ken,

No, that should be it. You don't have any fields of a type not marked
as serializable in your exception, do you?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ken Allen said:
I have placed the following decoration at the top of my custom exception
class. Do I need to do more?

-Ken
[Serializable()]

"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message Ken,

Have you made your exception serializable? If it is not serializable,
then the framework will not know how to create a representation of the
exception to throw on the client side.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

I have a .net client/server application using remoting, and I
cannot
get
the
custom exception class to pass from the server to the client. The custom
exception is derived from ApplicationException and is defined in an
assembly
common to the client and server components. The custom class merely
defines
three (3) constructors -- the null constructor; one with a string
parameter;
and one with a string and innner exception parameter -- that simply
invoke
the base class construcvtor passing any parameters along.

I throw the custom exception in the server, but when the exception is
reported on the client it is a generic
System.Runtime.Remoting.RemotingException! The text indicates that I
should
"turn on customErrors in the server's config file", but I do not
have
a
config file. Right now I do not want to deal with the config files (I
have
too many other things to figure out).

The MSDN references the ability to perform configuration
programmatically,
but there is no reference to how this customError value can be set that
I
can find.

I added a debug statement to my code, and the values returned are False
for
local calls and true for remote calls, but I can still find no reference
on
how to programmatically enable the passing of the custom exception
across
the remote procedure call.

Help!

-Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top