问题标签 [serviceknowntype]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
870 浏览

c# - 无法让我的服务调用 ServiceKnownType 辅助方法

我在运行我的 ServiceKnownType 属性中指定的辅助方法时遇到问题。为简单起见,我有两个程序集:一个带有我的服务接口和数据合约的接口,另一个带有我的服务实现和具体的数据合约。

这是我的服务及其实现的简化/精简版本。

我的服务接口.dll

MyService.dll(参考 MyService.Interface.dll)

问题的根源在于,要序列化 ​​的结果GetData(),必须告知服务具体MyData类和具体List<IMyData>泛型类,因为服务定义使用接口类型而不是具体类型。

简单的答案是用以下方式装饰 IMyService:

但是,MyData在 MyService.Interface.dll 中未引用的程序集中定义(并且不能由于循环引用。)

我的下一个想法是使用ServiceKnownTypeMyService 本身的“辅助方法”形式:

GetDataContracts据我所知,除了从未调用过之外,它应该可以工作。我尝试将它移动到一个单独的静态类中(与 MyService 平行并嵌套在其中),但在任何情况下我都无法获得一个断点来停在那里。

编辑:我还想说通过 web.config 添加已知类型也不起作用,因为我无法以这种方式添加泛型类型。您只能通过 web.config 添加简单、具体的类型:

我的混凝土List<IMyData>在程序集中没有完全限定的类型名称。

0 投票
3 回答
1593 浏览

wcf - 在 WCF 服务中使用 ServiceKnownType 时,在 SoapUI 中将多态类型作为参数传递

我有 WCF 服务;该服务有一个接收类型参数的操作Request。这只是基本类型,在调用操作时,我们实际上发送了一个类型的值Request_V1(它继承自Request),它具有我要发送的请求的完整实现。

尝试使用soapUI 测试服务时,我能够创建复杂类型的类型Request_V1(添加正确的命名空间),但由于某种原因,服务接收到的值就好像它是Request类型一样。

阅读关于ServiceKnowType,我发现在这里我需要在客户端以某种方式明确指定这种继承关系,但我还没有找到任何关于如何在soapUI上做到这一点的信息

有没有人经历过并解决过同样的问题?

谢谢

0 投票
0 回答
108 浏览

wcf - ServiceKnownType 导致 WCF 服务延迟连接

我有一个带有 wsHttpBinding 的 WCF 服务。服务合同接口使用 ServiceKnownTypeAttribute 声明为:

GetKnownTypes 方法返回 75 种类型的集合。

我尝试将服务与 ServiceKnownType 和没有 ServiceKnownType 连接。

连接 ServiceKnownTypeAttribute 大约需要 2.3 分钟,如果没有连接则不到 1 秒。

如果我不使用 ServiceKnownTypeAttribute,那么我必须为每个类编写至少 3 个 API,即大约 225 个 API :(.. 使用 ServiceKnownTypeAttribute,我编写的最大 API 是 10,效果很好。

有人可以提出解决方案吗?

0 投票
1 回答
1022 浏览

c# - WCF 使用 ServiceKnownType 返回接口类型

我正在实施以下 WCF 服务

接口 IPerson 被定义

并且实现该接口的类 Person 定义为

问题是当我引用 PersonService 并调用 GetPerson 方法时,我得到的结果是类型对象而不是类型人。要获得 Person 类型,我必须进行显式转换。

有什么方法可以让 GetPerson web 方法返回 IPERSON 类型而不进行显式转换?

0 投票
1 回答
1696 浏览

c# - Can we use Service contract on abstract class

Can we use service contract attribute on abstract class instead of interface ?

Config:

if not possible then i am looking for the reason. i got the answer but answer was not clear to me.

1st answer

You can use service contract on Abstract class but at the time of adding a reference it will give you an error showing "If a class is marked with ServiceContractAttribute, then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.

this statement is not clear then another service class cannot derive from it". This is quite logical as if it allows then which derived class method should it call.

2nd answer

It will work if we include the [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract. Basically we have to tell WCF what to serialize/deserialize.

another guy said possible if we use [ServiceKnownType] (on a Service Contract) or [KnownType] on a Data Contract

looking for more insight that if not possible then why not possible. if possible explain with sample scenario & sample code. thanks

0 投票
0 回答
81 浏览

c# - WCF - 从客户端中的服务器检索类型

当 WCF 服务器实现 ServiceKnownType 时,如何在客户端检索未知类型的合同?

造成问题的类型不会返回或用作参数。我在我的服务器上集成了 GetKnowTypes(),如下所示:WCF Problem Sending Object To Client

显然我们不能使用 T 型。所以我序列化了我的属性,我想在我的客户端反序列化,但为此我需要原始类型。

我想在客户端获得类似这样的类型:

可能吗 ?如何 ?

0 投票
0 回答
236 浏览

c# - ServiceKnownType 客户端问题

服务器端:

服务详情:

执行:

Helper 类用于收集 KnownTypes:

课程:

客户端:

在调用服务之前,尝试在客户端注册 KnownTypes:

这里有错误:

格式化程序在尝试反序列化消息时抛出异常:尝试反序列化参数 http://tempuri.org/:myCustomObject时出错。InnerException 消息是“元素” http://tempuri.org/:myCustomObject包含来自映射到名称“ http://tempuri.org/:Customer ”的类型的数据。反序列化器不知道映射到此名称的任何类型。考虑使用 DataContractResolver 或将与“客户”对应​​的类型添加到已知类型列表中 - 例如,通过使用 KnownTypeAttribute 属性或将其添加到传递给 DataContractSerializer 的已知类型列表中。有关更多详细信息,请参阅 InnerException。

可能是很长的帖子,希望你知道我在这里想要做什么:

  1. 在服务器端助手类中添加CustomerAccount类。ServiceKnownType
  2. 在客户端的加载事件中,尝试KnownTypes从存在的同一个程序集中注册。CustomerAccount
  3. 然后创建Customer类型的实例,将其ID属性设置为 111,将其发送OperationContractChangeObjectProperty. 成功执行后,它应该增加 ID。

谢谢你的时间。

0 投票
1 回答
210 浏览

c# - 嵌套异常作为 WCF 服务的返回类型

我有具有以下接口定义的 ac# WCF 服务:

如您所见,该方法应该返回 NULL(当方法执行成功时)或包含任意错误消息的异常。该方法已使用 ServiceKnownType-attributes 声明,以避免在向客户端返回异常实例时出错。这仅在返回的异常类型等于声明的 ServiceKnownTypes 之一并且 InnerException 为 NULL 时才有效。但是,如果我返回一个异常,例如,将 ArgumentException 作为 InnerException,我会收到错误消息,指出 ArgumentException 类型未知:

我现在有两个问题:

  1. 我怎样才能使它可以返回任何类型的异常,而不必将每个可能的类型声明为 ServiceKnownType 属性?
  2. 是否可以返回具有任意 InnerException 类型的嵌套异常?
0 投票
0 回答
144 浏览

c# - 我在 WCF 的客户端返回一个定义的类型。也就是说,一个通用的 T

如何在 WCF 的客户端返回定义的类型。也就是说,一个通用的 T. 使用一个通用的服务。

我有这个错误:类型'System.Collections.Generic.List`1[T]'不能作为模式类型导出,因为它是一个开放的泛型类型。如果所有泛型参数类型都是实际类型,则只能导出泛型类型。

界面:

我的服务:

我的客户: