I'm currently converting our .net business objects library to a PCL file so that it can be used with Xamarin IOS/Android and while it contains mainly POCO objects, it also contains custom exceptions but this is throwing errors.
Take a typical Custom Exception:
[Serializable]
public class EncryptKeyNotFoundException : Exception
{
public EncryptKeyNotFoundException()
: base() { }
public EncryptKeyNotFoundException(string message)
: base(message) { }
public EncryptKeyNotFoundException(string format, params object[] args)
: base(string.Format(format, args)) { }
public EncryptKeyNotFoundException(string message, Exception innerException)
: base(message, innerException) { }
public EncryptKeyNotFoundException(string format, Exception innerException, params object[] args)
: base(string.Format(format, args), innerException) { }
protected EncryptKeyNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
}
As expected the PCL doesn't like [Serializable]
and SerializationInfo
. While I might get away with sticking [DataContract] instead of using [Serialiable]
, it still won't resolve the issue with SerializationInfo
.
Is there anyway to circumvent this problem?
Thanks.
Update:
I've had a look at Implementing custom exceptions in a Portable Class Library as suggested but the following 2 attributes are not recognised:
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
I must be missing a reference but to which assembly?
I'm currently looking at an alternative solution as provided in Portable class library: recommended replacement for [Serializable]
Hopefully this will work. I will update my answer once I have more info to provide.
Update:
ClassInterfaceAttribute
is part of the System.RunTime.InteroServices but I cannot add this to my PCL project, well at least it's not visible. Am I missing something?
The other article is providing additional info and it looks that when using conditional compilation, this should work, but again, while the sample code from the json library appears to work, I must be missing something as I cannot add a reference so that [Serializable]
does not throw an error, but I don't appear to be able to do so.
One thing I've tried is to simply comment out:
protected EncryptKeyNotFoundException(SerializationInfo info,
StreamingContext context) : base(info, context) { }
And I can compile my pcl project ok, so the question is do I need this?
Thanks.
Kapil (2019) recommends using custom exceptions when one creates an interface or a library, as this helps diagnose problems that have occurred in the code.
The purpose of a custom exception class is to integrate the look-up of localized message strings in a custom message catalog into the mechanism that is used for error reporting in the client infrastructure.
Use a custom exception when you want users to be able to programmatically distinguish between certain error conditions. If that situation does not exist, you can throw a more "general" exception, and avoid creating the custom exception class.
I think you have misinterpreted the answer in suggested link. You don't need to add ClassInterfaceAttribute
or ComVisibleAttribute
in your custom exception implementation. If we look in Exception class for .NET Framework we see :
[SerializableAttribute]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception : ISerializable, _Exception
and at Exception class for Silverlight, this
[ClassInterfaceAttribute(ClassInterfaceType.None)]
[ComVisibleAttribute(true)]
public class Exception
SerializableAttribute
is not available.
Another diference is that Exception class for Silverlight has only 3 constructors.
Constructor Exception(SerializationInfo, StreamingContext)
is not available. And also we can see in below screenshot of custom exception implementation in PCL library that only 3 constructors available for Exception.
There is no such constructor available which you are trying to create :
EncryptKeyNotFoundException(SerializationInfo info, StreamingContext context)
: base(info, context) { }
So, in PCL custom exception implementation with DataContract instead of Serializable, would be something like this :
[DataContract]
public class EncryptKeyNotFoundException : System.Exception
{
public EncryptKeyNotFoundException() : base() { }
public EncryptKeyNotFoundException(string message) : base(message) { }
public EncryptKeyNotFoundException(string message, Exception innerException) : base(message, innerException) { }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With