我有一个 C# 应用程序,它通过 RESTful 服务获取和返回信息。我之前处理这些操作的方式是方法简单地返回 JSON 字符串,然后将这些字符串包装在 XML 中。
为了防止这种情况,我将 ResponseFormat 更改为 JSON,现在我返回 DataContracts,它们都派生自一个抽象类:
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; }
}
为了应用 DataContract 属性,我将上面的接口更改为(现在)抽象类。
每个衍生产品还应用 DataContract 和 DataMember 属性。
当我调用服务的路由时,命令被执行,我可以在控制台中看到输出。但是,当返回返回值时,我在控制台中看到以下内容,并且 Web 浏览器保持为空。Fiddler(在 Windows 上)向我显示 HTTP 504 错误,而我的 Mac 向我显示连接重置。
XmlException (Dropped Connection?): On JSON writer data type 'type' must be specified. Object string is 'object', server type string is '__type'.
我用来测试的具体方法如下:
IRest服务:
[OperationContract]
[WebGet(UriTemplate = Routing.GetRoutesRoute, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
ICommandResult GetAllRoutes();
RestService(实现):
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
}
如何解决和预防这种情况?