WCF合同

编程入门 行业动态 更新时间:2024-10-25 06:24:54
本文介绍了WCF合同 - 命名空间和SerializationExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用的是第三方Web服务提供了以下调用和响应

I am using a third party web service that offers the following calls and responses

api.athirdparty/rest/foo?apikey=1234 <response> <foo>this is a foo</foo> </response>

api.athirdparty/rest/bar?apikey=1234 <response> <bar>this is a bar</bar> </response>

这是合同和支持类型我写了

This is the contract and supporting types I wrote

[ServiceContract] [XmlSerializerFormat] public interface IFooBarService { [OperationContract] [WebGet( BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "foo?key={apikey}")] FooResponse GetFoo(string apikey); [OperationContract] [WebGet( BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Xml, UriTemplate = "bar?key={apikey}")] BarResponse GetBar(string apikey); } [XmlRoot("response")] public class FooResponse { [XmlElement("foo")] public string Foo { get; set; } } [XmlRoot("response")] public class BarResponse { [XmlElement("bar")] public string Bar { get; set; } }

然后我的客户看起来像这样

and then my client looks like this

static void Main(string[] args) { using (WebChannelFactory<IFooBarService> cf = new WebChannelFactory<IFooBarService>("thirdparty")) { var channel = cf.CreateChannel(); FooResponse result = channel.GetFoo("1234"); } }

当我运行此我得到下面的异常

When I run this I get the following exception

无法反序列化XML主体与根的名字'反应'和根命名空间'(操作'的getFoo和合同(IFooBarService,tempuri/)),使用XmlSerializer的。确保对应于XML类型被添加到服务的已知类型集合

Unable to deserialize XML body with root name 'response' and root namespace '' (for operation 'GetFoo' and contract ('IFooBarService', 'tempuri/')) using XmlSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.

如果我注释掉 IFooBarService 的 GetBar 运行,它工作正常。我知道我在这里缺少了一个重要的概念 - 只是不知道要寻找比较什么。什么是构建我的合同类型的正确方法,使他们能够正确地反序列化?

If I comment out the GetBar operation from IFooBarService, it works fine. I know I'm missing an important concept here - just don't know quite what to look for. What is the proper way to construct my contract types, so that they can be properly deserialized?

推荐答案

我会说你的第三方服务受到严重破坏。有一个命名空间冲突在这里 - 有命名的两个元素响应,但不同的XML Schema类型

I'd say your third-party service is badly broken. There's a namespace collision here - there are two elements named response but with different XML Schema types.

我认为你将不得不不使用任何.NET技术,涉及反序列化这个XML。就没有办法告诉.NET成反序列化XML而.NET类型。

I think you're going to have to not use any .NET technology that involves deserializing this XML. There would be no way to tell .NET into which .NET type to deserialize the XML.

您只好手工做的。的LINQ to XML是方便的用于这一目的。

You'll just have to do it by hand. LINQ to XML is handy for this purpose.

更多推荐

WCF合同

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

发布评论

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

>www.elefans.com

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