0

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.

ekad
  • 14,436
  • 26
  • 44
  • 46
Nabila
  • 191
  • 4
  • 19

1 Answers1

0

I am observing that your problem is with DataType. In your Javascript call, you are setting datType as jsonp

dataType: "jsonp"

And in your Default.aspx.cs file you are sending plain JSON. There is difference between JSON and JSONP.

Try changing JSON type in your Deafult.aspx.cs file and then run. This might resolve your problem.

EDIT: Change your Contract as:

[OperationContract]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, UriTemplate = "json")]
public string ExecuteAction(JSONRequest request)
{
   String JSONResult = String.Empty;
   MethodInfo action = services.GetMethod(action, BindingFlags.NonPublic |    BindingFlags.Public | BindingFlags.Static);
   JSONResult =(String)action.Invoke(null, new object[] { args });
}

[DataContract]
public class JSONRequest
{
   [DataMember]
   public string Action {get; set;}
   [DataMember]
   public string Args {get; set;}
}
Community
  • 1
  • 1
Faizan Mubasher
  • 4,427
  • 11
  • 45
  • 81