Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WCF中的JSON请求和响应_C#_Json_Wcf - Fatal编程技术网

C# WCF中的JSON请求和响应

C# WCF中的JSON请求和响应,c#,json,wcf,C#,Json,Wcf,我正在尝试以JSON格式从WCF web服务(用c#编写)发出请求并接收响应。 这是端点的配置: <service behaviorConfiguration="UserServiceBehavior" name="UserService"> <endpoint address="JSON" binding="webHttpBinding" contract="IUserService" behaviorConfiguration="JS

我正在尝试以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>
对于返回CLR类型的方法,它非常有效:响应是JSON格式的(我想也是请求)。 对于返回非CLR类型的方法(在我的例子中,是代理客户端),如果我尝试以JSON发出请求并接收响应,服务器会给我带来404错误,但如果我切断此选项:

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

发现问题。多亏了SvcTracingTool,我发现所有这些都是由于序列化问题造成的,正如引发的异常所说:

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


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

这是一个基于个人经验的建议:如果可能,请改为使用Web API。我现在很开心,因为我已经摆脱了WCF的负担。这是不可能的。我发现了问题,然后我会回答我自己的问题。
[WebInvoke(Method = "GET", UriTemplate = "myUriTemplate", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json