Inheritance of an existing error. Class Class Serialization

I inherit and modify the System.Net.MailMessage class for use in a web service. I need to save it by name MailMessage for other reasons. When I use this in the code below, I get the error below.

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

I believe that I need to add the XMLRoot and Type attributes, but I cannot 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() { } } } 
+4
source share
1 answer

You need 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 derived class will not be serializable.

+7
source

Source: https://habr.com/ru/post/1392105/


All Articles