samedi 5 avril 2014

c# - erreur 500 lors de l'appel d'API Web avec XML - Stack Overflow


I developed a web api in .net 4.5. When I test the api call, I am getting a 500 error. This behavior only happens when I send in XML. If I use JSON of Form URL encoded string the server handles the request properly.


I added


GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

to the WebApiConfig.cs and the error returned is:


<Error><Message>An error has occurred.</Message><ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage><ExceptionType>System.NullReferenceException</ExceptionType><StackTrace>   at System.Web.Http.ApiController.&lt;InvokeActionWithExceptionFilters&gt;d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()</StackTrace></Error>

My header are:


User-Agent: Fiddler
Content-Type: application/xml; charset=utf-8
Host: corebeta.pexcard.com
Content-Length: 119

My request is:


<Admin.CardListRequest>
<username>TestUser</username>
<password>GoodPassword</password>
</Admin.CardListRequest>

My class looks like this:


public class CardListRequest{
public string UserName { get; set; }
public string Password { get; set; }
}

The WebAPI code looks like this(log is using log4net, and nothing is written to that log):


    /// <summary>
/// Gets the list of open cardholders
/// </summary>
/// <param name="UserName">The username you use to log on to pexcardadmin.com</param>
/// <param name="Password">The password you use to log on to pexcardadmin.com</param>
/// <returns>A list of cardholders with balances</returns>
[HttpGet]
public List<Admin.Account> CardList(string UserName, string Password)
{
try
{

log.Info(String.Format("Admin Parameters: <UserName={0}>", UserName));
AuthenticateApi(UserName, Password, "cardList");
var Ret = GetCoreCardCardList(UserName, Password);
APIData.Response = "Success";


return Ret;
}
catch (Exception e)
{
log.Error(e);
APIData.Response = e.Message;
throw e;
}
finally
{
APIData.ResponseTime = DateTime.Now;
APILog.LogAPI(APIData);
}

}
/// <summary>
/// Gets the list of open cardholders
/// </summary>
/// <param name="Req"> The Request Parameter</param>
/// <returns>A list of cardholders with balances</returns>
[HttpPost]
public List<Admin.Account> CardList(Admin.CardListRequest Req)
{
APIData.ParamBlob = JsonConvert.SerializeObject(Req);
return CardList(Req.UserName, Req.Password);

}

Does anyone have any ideas on why this error is being thrown?


Thanks, Steven




This was resolved by ensuring that that the XML is correct. To do this I created a new web api function that had the return type I wanted and with the fields I wanted filled in, and used that exact response to send into the web api.



I developed a web api in .net 4.5. When I test the api call, I am getting a 500 error. This behavior only happens when I send in XML. If I use JSON of Form URL encoded string the server handles the request properly.


I added


GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

to the WebApiConfig.cs and the error returned is:


<Error><Message>An error has occurred.</Message><ExceptionMessage>Object reference not set to an instance of an object.</ExceptionMessage><ExceptionType>System.NullReferenceException</ExceptionType><StackTrace>   at System.Web.Http.ApiController.&lt;InvokeActionWithExceptionFilters&gt;d__1.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()</StackTrace></Error>

My header are:


User-Agent: Fiddler
Content-Type: application/xml; charset=utf-8
Host: corebeta.pexcard.com
Content-Length: 119

My request is:


<Admin.CardListRequest>
<username>TestUser</username>
<password>GoodPassword</password>
</Admin.CardListRequest>

My class looks like this:


public class CardListRequest{
public string UserName { get; set; }
public string Password { get; set; }
}

The WebAPI code looks like this(log is using log4net, and nothing is written to that log):


    /// <summary>
/// Gets the list of open cardholders
/// </summary>
/// <param name="UserName">The username you use to log on to pexcardadmin.com</param>
/// <param name="Password">The password you use to log on to pexcardadmin.com</param>
/// <returns>A list of cardholders with balances</returns>
[HttpGet]
public List<Admin.Account> CardList(string UserName, string Password)
{
try
{

log.Info(String.Format("Admin Parameters: <UserName={0}>", UserName));
AuthenticateApi(UserName, Password, "cardList");
var Ret = GetCoreCardCardList(UserName, Password);
APIData.Response = "Success";


return Ret;
}
catch (Exception e)
{
log.Error(e);
APIData.Response = e.Message;
throw e;
}
finally
{
APIData.ResponseTime = DateTime.Now;
APILog.LogAPI(APIData);
}

}
/// <summary>
/// Gets the list of open cardholders
/// </summary>
/// <param name="Req"> The Request Parameter</param>
/// <returns>A list of cardholders with balances</returns>
[HttpPost]
public List<Admin.Account> CardList(Admin.CardListRequest Req)
{
APIData.ParamBlob = JsonConvert.SerializeObject(Req);
return CardList(Req.UserName, Req.Password);

}

Does anyone have any ideas on why this error is being thrown?


Thanks, Steven



This was resolved by ensuring that that the XML is correct. To do this I created a new web api function that had the return type I wanted and with the fields I wanted filled in, and used that exact response to send into the web api.


0 commentaires:

Enregistrer un commentaire