ASP. NET uploads files through Remoting service

Source: Internet
Author: User

I recently learned about Remoting and found that Remoting is really a good thing.

We usually have three methods to use remoting, one is

First: Publishing a public object
The public object is created locally.
Type 2: Remote creation of a public object (SAO)
Object created in client request
Third: Remote creation of a private object (CAO)
The object is created on the HOST, and the client references the object on the server.

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

Method 1
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 ");
Method 2
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 ");
Method 3
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 call and processing, this object must inherit from the class albyrefobject.

How to transfer files in remoting? The basic idea is to open the client file in the client and convert the object that calls the host after the Byte [] type.
Objects transmitted between Client and Host
[Serializable]
Public struct kAction
{
Public string filename;
Public byte [] context;

}; Open the file and save the stream byte 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 reading the Kaction, the Host saves 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 ();
New objects cannot be defined in the object. The system prompts "containing potentially dangerous types" when preparing to be sent to the HOST ".
[Serializable]
Public struct kAction
{
Public string filename;
Public byte [] context;
Public FineInfo fileInfo; // here

};
Record your experiences. If you have time, I will sort it out and write a complete article.

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.