WCF中的JSON请求和响应(JSON requests and responses in WCF)

编程入门 行业动态 更新时间:2024-10-27 07:24:51
WCF中的JSON请求和响应(JSON requests and responses in WCF)

我正在尝试以JSON格式发出请求并从WCF Web服务(用c#编写)接收响应。 这是端点的配置:

<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>

方法的注释是这样的:

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

对于返回CLR类型的方法,它工作得很好:响应是JSON格式的(我想也是请求)。 对于返回非CLR类型的方法(在我的情况下,代理客户端)如果我尝试发出请求并在JSON中接收响应,服务器会给我带来404错误,但是如果我切断了这个:

RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json

服务器回复包含我正在搜索的数据的XML文档。 这可能是与客户端代理相关的问题吗? 如何生成支持JSON序列化和反序列化的e客户端代理? 如果我打开帮助页面,我实际上可以在方法列表中看到方法,但我无法从URL中触发它。

I am trying to make request and receive response from a WCF web service (written in c#) in JSON format. 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). 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:

RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json

the server replies with an XML document containing the data I'm searching for. Could this be a problem related to the client proxy? How can I generate e client proxy which supports JSON serialization and de-serialization? 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.

最满意答案

发现问题。 感谢SvcTracingTool,我发现所有这些都是由序列化问题引起的,因为引发的异常说:

InnerException消息是'类型'xxxxxxxxx'无法序列化为JSON,因为其IsReference设置为'True'。 JSON格式不支持引用,因为没有用于表示引用的标准化格式。 要启用序列化,请在类型或相应类型的父类上禁用IsReference设置。 有关更多详细信息,请参阅InnerException。

现在,下一步是理解为什么WCF返回404错误而不是异常。

Problem found. Thanks to the SvcTracingTool I've discovered that all of that was due to a serialization problem, as the exception raised says:

The InnerException message was 'The type 'xxxxxxxxx' cannot be serialized to JSON because its IsReference setting is 'True'. The JSON format does not support references because there is no standardized format for representing references. To enable serialization, disable the IsReference setting on the type or an appropriate parent class of the type.'. Please see InnerException for more details.

Now, the next step is understand why WCF returned a 404 error instead of the exception.

更多推荐

本文发布于:2023-08-06 04:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1444645.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:JSON   WCF   responses   requests

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!