我有一个单例 C# 服务,它通过服务合同公开它的 API。一个特定的功能在我设置的开发环境中运行良好,但我在特定查询上从该服务收到错误 400(其他查询工作正常,其中有很多 ~25)。
堆栈基本上是 JS ui -> IIS (10) -> C# Service。IIS 和 C# 服务之间的查询失败。
该站点运行的是 IIS 10,C# 服务是 C# 2015。
我尝试重命名该函数,更改它的签名(创建一个自定义类,将两个参数都传递到一个 JSON 字符串中,而不是两个单独的项目)。我还尝试了 GET、POST 和 PUT 方法,使用 POST 方法我在网站上获得了更高的成功率(例如,14/16 使用 POST,只有 6/16 使用 GET/PUT)。它们都在开发环境中工作,使用 utf-8 图表集
问题分为三个部分。C# Service 有一个接口和一个实现它的类:
[ServiceContract(SessionMode = SessionMode.NotAllowed)]
public interface ITreeQuery
{
[WebInvoke(Method = "POST", UriTemplate = "GRTBP", BodyStyle = WebMessageBodyStyle.Wrapped, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
TreeResult GRTBP(string query);
}
班级:
[ServiceBehavior(IncludeExceptionDetailInFaults = true, AddressFilterMode = AddressFilterMode.Any, InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single)]
public class TreeQuery : ITreeQuery
{
public TreeResult GRTBP(string query)
{
ReasonTreeQuery _query = JsonConvert.DeserializeObject<ReasonTreeQuery>(query);
Func<string, string, List<List<AFCacheElement>>> getReasonTree = (value1, value2) => (_databaseMonitor.GetReasonTreeByPath(value1, value2));
return Query(DecodeString(_query.ElementPath), DecodeString(_query.ElementTemplate), getReasonTree);
}
}
使用以下命令从 IIS 调用:
public class ReasonTreeController : ApiController
{
[Route("GetReasonTreeByPath")]
public JObject GetReasonTreeByPath(string elementPath, string elementTemplate)
{
try
{
WebRequest webRequest = WebRequest.Create(@"https://" + System.Configuration.ConfigurationManager.AppSettings["LocalHost"] + @"/tree-query/GRTBP");
webRequest.Method = @"POST";
webRequest.ContentType = @"application/json; charset=utf-8";
ReasonTreeQuery query = new ReasonTreeQuery(elementPath, elementTemplate);
Dictionary<string, string> content = new Dictionary<string, string> {
{ "query", JsonConvert.SerializeObject(query) }
};
string jsonResponse = string.Empty;
using (StreamWriter streamWriter = new StreamWriter(webRequest.GetRequestStream()))
{
streamWriter.Write(JsonConvert.SerializeObject(content));
streamWriter.Flush();
streamWriter.Close();
HttpWebResponse httpResponse = (HttpWebResponse)webRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
jsonResponse = streamReader.ReadToEnd();
}
}
JObject jObject = JObject.Parse(jsonResponse);
return jObject;
}
catch (Exception ex)
{
throw ex;
}
}
完整的错误(没有堆栈跟踪)是:
"ExceptionMessage":"远程服务器返回错误:(400) Bad Request。"
但我期待200。