现在的位置: 首页 > 综合 > 正文

【开源系列】三国演义LBS (十一)源码:远程调用框架

2013年02月25日 ⁄ 综合 ⁄ 共 4599字 ⁄ 字号 评论关闭

前言

-----------------------------------------------

相关讨论组入口: http://www.pixysoft.net/ (点击进入)

 

功能简介:

-----------------------------------------------     

Pixysoft.Framework.Remoting 

基于Http协议,对服务器代码实现远程调用。

是Web2.0 项目的基础。

 

快速入门  

-----------------------------------------------      

远程调用,最重要的是权限,因此Remoting框架基于了Pixysoft.Framework.Security框架。使用了其中的Token机制(令牌机制)。当然这个Security还包含了RBAC, SSO等巨星功能,本文就不详细说明了。

 

首先是部署Security。

        public void deploy_security()
        {
            
//使用默认的用户名、密码 

            //此默认在Security框架内的TokenHelper有描述

            
string username = "helloworld";
            
string password = "helloworld";
            
string md5 = Pixysoft.Security.MD5.GetMD5(password);
            Console.WriteLine(md5);

            //部署权限框架

            TokenSecurityManager.Instance.Restart();
            
string token = TokenSecurityManager.Instance.Login(username, md5);
            Console.WriteLine(token);
            Console.WriteLine(TokenSecurityManager.Instance.Register(token, 
"test", Pixysoft.Security.MD5.GetMD5("test")));

            //把生成的helloworld.db拷贝到网站的目录下

        } 

Security框架初次启动的时候,内置了默认的用户名密码 helloworld/helloworld。通过以上代码,能够在运行目录下生成一个helloworld.db的数据库,就是一个SQLite数据库。

  

然后嘛,就是把这个helloworld.db复制到网站目录下(Pixysoft.Framework.Remoting.Demo)。

 

第三步,就是配置网站的配置文件web.Config,添加一个httphandler

        <httpHandlers>
            
<add verb="GET, POST" path="remoting.asmx" type="Pixysoft.Framework.Remoting.RemoteHandler, Pixysoft.Framework.Remoting" validate="false"/>

        </httpHandlers> 

 

第四步,就是测试一下链接是否正常,在VS2005中,打开这个网站,得到测试URL是: 

http://localhost:4892/Pixysoft.Framework.Remoting.Demo/ 

这个时候,就可以访问remoting了,如下:

http://localhost:4892/Pixysoft.Framework.Remoting.Demo/remoting.asmx 

 

可以看到返回是:

无法显示 XML 页。 
使用 样式表无法查看 XML 输入。请更正错误然后单击 刷新按钮,或以后重试。 

--------------------------------------------------------------------------------

文档的顶层无效。处理资源 'http://localhost:4892/Pixysoft.Framework.Remoting.Demo/remoting.asmx' 时出错。第 1 行,位置: 1 

copyright 2010 by pixysoft.net

这样,表示部署一切正常。

 

 

接下来,我们测试Remoting框架自带的测试代码,实现代码和接口分别是:

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Pixysoft.Framework.Remoting.Core
{
    
class RemotingHelloworld : IRemotingHelloworld
    {
        
public IRemotingValue HelloWorld(DateTime para1, string para2, int para3, IRemotingValue para4)
        {
            IRemotingValue value 
= Pixysoft.Tools.PojoHelper.GetPojo<IRemotingValue>();
            value.DateTimeValue 
= para1;
            value.StringValue 
= para2;
            value.IntValue 
= para3;
            value.Value 
= para4.Value;
            
return value;
        }

        public byte[] PostBinary(byte[] binary)
        {
            
return binary;
        }
    }
}

namespace Pixysoft.Framework.Remoting
{
    [Remote(
"Pixysoft.Framework.Remoting""Pixysoft.Framework.Remoting.Core.RemotingHelloworld")]
    
public interface IRemotingHelloworld
    {
        IRemotingValue HelloWorld(DateTime para1, 
string para2, int para3, IRemotingValue para4);

        byte[] PostBinary(byte[] binary);
    }

    public interface IRemotingValue
    {
        
string Value { get;set;}

        DateTime DateTimeValue { get;set;}

        string StringValue { get;set;}

        int IntValue { get;set;}
    }

} 

 

代码很简单,我们本地仅仅调用IRemotingHelloworld这个接口,然后框架就帮助我们远程激活了RemotingHelloworld代码,并计算出返回值。当然这个映射关系就在接口的标签上:

[Remote("Pixysoft.Framework.Remoting""Pixysoft.Framework.Remoting.Core.RemotingHelloworld")] 

这句话就是关键,告诉了框架,这个接口需要激活什么代码。 

 

最后,我们就进行远程调用了。

        public void remotecall()
        {
            
//启动Remoting.Demo 网站,获取url

            
string url = "http://localhost:4892/Pixysoft.Framework.Remoting.Demo/remoting.asmx";

            IRemoteChannel<IRemotingHelloworld> remoting = RemotingManager.CreateRemoteChannel<IRemotingHelloworld>(url);

            remoting.Login("helloworld""helloworld");

            IRemotingValue value = Pixysoft.Tools.PojoHelper.GetPojo<IRemotingValue>();

            IRemotingValue response = remoting.RemoteProxy.HelloWorld(DateTime.Now, "hello"3, value);

            Console.WriteLine(response.DateTimeValue);
            Console.WriteLine(response.StringValue);
            Console.WriteLine(response.IntValue);
            Console.WriteLine(response.Value);

        } 

 

输出结果就是:

------ Test started: Assembly: Pixysoft.Framework.Remoting.dll ------

2011-5-26 下午 10:13:18
hello
3

1 passed, 0 failed, 0 skipped, took 21.72 seconds (Ad hoc).

 

 

下期预告:

-----------------------------------------------     

Pixysoft.Framework.Json 真正进入Web2.0开发吧!

是web 2.0 快速开发的最核心框架,结合了JQuery/Ajax等技术!!! 

 

附件下载

-----------------------------------------------    

Pixysoft.Framework.Remoting 打包下载:  

http://qun.qq.com/air/#95755843/share  

抱歉!评论已关闭.