我们一般有三种体式格局来运用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),
"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这个类。
如安在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
以上就是ASP.NET经由过程Remoting service上传文件的实例详解的细致内容,更多请关注ki4网别的相干文章!