我有一个带有服务接口的WCF服务
[ServiceContract] public interface IMyService { [OperationContract] [ServiceKnownType(typeof(Person))] [ServiceKnownType(typeof(Employee))] IPerson GetPerson(); }我对GetPerson的实现是
public IPerson GetPerson() { IPerson obj = new Person(); obj.FirstName = "Bhuvan"; obj.LastName = "Ram"; return obj; }在我的客户端,我使用的很简单
KnownType.MyServiceClient obj = new KnownType.MyServiceClient(); Person objp = (Person)obj.GetPerson()'但是当我试图访问时,我收到一个错误
底层连接已关闭:连接意外关闭。
我的堆栈跟踪是这样的
在System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout) 在System.Net.HttpWebRequest.GetResponse() at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpChannelRequest.WaitForReply(TimeSpan timeout)..
我的员工班是
[DataMember(Name = "LastName")] public string LastName { get; set; } #endregion> [DataContract(Name =“Employee”)] [KnownType(typeof(IEmployee))] public class Employee:IEmployee {[DataMember] public string EmployeeName {get; set; }}
>我的人类是[DataContract(Name =“Person”)] [KnownType(typeof(IPerson))] 公共类人物:IPerson { #region IPerson成员[DataMember(Name =“FirstName”)] public string FirstName {get; 组; }
}
我该如何解决这个问题?
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
[DataMember(Name = "LastName")] public string LastName { get; set; } #endregion> [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; }
}
How do I resolve this?
最满意答案
Concrete类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; } }可以在方法签名上使用ServiceKnownType属性,而不是在整个类上使用KnownType属性
[OperationContract] [ServiceKnownType(typeof(Person))] IPerson GetPerson();来源: 查看此链接了解详情
参考: 使用Knowntype
注意:
仅在各地使用Knowntype或仅在ServiceContract上使用ServiceKnowntype。 尝试使用Person和Employee类的DataContract的唯一命名空间。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.更多推荐
发布评论