简体   繁体   English

WCF中的JSON请求和响应

[英]JSON requests and responses in WCF

I am trying to make request and receive response from a WCF web service (written in c#) in JSON format. 我正在尝试从JSON格式的WCF Web服务(用C#编写)发出请求并接收响应。 This is the endpoint's configuration: 这是端点的配置:

 <service behaviorConfiguration="UserServiceBehavior" name="UserService">
    <endpoint address="JSON" binding="webHttpBinding" contract="IUserService" 
              behaviorConfiguration="JSONEndpointBehavior" bindingConfiguration="" name="RESTEP">
    </endpoint>
    <endpoint address="" binding="basicHttpBinding" contract="IUserService">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>

... ...

<endpointBehaviors>
    <behavior name="JSONEndpointBehavior">
      <webHttp/>
    </behavior>
  </endpointBehaviors>

The annotation on methods is something like this: 方法的注释是这样的:

[WebInvoke(Method = "GET", UriTemplate = "myUriTemplate", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]

For methods returning a CLR type it works great: responses are in JSON format (and also requests, I suppose). 对于返回CLR类型的方法,它的工作原理非常好:响应采用JSON格式(我想也是请求)。 For methods returning non-CLR types (in my case, a proxy client) if I try to make requests and to receive responses in JSON the server brings me a 404 error, but if I cut off this: 对于返回非CLR类型的方法(在我的情况下为代理客户端),如果我尝试通过JSON发出请求并接收响应,则服务器会给我带来404错误,但是如果我切断了该错误:

RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json

the server replies with an XML document containing the data I'm searching for. 服务器回复一个XML文档,其中包含我要搜索的数据。 Could this be a problem related to the client proxy? 这可能是与客户端代理有关的问题吗? How can I generate e client proxy which supports JSON serialization and de-serialization? 如何生成支持JSON序列化和反序列化的e客户端代理? If I turn on the help page I can actually see the method in methods list, but I can't fire it from the URL. 如果打开帮助页面,我实际上可以在方法列表中看到该方法,但是无法从URL触发它。

Problem found. 发现问题。 Thanks to the SvcTracingTool I've discovered that all of that was due to a serialization problem, as the exception raised says: 多亏了SvcTracingTool,我发现所有这些都是由于序列化问题引起的,如引发的异常所示:

The InnerException message was 'The type 'xxxxxxxxx' cannot be serialized to JSON because its IsReference setting is 'True'. InnerException消息为“类型'xxxxxxxxx'无法序列化为JSON,因为其IsReference设置为'True'。 The JSON format does not support references because there is no standardized format for representing references. JSON格式不支持引用,因为没有用于表示引用的标准化格式。 To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.'. 要启用序列化,请在该类型或该类型的适当父类上禁用IsReference设置。 Please see InnerException for more details. 有关更多详细信息,请参见InnerException。

Now, the next step is understand why WCF returned a 404 error instead of the exception. 现在,下一步是了解WCF为什么返回404错误而不是异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM