首頁  >  文章  >  後端開發  >  ASP.NET透過Remoting service上傳檔案的實例詳解

ASP.NET透過Remoting service上傳檔案的實例詳解

巴扎黑
巴扎黑原創
2017-05-21 18:34:081198瀏覽

最近在因為在學習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),
                 #第二種方式
Host:
ChannelServices.RegisterChannel (new TcpChannel(1500));
RemotingConfiguration.RegisterWellKnownServiceType(typeof(cTransfer),
    "TestService", WellKnownObjectMode.Singleton);Clferient:# (Transferient ) Activator.GetObject(typeof(cTransfer),
                           Channel (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這個類別。

如何在remoting中傳送檔案呢?基本想法就是在client開啟client的文件,轉換在Byte[]型別之後呼叫host的物件。
Client與Host傳送的物件
[Serializable]
    public struct kAction

    {

        public string filename;  } ;開啟文件,將流位元組儲存到Context中去
Stream  fileStream=File.Open(this.transFileName.Text,FileMode.Open);
            fileStream. = new byte[((int) fileStream.Length) + 1];
            fileStream.Read(Content,0,Content.Length) ;
在Host在讀取到Kaction之後,將它儲存到指定檔案在Stream) ;
            fileStream.Close();
            meoeryStream.Close(   在準備發送到HOST上會提示「包含潛在危險的類型」。
[Serializable]
    public struct kAction
    {
        public string filename;
      ineInfo fileInfo;//這裡

    };
記錄一下自己的心得。有空我會好好整理下回做篇完整點的。

cnzc's blogs

以上是ASP.NET透過Remoting service上傳檔案的實例詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn