我有一个C# WCF RESTful服务充当JSON传递--这意味着我的服务对另一个RESTful服务执行HTTPGet --接收回JSON响应,我需要将该响应返回给调用方。这种传递服务的原因是我们的企业不允许不同的域进行通信,这将作为一个解决方案公开。当已知类型包含在此服务中时,一切正常,但是,这意味着对于每次DataContract更改都必须更新/重新发布中间服务,因为它不应该关心接收到什么JSON才能返回调用方。
我尝试使用JSON.NET将JSON字符串反序列化为动态对象。尽管我的服务契约说它将返回一个动态类型,并且所有都符合,但我在运行时得到了一个ServiceKnownType序列化错误。我不能在运行时b/c中动态声明KnownType,我希望我的服务对类型一无所知。
(A)域X中的网络应用-> (B)面向公众的服务--> (C)域Y上的RESTful WCF服务
是否有可能甚至不将收到的JSON反序列化并在响应中将其发送出去?其他想法?有什么代码可以帮助更好地描述我自己吗?
谢谢!
这就是解决方案,感谢L.B. :)
//Call other WS and get the Json response
var request = WebRequest.Create(requestUri);
request.ContentType = "application/json; charset=utf-8";
string text;
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
text = sr.ReadToEnd();
var data = new MemoryStream(Encoding.UTF8.GetBytes(text));
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length;
return data;
}
}发布于 2013-11-19 21:12:01
您可以创建一个返回流的方法(可以认为它是“返回任何对象”)
[OperationContract, WebGet(ResponseFormat = WebMessageFormat.Json)]
public Stream SomeMethod(......)
{
//Call other WS and get the Json response
var data = new MemoryStream(Encoding.UTF8.GetBytes(json));
WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8";
WebOperationContext.Current.OutgoingResponse.ContentLength = data.Length;
return data;
}https://stackoverflow.com/questions/20081894
复制相似问题