`
mlzboy
  • 浏览: 742199 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Remoting双向通信使用RemotingServices.Marshal

阅读更多

最近大家都在讲wcf,我不太感冒,虽然看过,但是还是用的remoting,在使用RemotingServices.Marshal的时候遇到一些问题,于是做了一个demo,这个RemotingServices.Marshal的做法在网上例子中没有看到,故撰写此文,将RemotingServices.Marshal返回的ObjRef对象序列化传递到服务器端,再由服务器端反序列用以访问客户端对象,这个做法的好处是原先我们只能client访问server,现在可以通过动态的发布对象,并将对象传递到服务器端,使得服务器可以访问客户端的对象了,这样可以达到双向通信的目的。不太会写文字,还是以图片和代码说明吧。

客户端代码如下:

 

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using Interface;

namespace Client
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            Stream sw 
= File.OpenRead(@"D:\wwwroot\OICF\MarshalDemo\Server\bin\Debug\xx.bin");
            BinaryFormatter bf
=new BinaryFormatter();
            ITest test
=(ITest)bf.Deserialize(sw);
            test.Say(
"hello world");
        }
    }
}

 

这里没有将序列化后的代码上传到服务器端,我是直接在服务器端读文件了

 

服务器端代码:

 

Code
<!--<br /> <br /> Code highlighting produced by Actipro CodeHighlighter (freeware)<br /> http://www.CodeHighlighter.com/<br /> <br /> -->using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.Remoting;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Windows.Forms;
using Interface;

namespace Client
{
    
public partial class Form1 : Form
    {
        
public Form1()
        {
            InitializeComponent();
        }

        
private void Form1_Load(object sender, EventArgs e)
        {
            Stream sw 
= File.OpenRead(@"D:\wwwroot\OICF\MarshalDemo\Server\bin\Debug\xx.bin");
            BinaryFormatter bf
=new BinaryFormatter();
            ITest test
=(ITest)bf.Deserialize(sw);
            test.Say(
"hello world");
        }
    }
}

 

工程代码地址(vs2008):http://files.cnblogs.com/lexus/MarshalDemo.zip

 

 

分享到:
评论

相关推荐

    remoting传输方式

    这通常通过`Activator.CreateInstance`或`RemotingServices.Marshal`方法完成。 客户端则通过通道来获取远程对象的引用。这通常涉及构造一个适当的URI,该URI指向服务端的对象,并使用`Activator.GetObject`方法...

    c#.net remoting 实例 范例

    - **创建Remoting服务器**:首先,定义一个接口和实现该接口的类,然后通过`[Serializable]`和`[RemotingServices.Marshal]`特性将该类暴露为远程对象。 - **创建Remoting客户端**:在客户端,使用`Activator....

    C# remoting接口实现简单示例

    RemotingServices.Marshal(remoteObject, objectUri); Console.WriteLine("服务器已启动,等待连接..."); Console.ReadLine(); ``` 在客户端,我们可以创建一个透明代理,通过代理调用服务器端的方法: ```csharp...

    Deflexion游戏开发(四):网络对战基于.Net Remoting

    - 使用`Activator.GetObject`或`RemotingServices.Marshal`方法将游戏管理器对象暴露给远程调用。 2. **配置客户端**: - 在客户端,我们需要知道服务器端的URL和对象的类型信息,以便通过`Activator.GetObject`...

    Remorting服务C#示例

    1. **设置远程对象**: 在服务端,我们需要定义一个公共类,并将其标记为`[Serializable]`以允许序列化,同时使用`[RemotingServices.Marshal]`或`[MarshalByRefObject]`特性来标记这个类,表示它可以作为远程对象...

    ASP.NET通过Remoting service上传文件

    例如,使用`ChannelServices.RegisterChannel(new TcpChannel(1500))`创建TCP通道,然后使用`RemotingServices.Marshal(Trans, "TestService")`将`cTransfer`对象映射到服务。客户端通过`Activator.GetObject`获取该...

Global site tag (gtag.js) - Google Analytics