I am building a MAF Pipeline which an add-in can also use for callbacks to the host system to use some service from the host. Those methods can throw exceptions which should be handled by the add-in. Handle it that case should not only mean to just catch them but to also analyze them.
As always there are two options to get objects across the appdomain border: by serializing them or by extending MarshalbyRefObject
.
Actually I am having problems with both options:
When I am using serialization, then my add-ins needs to know the exact types of the exceptions as it else can't deserialize the exception. That means that I can't work here on an abstraction layer. The Exception class itself is marked as serializable, so also all subclasses need to be marked as serialable so that this works. For me this is not really a solutions as I can't isolate the types between host and add-in (as I can't work on abstractions)
Using MarshalbyRefObject won't work as well as all Exception need to extend "Exception" and therefore can't extend MarshalbyRefObject.
Is there any standard pattern which could solve this problem?
The way I have solved this in the past is to use the Data
object in an exception to pass private data between the layers.
try
{
}
catch (SomeSpecificException spex)
{
var exception = new Exception();
exception.Data.Add("Something", "Specific");
throw exception;
}
Basically, in my adapter layers I have code that converts any specific exceptions into general ones. Then in the adapter layer on the other side, I can inspect the data object and convert that to an exception that is usable by its callers.