I want to consume my json wcf web service from the code behind of my page:
Default.aspx.cs
string result = url + "/ExecuteAction?callback=?";
var httpWebRequest = (HttpWebRequest)WebRequest.Create(result);
httpWebRequest.ContentType = "text/json";
httpWebRequest.Method = "POST";
using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
{
string json = new JavaScriptSerializer().Serialize(new
{
action = "HelloWorld",
args = "Nabila"
});
streamWriter.Write(json);
streamWriter.Flush();
streamWriter.Close();
}
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var response = streamReader.ReadToEnd();
}
My service:
[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json)]
public string ExecuteAction(string action, string args)
{
String JSONResult = String.Empty;
MethodInfo action = services.GetMethod(action, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
JSONResult =(String)action.Invoke(null, new object[] { args });
}
services.cs
public static string HelloWorld(string msg)
{
return JsonConvert.SerializeObject("Hello World"+msg);
}
I get the following exception :
consume json wcf web service from c# The remote server returned an error: (405) Method Not Allowed. System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
Note: It works when I consume my web service using javascript:
$.ajax({
type: "POST",
crossDomain: true,
timeout: 10000,
url: getUrl() + "ExecuteAction?callback=?",
data: { action: "HelloWorld", args: 'test'},
contentType: "application/json; charset=utf-8",
dataType: "jsonp"
}).done(function (msg) {alert("success");}).fail(function () {alert("error");});
Could you please help me.
Thanks.
Nabila.