Jayrock: JSON and JSON-RPC for .NET

简介:
mojoPortal 项目中使用Joyrock和MagicAjaxNET,他没有使用Asp.net ajax ,是因为mojoPortal是一个运行在Windows的 .net framework或Linux,Mac OS的Mono平台上的cms系统,asp.net ajax 的协议决定了它不能应用于mono上。Joyrock的具体应用可以去看 mojoPortal  的最新版本的代码。

Joyrock 是一个LGPL的开源的软件,实现了JSON和JSON-RPC,支持微软ASP.NET框架。
看看服务器端的写法:
<%@ WebHandler Class="JayrockWeb.HelloWorld" %>
namespace JayrockWeb
{
    using System;
    using System.Web;
    using Jayrock.Json;
    using Jayrock.JsonRpc;
    using Jayrock.JsonRpc.Web;
    public class HelloWorld : JsonRpcHandler
    {
        [ JsonRpcMethod("greetings") ]
        public string Greetings()
        {
            return "Welcome to Jayrock!";
        }
    }
}
[ JsonRpcMethod("greetings") ]恰好对应于ASP.NET 的[WebMethod],深入理解一下就知道,这两个自定义属性就是起到标记作用,用来声明方法是可以远程调用的。
客户端调用:
 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
    " [url]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd[/url]">
<html xmlns=" [url]http://www.w3.org/1999/xhtml[/url]" lang="en" xml:lang="en">
<head>
    <title>Hello Jayrock</title>
    <script type="text/javascript" src="json.js"></script>
    <script type="text/javascript" src="helloworld.ashx?proxy"></script>
    <script type="text/javascript">
/* <![CDATA[ */
window.onload = function() 
{
    var s = new HelloWorld();
    alert("sync:" + s.greetings());
    s.greetings(function(response) { 
      alert("async:" + response.result) 
    });
}
/* ]]> */
    </script>
</head>
<body>
    <p>This page tests the HelloWorld service with Jayrock.</p>
</body>
可以看到helloworld.ashx?proxy指向了一个JS文件,他的内容就是:
// This JavaScript was automatically generated by
// Jayrock.Json.Rpc.Web.JsonRpcProxyGenerator, Jayrock, Version=0.9.7507.0, Culture=neutral, PublicKeyToken=null
// on 2006年12月5日 at 8:46:27 (中国标准时间)
// Proxy version 1.0
function HelloWorld(url)
{
    /* Returns a summary about the JSON-RPC server implementation for display purposes. */
    
    this["system.about"] = function(callback)
    {
        return call("system.about", [ ], callback);
    }
    
    /* Returns the version JSON-RPC server implementation using the major, minor, build and revision format. */
    
    this["system.version"] = function(callback)
    {
        return call("system.version", [ ], callback);
    }
    
    /* Returns an array of method names implemented by this service. */
    
    this["system.listMethods"] = function(callback)
    {
        return call("system.listMethods", [ ], callback);
    }
    
    this["greetings"] = function(callback)
    {
        return call("greetings", [ ], callback);
    }
    
    var url = typeof(url) === 'string' ? url : 'http://localhost:3409/Web/HelloWorld.ashx';
    var self = this;
    var nextId = 0;
    function call(method, params, callback)
    {
        var request = { id : nextId++, method : method, params : params };
        return callback == null ? 
            callSync(method, request) : callAsync(method, request, callback);
    }
    function callSync(method, request)
    {
        var http = newHTTP();
        http.open('POST', url, false, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.send(JSON.stringify(request));
        if (http.status != 200)
            throw { message : http.status + ' ' + http.statusText, toString : function() { return message; } };
        var response = JSON.eval(http.responseText);
        if (response.error != null) throw response.error;
        return response.result;
    }
    function callAsync(method, request, callback)
    {
        var http = newHTTP();
        http.open('POST', url, true, self.httpUserName, self.httpPassword);
        setupHeaders(http, method);
        http.onreadystatechange = function() { http_onreadystatechange(http, callback); }
        http.send(JSON.stringify(request));
        return request.id;
    }
    function setupHeaders(http, method)
    {
        http.setRequestHeader('Content-Type', 'text/plain; charset=utf-8');
        http.setRequestHeader('X-JSON-RPC', method);
    }
    function http_onreadystatechange(sender, callback)
    {
        if (sender.readyState == /* complete */ 4)
        {
            var response = sender.status == 200 ? 
                JSON.eval(sender.responseText) : {};
            
            response.xmlHTTP = sender;
                
            callback(response);
        }
    }
    function newHTTP()
    {
        return typeof(ActiveXObject) === 'function' ? 
            new ActiveXObject('Microsoft.XMLHTTP') : /* IE 5 */
            new XMLHttpRequest(); /* Safari 1.2, Mozilla 1.0/Firefox, and Netscape 7 */
    }
}
HelloWorld.rpcMethods = ["system.about","system.version","system.listMethods","greetings"]; 
上面JS文档是自动生成的,ASP.NET AJAX也有自动生成客户端访问对象的功能

Jayrock 远程方法要求写在一个ashx中,页面请求这个ashx的时候,在ProcessRequest 中根据Request对象中的参数信息,确定请求的服务器端方法名称和参数,然后进行调用,并返回结果。





本文转自 张善友 51CTO博客,原文链接:http://blog.51cto.com/shanyou/74156,如需转载请自行联系原作者
目录
相关文章
|
11月前
|
编解码 JSON 网络协议
Golang 语言使用标准库 net/rpc/jsonrpc 包跨语言远程调用
Golang 语言使用标准库 net/rpc/jsonrpc 包跨语言远程调用
97 0
|
3月前
|
JSON 开发框架 API
【推荐100个unity插件之20】一个强大的JSON处理库——Newtonsoft.Json(也称为Json.NET)
【推荐100个unity插件之20】一个强大的JSON处理库——Newtonsoft.Json(也称为Json.NET)
192 0
|
3月前
|
JSON 数据格式 微服务
.NET下 支持大小写不敏感的JSON Schema验证方法
有很多应用程序在验证JSON数据的时候用到了JSON Schema。 在微服务架构下,有时候各个微服务由于各种历史原因,它们所生成的数据对JSON Object属性名的大小写规则可能并不统一,它们需要消费的JSON数据的属性名可能需要大小写无关。 遗憾的是,目前的JSON Schema没有这方面的标准,标准中都是大小写敏感的。在类似上述情况下,这给使用JSON Schema进行数据验证造成了困难。
|
11月前
|
安全 Go
Go 使用标准库 net/rpc 包
Go 使用标准库 net/rpc 包
66 0
|
编解码 JSON 负载均衡
rpc的正确打开方式|读懂Go原生net/rpc包
我希望借助这篇文章,用尽可能少的语言,配合分析Go原生net/rpc包的部分核心代码,帮助你贯通RPC的知识,梳理RPC的运作流程,让你对RPC有一个比较全面的认识。
196 0
rpc的正确打开方式|读懂Go原生net/rpc包
|
SQL JSON 开发框架
.NET 中的 Json 使用体验
本文主要总结介绍.NET 中的对 Json 数据使用在使用过程中的关于编码、循环引用、时间格式化的一些问题
168 0
.NET 中的 Json 使用体验
|
存储 JSON JavaScript
.NET MVC第九章、Web Api Json序列化与反序列化
.NET MVC第九章、Web Api Json序列化与反序列化
198 0
.NET MVC第九章、Web Api Json序列化与反序列化
|
JSON 数据格式
【.NET开发福音】使用Visual Studio将JSON格式数据自动转化为对应的类
【.NET开发福音】使用Visual Studio将JSON格式数据自动转化为对应的类
558 0
【.NET开发福音】使用Visual Studio将JSON格式数据自动转化为对应的类
|
JSON 数据格式
将JSON字符串反序列化为指定的.NET对象类型
将JSON字符串反序列化为指定的.NET对象类型
162 0
|
JSON 前端开发 数据格式
.net MVC 使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值
在.net mvc的controller中,方法返回JsonResult,一般我们这么写: [HttpPost] public JsonResult QueryFeature(string url, string whereClause) { string str=""; return Json(str); }   此时如果str过长,就会报“使用 JSON JavaScriptSerializer 进行序列化或反序列化时出错,字符串的长度超过了为 maxJsonLength 属性设置的值”。
2912 0

热门文章

最新文章