Reputation: 4260
I have an .asmx webservice that works fine except it doesn't read prostdata. It looks like this:
namespace mynamespace.liby
{
[ScriptService]
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class json : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<sp_tech_keywordSearch_Result> search()
{
const string KEY = "key";
var key = string.Empty;
// convert postdata to dictionary
// this piece of code works fine in my asp.net MVC controller but not here :-(
string data = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
Dictionary<string, string> postData = JsonConvert.DeserializeObject<Dictionary<string, string>>(data);
if (postData.ContainsKey(KEY)) { key = postData[KEY]; }
var db = new my_Entities();
var result = db.sp_myStoredProcedure(key);
return result.ToList();
}
}
}
If I comment out the line
// if (postData.ContainsKey(KEY)) { key = postData[KEY]; }
the procedure is called without parameter and returns a list of values.
But when I keep this line it throws an error saying postData
is null.
When i check it in debug mode I see that data
is an empty string.
I call this service with angular like this:
callService: function () {
var postData = new Object();
postData.key =4;
return $http({ method: 'POST', url: MY_SERVICE, data: postData })
.then(function (obj) {
_Result = obj.data.d;
return true;
}, function (err) {
_Result = [];
console.log('serviceError');
return false;
});
},
My guess is that my code for reading postdata is wrong somehow. I have the same code running in an ASP.NET MVC controller and it works fine there. Does anyone have an idea on how to fix this?
Upvotes: 4
Views: 1850
Reputation: 4260
Adding this solved the problem
// goto beginning of the inputstream
HttpContext.Current.Request.InputStream.Position = 0;
// convert postdata to dictionary
string data = new StreamReader(HttpContext.Current.Request.InputStream).ReadToEnd();
For some reason the 'cursor' of the inputstream is on the last position and ReadToEnd()
results in reading an empty string.
Upvotes: 4