locked
How to mark a class in referenced DLL with DataContract attributes RRS feed

  • Question

  • I am using the Exchange Web Services Managed API SDK. One of the class in the API is EmailMessage. I want to use this class as a return type in my operation:

    using Microsoft.Exchange.WebServices.Data;
    
    public interface IGetEmail
    {
         EmailMessage GetEmail(string uniqueId);
    }
    

        How do I decorate EmailMessage with DataContract attributes? Also, how do I export this class in WSDL?

    Tuesday, October 11, 2011 5:43 PM

Answers

  • Yes, the problem is with the constructor. If you cannot change that class, then you'll need to make a copy of it and return it instead of the original ErrorMessage
    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    • Marked as answer by vijayst Wednesday, October 12, 2011 6:28 PM
    Wednesday, October 12, 2011 6:22 PM

All replies

  • 1. You don't really need to decorate classes with the DataContract attribute - as of WCF 3.5 SP1, the DataContractSerializer handles classes without the attributes - it will simply serialize all public fields/properties.

    2. If you decorate the IGetEmail interface with the serviceContract / operationContract attributes, the type will automatically be exported to the WSDL


    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    Tuesday, October 11, 2011 5:49 PM
  • Somehow, it does not work for me.

    EmailMessage is a class in a referenced DLL. so, there is no source available within the service namespace. Does the DataContractSerializer does some reflection to get EmailMessage metadata?

    It works fine if a new class is defined within the same namespace.

    Wednesday, October 12, 2011 6:53 AM
  • When you say it doesn't work, what do you mean? are you getting any error messages while opening the host or accessing the service?

    Which framework version are you using?


    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    Wednesday, October 12, 2011 11:36 AM
  • I am using WCF 4.0. When I say it does not work, on debugging the WCF Test Client does not read the metadata. If I browse to the .svc link on the server, it shows the same kind of exception in the page.

    EmailMessage is a complex type which has more types from the referenced DLL. For eg.

    public class EmailMessage

    {

       public MimeType MimeType { get; set; }

       public List<Attachment> Attachments { get; set; }

    }

    Wednesday, October 12, 2011 4:11 PM
  • And the exceptions is?
    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    Wednesday, October 12, 2011 5:03 PM
  • The exception that I am getting is:

    Type 'Microsoft.Exchange.WebServices.Data.EmailMessage' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute. If the type is a collection, consider marking it with the CollectionDataContractAttribute. See the Microsoft .NET Framework documentation for other supported types.

    Is it because EmailMessage does not have a default constructor?

    I created a class library and created a "complex" class. I used it in the Operation contract. WCF was able to serialize / deserialize and the type got exported to WSDL.

    Wednesday, October 12, 2011 6:10 PM
  • Yes, the problem is with the constructor. If you cannot change that class, then you'll need to make a copy of it and return it instead of the original ErrorMessage
    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    • Marked as answer by vijayst Wednesday, October 12, 2011 6:28 PM
    Wednesday, October 12, 2011 6:22 PM
  • Thank you for your valuable inputs.

    When I am making a copy of this class, I use several other classes from the DLL. I hope I do not have to clone each of the class.

    public class EmailMessageClone
    {
          public MimeContent MimeContent { get; set; }
          public AttachmentCollection AttachmentCollection { get; set; }
    }
    

    In the above example, will MimeContent, AttachmentCollection be serialized correctly, if they are in an external DLL and have default constructors?

    Wednesday, October 12, 2011 6:28 PM
  • If they have default ctor it should work
    Please mark posts as answers/helpful if it answers your question.
    Senior Consultant on WCF, ASP.NET, Siverlight, and Entity Framework. Author of Microsoft's Official WCF 4 Course. Co-author of the Microsoft HPC/Azure burst whitepaper.
    Visit my blog: http://blogs.microsoft.co.il/blogs/idof
    Wednesday, October 12, 2011 6:42 PM