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()
{
}
}
}
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.
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