通过Remoting service上传文件

分类: asp.net   出处:iocblog整理  更新时间:2008-06-22   添加到收藏  

最近在因为在学习remoting,纯粹只是了解一下,发现remoting确实是好东西。

我们通常有三种方式来使用remoting,一种是

第一种:publishing a public object
公开的对象创建在本地
第二种:remote creation of a public object (sao)
对象创建在客户端请求中
第三种:remote creation of a private object (cao)
对象创建在host上,客户端引用服务器上的对象

目次我也没有很好理解这三种的本质区别在哪里。而这三种方式的remoting创建方式也不相同。

第一种方式
host:
channelservices.registerchannel (new tcpchannel(1500));
ctransfer trans = new ctransfer();
remotingservices.marshal (trans, "testservice");client:
ctransfer t = (ctransfer) activator.getobject(typeof(ctransfer), (来源www.iocblog.net)
                                 "tcp://host:1500/testservice");
第二种方式
host:
channelservices.registerchannel (new tcpchannel(1500));
remotingconfiguration.registerwellknownservicetype(typeof(ctransfer),
    "testservice", wellknownobjectmode.singleton);client:
ctransfer t = (ctransfer) activator.getobject(typeof(ctransfer),
                                 "tcp://host:1500/testservice");
第三种方式
host:
channelservices.registerchannel (new tcpchannel(1500));
remotingconfiguration.registeractivatedservicetype(typeof(ctransfer));client:
object[] attr = {new urlattribute("tcp://host:1500")};
object[] args = {"sample constructor argument"};
ctransfer t = (ctransfer) activator.createinstance(typeof(ctransfer), args, attr);
如果我们需要一个对象(object)允许远程调用处理,那么这个对象(object)需要继承于marshalbyrefobject这个类。(来源www.iocblog.net)

如何在remoting中传送文件呢?基本思路就是在client打开client的文件,转换在byte[]类型之后调用host的对象。
client与host之间传送的对象
[serializable]
    public struct kaction
    {
        public string filename;
        public byte[] context;       
       
    };打开文件,将流字节保存到context中去
stream  filestream=file.open(this.transfilename.text,filemode.open);
            filestream.position=0;
            byte[] content = new byte[((int) filestream.length) + 1];
            filestream.read(content,0,content.length) ;
在host在读取到kaction之后,把它保存到指定文件夹下面
memorystream meoerystream=new memorystream(k_action.context);
            filestream filestream=new filestream(@"d:"+k_action.filename,filemode.create);
            meoerystream.writeto(filestream);
            filestream.close();
            meoerystream.close();           
发现不能在对象中又定义新的对象。在准备发送到host上会提示“包含潜在危险的类型”。
[serializable]
    public struct kaction
    {
        public string filename;
        public byte[] context;
                public fineinfo fileinfo;//这里

    };
记录一下自己的心得。有空我会好好整理下下回做篇完整点的。

cnzc's blogs


Tag: 上传



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。