WCF 休息多个数据合同包装

发布于 2024-09-06 21:11:05 字数 1058 浏览 5 评论 0原文

所以我使用 Rest WebHttp 调用 wcf web 服务。我还设置了第二个绑定 mex 进行测试。我的方法有效,但是当我尝试通过客户端应用程序通过 fiddler 或直接 httppost 进行测试时,我收到 400 Bad request 错误。我将多个数据契约传递给该方法。它迫使我使用wrapped的BodyStyle,但我不知道如何格式化我的xml以进行测试。我创建了一个测试方法来尝试这个方法并出现同样的错误。这是我的代码:我的所有其他方法都适用于一个数据契约,但我有两个数据契约的方法则不然。我相信问题出在这篇文章底部的 xml 中,它需要被包装,但我不知道如何包装它。

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)]
string Test(wsAuth a, wsMed m);

[DataContract(Namespace = "")]
public class wsMed
{
 [DataMember]
 public string Test{ get; set; }
}

[DataContract(Namespace = "")]
public class wsAuth
{
 [DataMember]
 public string UserName { get; set; }
 [DataMember]
        public string Password { get; set; }
 [DataMember]
 public string DeviceId { get; set; }
}

我正在使用的 xml

<Test>
<wsAuth>
 <DeviceId>jenglish</DeviceId>
 <Password>treetop</Password>
 <UserName>jenglish</UserName>
</wsAuth>
<wsMed>
 <Test></Test>
</wsMed>
</Test>

So I am calling a wcf webservice using Rest WebHttp. I also have set up a second binding mex for testing. My method is working but when I try to test through fiddler or straight httppost through a client app I get a 400 Bad request error. I am passing multiple datacontracts to the method. It forces me to use the BodyStyle of wrapped but I don't know how to format my xml for the test. I have created a test method to try this out and same error. Here is my code: All my other methods work with one datacontract but this one where I have two doesn't. I believe the issue is in the xml at the bottom of this post, it needs to be wrapped but I do not know how to wrap it.

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped)]
string Test(wsAuth a, wsMed m);

[DataContract(Namespace = "")]
public class wsMed
{
 [DataMember]
 public string Test{ get; set; }
}

[DataContract(Namespace = "")]
public class wsAuth
{
 [DataMember]
 public string UserName { get; set; }
 [DataMember]
        public string Password { get; set; }
 [DataMember]
 public string DeviceId { get; set; }
}

my xml that I am using

<Test>
<wsAuth>
 <DeviceId>jenglish</DeviceId>
 <Password>treetop</Password>
 <UserName>jenglish</UserName>
</wsAuth>
<wsMed>
 <Test></Test>
</wsMed>
</Test>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

童话 2024-09-13 21:11:05

我担心你会为此而挣扎。首先,您缺少 DataContract 序列化命名空间。 DataContractSerializer 对于 xml 格式非常挑剔。

最近有一个问题 来自某人试图做一些更简单的事情,但他很难让 WCF 接受它。

这里是有关如何格式化包装参数的说明。

I'm afraid you are going to struggle with this. First off, you are missing the DataContract serialization namespaces. The DataContractSerializer is pretty picky about how it wants it's xml formated.

There was a question just recently from someone trying to do something much simpler and he was having difficulty getting WCF to accept it.

Here is a an explanation of how to format wrapped parameters.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文