gpt4 book ai didi

c# - 将 IEnumerable 作为参数传递给 WCF 服务

转载 作者:数据小太阳 更新时间:2023-10-29 02:30:25 25 4
gpt4 key购买 nike

我得到的 xml 看起来像:

<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>1</item>
<item>2</item>
<item>3</item>
</items>

和一个 wcf 服务契约(Contract):

[ServiceContract(Namespace = "", Name = "MyService", SessionMode = SessionMode.NotAllowed)]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
void DoWork(IEnumerable<int> items);
}

服务绑定(bind)是基本的 http。但是当我尝试将该 xml 发布到 wcf 方法时出现错误:无法反序列化具有根名称“items”和根命名空间“”的 XML 消息

wcf 方法应该如何正确处理该 xml?

最佳答案

您的服务契约(Contract)似乎没有正确设置。

我认为您需要实现一个“包装器”类,它定义了一个与您的 XML 相匹配的类型结构。

例如:

[XmlRoot("items")]
public class MyItems
{
[XmlElement("item")]
public List<int> Items { get; set; }
}

我刚刚组装了一个快速测试应用程序,并使用您的示例 XML(通过 soapUI REST 客户端)成功验证了界面。

问候,

关于c# - 将 IEnumerable<int> 作为参数传递给 WCF 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21729562/

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