消费在C#JSON WCF服务 [英] Consume JSON WCF services in C#

查看:182
本文介绍了消费在C#JSON WCF服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的网页背后的code消耗的json WCF Web服务:

I want to consume my json wcf web service from the code behind of my page:

Default.aspx.cs

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

我的服务:

[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

services.cs

public static string HelloWorld(string msg)
{
    return JsonConvert.SerializeObject("Hello World"+msg);
}

我得到了以下异常:

I get the following exception :

使用来自C#中的远程服务器返回错误的JSON WCF Web服务:(405)不允许的方法。 System.Web.Util.CalliEventHandlerDelegateProxy.Callback(对象发件人,EventArgs五)

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)

请注意:
它的工作原理,当我使用javascript消耗我的web服务:

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

您能帮我。

感谢。

纳比拉。

推荐答案

我观察,你的问题是与数据类型即可。在JavaScript调用,您设置的 datType JSONP

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

dataType: "jsonp"

而在你的Default.aspx.cs文件,你都发送纯JSON。有<一个href=\"http://stackoverflow.com/questions/2887209/what-are-the-differences-between-json-and-jsonp\">difference JSON和JSONP之间。

尝试在您的 Deafult.aspx.cs 文件改变JSON类型,然后运行。这可能会解决你的问题。

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

编辑:
改变你的合同为:

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

这篇关于消费在C#JSON WCF服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆