我的ASP.NET REST应用返回400(错误请求)

时间:2019-01-11 21:23:33

标签: azure asp.net-web-api

我的REST服务(作为AppService在Azure上运行的ASP.NET)运行良好,直到最近的更改很小为止。如果我转到https://update.windward.net/,它将返回正常值。但是到https://update.windward.net/Service/version的POST会返回400。

我有99%的确定不会在之前调用我的服务。这是服务入口点:

        [WebGet(UriTemplate = "")]
        public string GetTest()
        {
            return $"Windward update REST service version {AssmVersion}; " +
                   $"Newest AutoTag version: {ConfigurationManager.AppSettings["AutoTagVersion"]}; " +
                   $"Most recent exception {mostRecentTime:G} : {mostRecentError}";
        }


        [WebInvoke(UriTemplate = "Service/version", Method = "POST", RequestFormat = WebMessageFormat.Xml,
            ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.Bare)]
        public XmlElement GetVersion(XmlElement root)
        {

            try
            {
//...
            }
            catch (Exception ex)
            {
                log.Error("GetVersion", ex);
                mostRecentError = $"GetVersion({root.Name}): {ex.Message}";
                mostRecentTime = DateTime.UtcNow;
                throw;
            }
        }

我无法使日志正常工作,因此调试所需的只是mostRecentError字符串。

2 个答案:

答案 0 :(得分:0)

我建议您从属性中剥离RequestFormatResponseFormatBodyStyle,并将root方法参数更改为string,以查看请求是否是否到达该方法。

一旦确定了这一点,请尝试重新添加已删除/更改的代码(一次添加一次),以查看影响该方法调用的原因。

希望有帮助!

答案 1 :(得分:0)

对于那些有类似问题的人来说,更有趣的是-我是如何找到问题的。

我使用了the Azure diagnostic tool described here (including how to use it)。由此,我发现我的服务方法被调用,并且在该代码中它引发了异常。

第1项-获得400可能意味着您的服务正在被调用,但会引发异常。

第2项,日志记录有效,但是Azure工具要好得多(至少就我而言)。