4

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

        }
    }
}
2
  • 2
    Try just attributing your subclass with [XmlType(Namespace="test")]. Commented Jan 20, 2012 at 19:06
  • BTW, are you aware that ASMX web services are a legacy technology and should not be used for new development? Commented Jan 20, 2012 at 19:47

1 Answer 1

8

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Does this mean this is not possible?
You will have to serialize it yourself. keyvan.ms/how-to-serialize-a-mailmessage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.