tags:

views:

85

answers:

2

Hi Team,

I am getting the following exception eventhough I have specified the Datacontract and Datamember. Could you please help me to understand what the issue is?

"Type 'MyServiceLibrary.CompanyLogo' cannot be serialized. Consider marking it with the DataContractAttribute attribute, and marking all of its members you want serialized with the DataMemberAttribute attribute."

Note: The exception occurs when I run the service host. I have not even created a client.

using System.ServiceModel;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.Runtime.Serialization;
using MyServiceLibrary;

namespace MySelfHostConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            System.ServiceModel.ServiceHost myHost = new ServiceHost(typeof(NameDecorator));
            myHost.Open(); 
            Console.ReadLine();
        }
    }

}

//The Service is 

using System.ServiceModel;
using System.Runtime.Serialization;

namespace MyServiceLibrary
{
    [ServiceContract(Namespace = "http://Lijo.Samples")]
    public interface IElementaryService
    {
        [OperationContract]
        CompanyLogo GetLogo();
    }

    public class NameDecorator : IElementaryService
    {
        public CompanyLogo GetLogo()
        {
            Shape cirlce = new Shape();
            CompanyLogo logo = new CompanyLogo(cirlce);
            return logo;
        }
    }

    [DataContract]
    public class Shape
    {
        public string SelfExplain()
        {
            return "sample";
        }

    }

    [DataContract]
    public class CompanyLogo
    {
        private Shape m_shapeOfLogo;

        [DataMember]
        public Shape ShapeOfLogo
        {
            get
            {
                return m_shapeOfLogo;
            }
            set
            {
                m_shapeOfLogo = value;
            }
        }

        public CompanyLogo(Shape shape)
        {
            m_shapeOfLogo = shape;
        }
        public CompanyLogo()
        {

        }
    } 

}

//And the host config is

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.serviceModel>
    <services>

      <service name="MyServiceLibrary.NameDecorator"
               behaviorConfiguration="WeatherServiceBehavior">

        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8017/ServiceModelSamples/FreeServiceWorld"/&gt;
          </baseAddresses>
        </host>

        <endpoint address=""
                  binding="basicHttpBinding"
                  contract="MyServiceLibrary.IElementaryService" />
        <endpoint address="mex"
                  binding="mexHttpBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WeatherServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

Thanks

Lijo

+1  A: 

Your CompanyLogo class must have a default parameterless constructor otherwise it cannot be unserialized.

Edit: Created a new project, copy pasted your code and everything seems to be working fine. Ensure that your service library is correctly referenced and not using an old version without the attribute.

Julien Lebosquain
+1  A: 

Can't seem to confirm those problems - works like a charm on my machine. I can get the service host up and running, no problem. I can connect to it from the WCF Service Test Client, and request looks like this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <s:Header>
    <Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none"&gt;http://Lijo.Samples/IElementaryService/GetLogo&lt;/Action&gt;
  </s:Header>
  <s:Body>
    <GetLogo xmlns="http://Lijo.Samples" />
  </s:Body>
</s:Envelope>

and response is this:

<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
  <s:Header />
  <s:Body>
    <GetLogoResponse xmlns="http://Lijo.Samples"&gt;
      <GetLogoResult xmlns:a="http://schemas.datacontract.org/2004/07/SerializeLogo" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
        <a:ShapeOfLogo>
          <a:ShapeName i:nil="true" />
        </a:ShapeOfLogo>
      </GetLogoResult>
    </GetLogoResponse>
  </s:Body>
</s:Envelope>

From my point of view: this service works (doesn't do much - but it works).

Double- and triple-check all your parameters - something must be off somehow....

marc_s
It was a problem with version of Serialization.dll. Sorry for the confusion. Thanks for your timely support
Lijo