2
votes

I have a .net 4.0 Framework WCF service. I referenced this WCF to a .NET Framework 2.0 Website (not a web application, just a website).

But when I invoke a method of the WCF I get an exception like:

There was an error generating the XML document.

And the Inner Exception is:

System.Uri cannot be serialized because it does not have a parameterless constructor.

I do have a parameterless consturctor and the one which was inherited in my WCF as below:

//Service Class
public class MyWcfService : IWcfService
{
     //I even put this hoping to make it work
     public MyWcfService()
     {
     }
     public MyEntity GetEntity(ArgumentObject arg)
     {
          return DoGetMyEntity(arg);
     }
}


[Serializable]
public class MyEntity : EntityBase
{   
    public DateTime LastUpdate { set; get; }

    //Here is the parameterless constructor
    public MsisdnInformation()
    {
        this.LastUpdate = DateTime.Now;
    }
}

[Serializable]
public class EntityBase
{   
    //Here is the parameterless constructor
    public EntityBase()
    {

    }
}

This WCF is working with other projects (but those projects are .NET 3.0 or later framework projects. Some of those projects are Website and some of those are WepApplication.

So far, on my internet search I found nothing to help my case.

What do I need to do to fix this error?

2
Why not believe what the exception is telling you? The System.Uri class cannot be serialized because it doesn't have a parameterless constructor. So, don't try to serialize it. The .NET 2.0 site is using the XML Serializer to access your service, and that serializer requires all types to have a parameterless constructor.John Saunders
You pointed out a valid point which I was unintentionally sending HttpContext.Current.Request.AbsoluteUri and that is the thing couldn't be serialized. Thank you.E-A

2 Answers

3
votes

As John Saunders pointed out as Comment on my question, I needed to change

httpContext.Request.Url

to

httpContext.Request.Url.ToString()

in order to pass the

System.Uri

parameter to the WCF from a .NET 2.0 Framework.

I am a little embarrassed that I overlooked this parameter.

2
votes

Use DataContract rather than Serializable. This will mean that serialization will be handled by the DataContractSerializer.

DataContract (and DataMember) attributes supersede Serializable attribute.