C#RESTful服务 - HTTP 504 /连接重置

时间:2017-02-09 14:22:44

标签: c# json rest webinvoke webget

我有一个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'.

我用来测试它的具体方法如下:

IRestService:

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

如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我认为这是一个序列化问题,与您的abstact DataContract。

看看这个:KnownTypeAttribute - Serialization

我认为您需要使用KnownType属性在抽象基类上指定所有子类。

通过这种方式,DataContractSerializer可以在序列化和反序列化对象时识别所有类型。

 [DataContract]
 [KnownType(typeof(SubClassName1))] <-
 [KnownType(typeof(SubClassName2))] <-
 [KnownType(typeof(SubClassName3))] <-
 public abstract class ClassName
 {
      ...
 }

抱歉我的英文。我希望我能提供帮助,

斯特凡诺