2
votes

I have a WCF service with a service interface

[ServiceContract]    
public interface IMyService
{
    [OperationContract]
    [ServiceKnownType(typeof(Person))]
    [ServiceKnownType(typeof(Employee))]
    IPerson GetPerson();
 }

and my Implementation of GetPerson is

 public IPerson GetPerson()
 {
        IPerson obj = new Person();
        obj.FirstName = "Bhuvan";
        obj.LastName = "Ram";        
        return obj;
  }

And in my client as simple I used

KnownType.MyServiceClient obj = new KnownType.MyServiceClient();
Person objp = (Person)obj.GetPerson()'

But when I am trying to access, I am receiving an error

The underlying connection was closed: The connection was closed unexpectedly.

and my stack trace is like this

at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)
at System.Net.HttpWebRequest.GetResponse()
at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)..

My Employee Class is

> [DataContract(Name = "Employee")] [KnownType(typeof(IEmployee))]
public class Employee:IEmployee { [DataMember] public string EmployeeName { get;set; } }

> My Person Class is [DataContract(Name = "Person")] [KnownType(typeof(IPerson))]
public class Person: IPerson {
#region IPerson Members [DataMember(Name = "FirstName")] public string FirstName { get; set; }

 [DataMember(Name = "LastName")]
 public string LastName { get; set; }
 #endregion

}

How do I resolve this?

1
Post some more code of IPerson to know how the attributes are declaredC-va
If on .NET 4.5 and it worked on .NET 4.0, known issue. Search here or on Google.leppie
i am using .Net 4.0 and if i use only [ServiceKnownType(typeof(Person))] its working fine . but if i use multiple known types then as [ServiceKnownType(typeof(Person))] [ServiceKnownType(typeof(Employee))] Then its not Working. I would like to access Employee and Person as known types in my classBhuvan
Use any one(Knowntype/ServiceKnownType). if you use ServiceKnownType in your ServiceContract, no need to use knowntype in concrete classes. Or else use Knowntype every where.C-va
Is Employee not IPerson?Jens Kloster

1 Answers

0
votes

There might be a serialization issue on Concrete class Person.

 [ServiceContract]
 public interface IMyService
 {
    [OperationContract]
    IPerson GetPerson();
 } 

 public interface IPerson
 {
   string FirstName { get; set; }
   string LastName { get; set; }
 }

   [DataContract(Name = "Person")]
   [KnownType(typeof(IPerson))]
  public class Person : IPerson
  {
     [DataMember(Name = "FirstName")]
     public string FirstName { get; set; }

     [DataMember(Name = "LastName")]
     public string LastName { get; set; }
  }

Can use ServiceKnownType attribute on the method signature, rather than using the KnownType attribute on the whole class

[OperationContract]
[ServiceKnownType(typeof(Person))]
IPerson GetPerson();

Source: Check this link for detail

Ref : Using Knowntype

Note:

  • Use Knowntype everywhere or ServiceKnowntype on ServiceContract only.
  • Try with unique namespace to DataContract of Person and Employee classes.