0

What am I doing wrong here? I'm trying to create a WCF web service that access a different dll project. The return is a custom list that contains strings and integars. When debugging I get the following error when simply navigating to Service1.svc:

An ExceptionDetail, likely created by IncludeExceptionDetailInFaults=true, whose value is: System.InvalidOperationException: An exception was thrown in a call to a WSDL export extension: System.ServiceModel.Description contract: http://tempuri.org/:IService1 ----> System.Runtime.Serialization.InvalidDataContractException: Type 'RoTools.RoAmCalls+CustomItem' cannot be serialized. Consider marking it with the 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

So researching the error online I thought adding the KnownType would help, but I'm still getting the same error. Thanks for your help. Here is the code from the Service1.cs.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RoWebService
{
[ServiceContract]
public interface IService1
{
    [OperationContract]
    List<RoTools.RoAmCalls.CustomItem> SingleProductCheck(string productId); 
}


[KnownType(typeof(RoTools.RoAmCalls.CustomItem))]
[DataContract]
public class CompositeType
{

    List<RoTools.RoAmCalls.CustomItem> myResults;

    [DataMember]
    public List<RoTools.RoAmCalls.CustomItem> myResults
    {
        get { return myResults; }
        set { myResults = value; }
    }
}

And here is the code from the Service1.svc.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace RoWebService
{
public class Service1 : IService1
{
    public List<RoTools.RoAmCalls.CustomItem> SingleProductCheck(string productId)
    {
        log4net.ILog log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);


        var myReturn = 
        RoTools.RoAmCalls.GetPriceAndStatusSingleItem(productId, log, System.Configuration.ConfigurationManager.AppSettings["something1"],
            System.Configuration.ConfigurationManager.AppSettings["something2"]);

        return myReturn;
    }
}
}
7
  • All user-defined types used in WCF (RoTools.RoAmCalls.CustomItem in this case) must be marked with [DataContract] attribute. Its members must be marked with [DataMember] attribute. Hope this helps. Commented May 12, 2017 at 15:29
  • I already have DataContract and DataMember marked. Did I do them incorrectly? Commented May 12, 2017 at 16:10
  • I don't know, you haven't posted RoTools.RoAmCalls.CustomItem implementation. Commented May 12, 2017 at 16:11
  • Maybe that's my mistake then. All the code shown above is what I have right now. I'm not sure how to go about what your stating though. Commented May 12, 2017 at 16:15
  • Do you have access to RoTools.RoAmCalls.CustomItem source code? Commented May 12, 2017 at 16:23

2 Answers 2

1

All user-defined types used in WCF (RoTools.RoAmCalls.CustomItem in this case) must be marked with [DataContract] attribute. Its members must be marked with [DataMember] attribute.

Mark RoTools.RoAmCalls.CustomItem with [DataContract] attribute or consider create a separate class, that will store the data from RoTools.RoAmCalls.CustomItem and will be passed through WCF.

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

2 Comments

To add to this for anyone that finds this in the future. I actually had to go into the DLL class files and mark the original CustomItem, which was not in the WPF project, with [DataMember]. it was originally meant for a console application. Adding [DataMember] to the class did not negatively affect the console application so I did not need to create a separate class. Thank you very much for your help.
UPDATE : Although Yevgeniy's answer did stop my errors on the page after implementing this I actually found out that it did not return any data. I instead had to switch the DLL reference from [DataMember] to [Serialized] to return the data I needed.
0

Can you please replace your Composite class to:

[DataContract]
public class CompositeType
{
    [DataMember]
    public List<RoTools.RoAmCalls.CustomItem> myResults { get; set; }
}

1 Comment

So sorry, CustomItem class have not a DataContract attribute?

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.