【发布时间】:2016-04-07 04:28:21
【问题描述】:
我是 WCF 的新手,但是在阅读了有关 StackOverflow 的文章和几个问题之后,我设法提出了一个自托管的 RESTful 服务。 我最后面临的问题是返回给操作合约的参数值为null。我意识到之前有几个关于此的问题,但我认为我已经处理了那里提出的所有设置并且没有解决问题。 我能够得到一个json响应,但是它的json主体被发送到一个“POST”调用,它没有被反序列化到参数对象中。
代码如下:-
[ServiceContract]
public interface IExchServer
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "/init")]
DomainInfo init();
[OperationContract]
[WebInvoke(Method = "POST",
RequestFormat = WebMessageFormat.Json,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "/close")]
string close(DomainInfo connection); }
这是课程
[DataContract]
public class DomainInfo
{
[DataMember]
public string Domain { get; set; }
[DataMember]
public string UserName { get; set; }
[DataMember]
public string password {get;set;}}
在我使用的其他客户端 (PostMan) 中,我已将标头设置为 application/json。我已尝试使用以下有效负载:
{"DomainInfo":{
"Domain": "domain.com",
"UserName": "admin@domain.com",
"password": "pass@123"}}
还有
{"Domain": "domain.com",
"UserName": "admin@domain.com",
"password": "pass@123"}
在第一个中,我得到以下 WCF 跟踪
Incoming HTTP request to URI 'http://localhost:9001/ExchServer/close' matched operation 'close'
Opening System.ServiceModel.InstanceContext/17818390
Opened System.ServiceModel.InstanceContext/17818390
A message was read
An unrecognized element was encountered in the XML during deserialization which was ignored.
在第二个中我得到了
Incoming HTTP request to URI 'http://localhost:9001/ExchServer/close' matched operation 'close'.
Opening System.ServiceModel.InstanceContext/5923895
Opening System.ServiceModel.InstanceContext/5923895
A message was read
An unrecognized element was encountered in the XML during deserialization which was ignored
An unrecognized element was encountered in the XML during deserialization which was ignored
An unrecognized element was encountered in the XML during deserialization which was ignored
没有报告其他错误。在调试模式下启动 VS 时,我看到参数为空。请在这方面指导我,因为这似乎是一个非常基本的用法。 init() 合约按预期工作,并返回一个 json 字符串。 提前致谢。
【问题讨论】: