gpt4 book ai didi

.net - WCF:接口(interface)、泛型和 ServiceKnownType

转载 作者:行者123 更新时间:2023-12-04 05:21:56 30 4
gpt4 key购买 nike

我有以下内容:

[ServiceContract]
[ServiceKnownType(typeof(ActionParameters))]
[ServiceKnownType(typeof(SportProgram))]
[ServiceKnownType(typeof(ActionResult<SportProgram>))]
public interface ISportProgramBl
{
[OperationContract]
IActionResult<ISportProgram> Get(IActionParameters parameters);
}

当我运行 Get 方法时,出现以下错误:

There was an error while trying to serialize parameter http://tempuri.org/:GetResult. The InnerException message was 'Type 'PPS.Core.DomainModel.Support.Action.ActionResult`1[ [PPS.Core.DomainModel.SportProgram.ISportProgram, PPS.Core.DomainModel, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' with data contract name 'ActionResultOfanyType: http://schemas.datacontract.org/2004/07/PPS.Core.DomainModel.Support.Action' is not expected. Add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'. Please see InnerException for more details.



从这个错误中我可以看到它可以解析 ActionResult 但它无法解析 ISportProgram 即使我有 ServiceKnownType(typeof(ActionResult < SportProgram >)) 在我的服务界面...

请注意,这是生成的引用 stub ,看起来像这样,所以我可以看到已知类型被正确地引入:
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
[System.ServiceModel.ServiceContractAttribute(ConfigurationName="SportProgramStb.ISportProgramBl")]
public interface ISportProgramBl {

[System.ServiceModel.OperationContractAttribute(Action="http://tempuri.org/ISportProgramBl/Get", ReplyAction="http://tempuri.org/ISportProgramBl/GetResponse")]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.SportProgram.SportProgram))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.Support.Action.ActionParameters))]
[System.ServiceModel.ServiceKnownTypeAttribute(typeof(PPS.Core.DomainModel.Support.Action.ActionResult<PPS.Core.DomainModel.SportProgram.SportProgram>))]
object Get(object parameters);
}

为什么会出现这种情况????请注意它正确地通过 WCF 服务......但是当返回结果时它会抛出异常。

最后 ActionResult 看起来像这样:
public interface IActionResult<T> 
{
T Result { get; set; }
}

干杯
安东尼

最佳答案

好吧,我认为这是 SOA 与 OOP“阻抗不匹配”的另一种情况。两个世界是完全不同的。

在 WCF 中,从客户端传递到服务器的所有内容都以 的形式传递。序列化消息 - 没有使用引用。

这意味着:您想要在客户端序列化、将其发送到服务器、反序列化并在那里使用它的所有内容都必须是 混凝土 - 你不能传递接口(interface),你不能使用“未解析”的泛型——你需要把它拼出来。基本上,从客户端通过线路传递到服务器的所有内容都必须在 XML 模式中表示。

这有很多含义:

  • 没有接口(interface) - 你不能传递接口(interface) - 你需要使用具体类型
  • 没有“自动”继承——你不能只定义一个基类并传递基于它的派生类——这些也需要指定(这就是 ServiceKnownType 属性的用途)
  • 没有自动泛型 - 同样,您需要使用具体类型

  • 这听起来可能有很多限制——但这是因为 WCF 使用所有基于消息的通信——它无法处理引用、继承、泛型等——你需要把它拼出来。

    所以我本身并没有真正的答案——我只是认为你需要重新考虑你的策略并改变你的客户端和服务器通过 WCF 交换信息的方式。

    马克

    PS:我做了更多的研究,与我的理解相反,似乎有一种方法可以序列化任何基于接口(interface)和/或抽象基类的东西,只要你能确定它总是只有 . NET 网络的两端(即 不是 可与 Java 等互操作)。

    Aaron Skonnard blog post on the NetDataContractSerializer和另一个 blog postyet another展示如何使用 NetDataContractSerializer 来传递诸如 IPerson 之类的东西作为方法的参数。

    关于.net - WCF:接口(interface)、泛型和 ServiceKnownType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1376495/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com