C#RESTful服务-HTTP 504/连接重置
我有一个C#应用程序,它通过RESTful服务获取和返回信息。 我以前处理这些操作的方式是,这些方法只返回JSON字符串,然后将这些字符串包装成XML 为了防止出现这种情况,我将ResponseFormat更改为JSON,现在返回DataContracts,它们都来自一个抽象类:C#RESTful服务-HTTP 504/连接重置,c#,json,rest,webinvoke,webget,C#,Json,Rest,Webinvoke,Webget,我有一个C#应用程序,它通过RESTful服务获取和返回信息。 我以前处理这些操作的方式是,这些方法只返回JSON字符串,然后将这些字符串包装成XML 为了防止出现这种情况,我将ResponseFormat更改为JSON,现在返回DataContracts,它们都来自一个抽象类: using System.Runtime.Serialization; /// <summary> /// Abstract class for defining a standard definitio
using System.Runtime.Serialization;
/// <summary>
/// Abstract class for defining a standard definition of a command return result.
/// A command is not required to return a result, however it is recommended.
/// A return result must implement the properties defined in this abstract class.
/// A command may return an anonymous object instead of an instance of this abstract class.
/// </summary>
[DataContract(Namespace = "")]
public abstract class ICommandResult {
// Don't let the name disturb you, this used to be an interface.
/// <summary>
/// Gets or sets the message.
/// </summary>
[DataMember]
public abstract string Message { get; set; }
}
我用来测试这一点的具体方法如下:
IRestService:
[OperationContract]
[WebGet(UriTemplate = Routing.GetRoutesRoute, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
ICommandResult GetAllRoutes();
RestService(实现):
实际的命令实现(因此也可以通过控制台调用):
ICommandResult命令\u打印路由(命令发送方,参数字符串[]args){
var fields=typeof(Routing).GetFields();
var fieldValues=新字典();
var outputToConsole=true;
if(args.Count(x=>x.ToLowerInvariant().Equals(“--no output”))==1)
outputToConsole=false;
foreach(字段中的变量字段)
Add(field.Name,(string)field.GetRawConstantValue());
var output=JsonConvert.SerializeObject(字段值,格式.缩进);
if(输出解决方案)
写线(输出);
//返回新的CommandResult(“找到以下路由”,字段值);
returnnewcommandresult(“找到以下路由(JSON编码)”,输出);//尝试不同的返回类型
}
如何修复和防止这种情况?我认为这是一个序列化问题,与您的Abstract DataContract有关 看这个: 我认为您需要使用KnownType属性指定抽象基类上的所有子类 通过这种方式,DataContractSerializer可以在序列化和反序列化对象时识别所有类型
[DataContract]
[KnownType(typeof(SubClassName1))] <-
[KnownType(typeof(SubClassName2))] <-
[KnownType(typeof(SubClassName3))] <-
public abstract class ClassName
{
...
}
[DataContract]
[KnownType(typeof(SubClassName1))]在这种情况下不是一种非常可伸缩的方式。我试试看,然后再打给你。这似乎解决不了问题。除非我必须定义返回值中可能使用或不使用的每个类型。它必须比这个简单。@Beatslegher是的,如果您想在wcf dto中使用抽象类,您必须定义所有这些属性。我尝试在vs中重新创建您的情况,如果我调用返回抽象类的OperationContract,我可以调试它,但当它返回时,响应不会到达。(就像你看到的那样)。如果我放置属性[KnownType(typeof(SubClassName1))],我可以看到wcf响应。如果我返回子类,响应是(示例):{“Message”:“NoAbstract”}如果我返回抽象类,响应是:{“\uu type”:“TestResult”,“Message”:“Test”}我想这是我的子类。如果我记得的话,在你的帖子中,我在XMLException中看到了单词“\uuuu type”。也许这两件事有某种联系?(我不知道,我想)最后一件事,您是否尝试在“普通”类中修改您的抽象类,并查看WCF是否返回一些数据?如果您尝试这个方法,您/我们可以知道“504错误”是由抽象类还是其他东西给出的。
public ICommandResult GetAllRoutes() {
var results = ExecuteCommand("print-routes", "--no-output");
// ExecuteCommand returns an ICommandResult object
return results;
}
ICommandResult Command_PrintRoutes(Command sender, params string[] args) {
var fields = typeof(Routing).GetFields();
var fieldValues = new Dictionary<string, string>();
var outputToConsole = true;
if (args.Count(x => x.ToLowerInvariant().Equals("--no-output")) == 1)
outputToConsole = false;
foreach (var field in fields)
fieldValues.Add(field.Name, (string)field.GetRawConstantValue());
var output = JsonConvert.SerializeObject(fieldValues, Formatting.Indented);
if (outputToConsole)
WriteLine(output);
//return new CommandResult<Dictionary<string, string>>("Found following routes", fieldValues);
return new CommandResult<string>("Found following routes (JSON-encoded)", output); // Trying out different return types
}
[DataContract]
[KnownType(typeof(SubClassName1))] <-
[KnownType(typeof(SubClassName2))] <-
[KnownType(typeof(SubClassName3))] <-
public abstract class ClassName
{
...
}