Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheriting Existing .Net Class Serialization Error

I am inheriting and modifying the System.Net.MailMessage class for use in a web service. I need to keep it named MailMessage for other reasons. When I use this in the code below, I get the error below.

"Types 'System.Net.Mail.MailMessage' and 'TestWebService.MailMessage' both use the XML type name, 'MailMessage', from namespace 'http://tempuri.org/'. Use XML attributes to specify a unique XML name and/or namespace for the type."

I belive I have to add XMLRoot and Type attributes, but I can't seem to figure out the right combination. What do I need to do to resolve this error?

namespace TestWebService
{

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string Test(MailMessage emailMessage) 
        {
            return "It Worked!";
        }
    }
}

namespace TestWebService 
{
    public class MailMessage : System.Net.Mail.MailMessage
    {

        public MailMessage() : base()
        {

        }
    }
}
like image 876
SchwartzE Avatar asked Feb 21 '23 18:02

SchwartzE


1 Answers

You have to add XmlTypeAttribute to change the name or namespace to make it unique for serialization

using System.Xml.Serialization

[XmlType(Namespace = "http://tempuri.org/", TypeName = "SomethingOtherThanMailMessage")]
public class MailMessage : System.Net.Mail.MailMessage
{
}

However, System.Net.Mail.MailMessage itself is not serializable so your class that is derived from it won't be serializable.

like image 144
amit_g Avatar answered Mar 04 '23 11:03

amit_g