WCF合同 - 命名空间和SerializationExceptions [英] WCF contracts - namespaces and SerializationExceptions

查看:127
本文介绍了WCF合同 - 命名空间和SerializationExceptions的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

http://api.athirdparty.com/rest/foo?apikey=1234

<response>
  <foo>this is a foo</foo>
</response>

http://api.athirdparty.com/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,http://tempuri.org/)),使用XmlSerializer的。确保对应于XML类型被添加到服务的已知类型集合

Unable to deserialize XML body with root name 'response' and root namespace '' (for operation 'GetFoo' and contract ('IFooBarService', 'http://tempuri.org/')) 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合同 - 命名空间和SerializationExceptions的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆