0

I have a singleton C# service that exposes it's API via a service contract. A specific function runs fine in the development environment I have setup, but I get an error 400 from this service on a specific query (the other queries work just fine, there's many of them ~25).

The stack is basically JS ui -> IIS (10) -> C# Service. The query fails between IIS and the C# Service.

The site is running IIS 10 and the C# service is C# 2015.

I have tried renaming the function, changing it's signature (creating a custom class that passes both of the parameters in a single JSON string as opposed to two separate items). I've also tried GET, POST and PUT methods and using the POST method I get a higher success rate on sites (say, 14/16 work with POST and only 6/16 work with GET/PUT). All of them work in the development environment, with the utf-8 chartset

There's three parts to the problem. The C# Service has an interface and a class implementing it:

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

Class:

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

Which is invoked from IIS using:

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

The full error (without stack trace) is:

"ExceptionMessage":"The remote server returned an error: (400) Bad Request."

but I'm expecting a 200.

  • The obvious answer is that the IIS configuration differs between your machine and the machines where the service is deployed. – Ian Kemp Apr 02 '19 at 07:02
  • @IanKemp Normally I'd agree with you, but why do the other queries work? Which configuration settings should I be looking for? – Shaun Van Zyl Apr 02 '19 at 07:13
  • First use FRT to learn what returns 400 https://learn.microsoft.com/en-us/iis/troubleshoot/using-failed-request-tracing/troubleshooting-failed-requests-using-tracing-in-iis – Lex Li Apr 02 '19 at 10:50

0 Answers0