Asp. NET example of uploading files via remoting service

Source: Internet
Author: User
Recently in because of learning remoting, purely just to understand, found that remoting is really good things.

We usually have three ways to use remoting, one is

First type: Publishing a public object
Exposed objects are created on-premises
Second type: Remote creation of a public object (SAO)
Object creation in the client request
Third type: Remote creation of a private object (CAO)
object is created on the host, the client references the object on the server

I do not have a good understanding of the three kinds of essential differences in the contents. And these three ways of remoting are created in different ways.

The first way
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
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
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 to allow remote invocation processing, then this object needs to inherit from the MarshalByRefObject class.

How do I transfer files in remoting? The basic idea is to call the host object after the client opens the client's file and converts it to the byte[] type.
Objects that are transferred between the client and host
[Serializable]
public struct kaction
{
public string filename;
Public byte[] context;

Open the file and save the stream bytes to the 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);
After the host has read the kaction, save it to 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 ();
Discovery cannot define a new object in the object. You will be prompted to "include potentially dangerous types" on the ready to send to host.
[Serializable]
public struct kaction
{
public string filename;
Public byte[] context;
Public Fineinfo fileinfo;//Here

};
Keep a record of your experiences. I will tidy up next time to do a full point.

Cnzc ' s Blogs

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.