底层连接在WCF中使用Serializers关闭(The underlying connection was closed in WCF with Serializers)

编程入门 行业动态 更新时间:2024-10-21 19:33:53
底层连接在WCF中使用Serializers关闭(The underlying connection was closed in WCF with Serializers)

我有一个带有服务接口的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)..

我的员工班是

> [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; 组; }

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

}

我该如何解决这个问题?

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?

最满意答案

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.

更多推荐

本文发布于:2023-07-14 20:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1107258.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:底层   WCF   Serializers   closed   connection

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!