ASP.NET uploads files via Remoting service

  • 2020-05-05 11:05:01
  • OfStack

Recently, because I was studying Remoting, I just learned about it and found that Remoting is really a good thing.

There are three ways to use remoting and

The first: Publishing a public object
The exposed objects are created locally in
Second: Remote creation of a public object (SAO)
The object is created in
in the client request Third: Remote creation of a private object (CAO)
The object is created on HOST, and the client references the object

on the server

I don't quite understand the essential differences between the three. The three methods of remoting creation are also different.

The first way is
Host:
ChannelServices.RegisterChannel (new TcpChannel(1500));
cTransfer Trans = new cTransfer();
RemotingServices.Marshal (Trans, "TestService");Client:
cTransfer T = (cTransfer) Activator.GetObject(typeof(cTransfer),
                                                                "tcp://host:1500/TestService");
The second way is
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");
The third way is
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);
If we need an object (object) to allow remote call processing, then this object (object) needs to inherit from the MarshalByRefObject class.

How do I transfer files in remoting? The basic idea is to open the client file in client and convert the host object after Byte[].

objects passed between Client and Host [Serializable]
      public struct kAction
      {
              public string filename;
              public byte[] context;              

      & # 125;; Open the file and save the stream bytes to Context to
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) ;
After Host has read Kaction, save it to
under the specified folder MemoryStream meoeryStream=new MemoryStream(k_Action.context);
                      FileStream fileStream=new FileStream(@"d:\"+k_Action.filename,FileMode.Create);
                      meoeryStream.WriteTo(fileStream);
                      fileStream.Close();
                      meoeryStream.Close();                      
Found that you cannot define a new object in an object. "Contains potentially dangerous types" will be prompted when preparing to send to HOST.
[Serializable]
      public struct kAction
      {
              public string filename;
              public byte[] context;
              / /

here

      };
Take notes. When I am free, I will make a good arrangement and complete the article the next time.

cnzc's blogs


Related articles: