如何在Remoting中使用singleton和singleCall

时间:2013-08-06 09:13:36

标签: c# winforms remoting

我创建了一个Remoting应用程序。 Librery类中的代码是

 public class InitialClass1:MarshalByRefObject
{
    int i=0;
    public InitialClass1()
    {

    }
    public string getInitial(string nam)
    {                
        i++;
        if (nam.ToLower() == "naresh") return i.ToString()+").Jadapalli";
        else if (nam.ToLower() == "balu") return i.ToString() + "Gonugunta";
        else if (nam.ToLower() == "murali") return i.ToString() + "Vempuluri";
        else if (nam.ToLower() == "chandra sekhar") return i.ToString() + "Ponnam";
        else if (nam.ToLower() == "aneev") return i.ToString() + "Katti";
        else if (nam.ToLower() == "rajini") return i.ToString() + "Karlapudi";
        else
            return i.ToString() + "No results";

    }
}

控制台应用程序中的代码是

class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        TcpChannel tcp = new TcpChannel(1234);
        ChannelServices.RegisterChannel(tcp,false);
        string s = ConfigurationManager.AppSettings["remote"];
        RemotingConfiguration.RegisterWellKnownServiceType(typeof(InitialClass1),s,WellKnownObjectMode.SingleCall);
        Console.WriteLine("Remoting starting...");
        Console.ReadLine();            
    }
}

Windows应用程序中的代码

 private void button1_Click(object sender, EventArgs e)
    {
        label3.Text = Icls.getInitial(textBox1.Text);
    }

    private void Form1_Load(object sender, EventArgs e)
    {

         string url = ConfigurationManager.AppSettings["remote"];
        TcpChannel chan = new TcpChannel();
        ChannelServices.RegisterChannel(chan, false);

        RemotingConfiguration.ApplicationName = "TestRemoting";
        RemotingConfiguration.RegisterActivatedClientType(typeof(InitialClass1), url);

        Icls = new InitialClass1();

    }

它显示例外为Requested Service not found

App.Config文件中的

代码是

<configuration>
  <appSettings>
    <add key="remote" value="tcp://localhost:1234/TestRemoting"/>
  </appSettings>
</configuration>

2 个答案:

答案 0 :(得分:0)

你应该尝试使用button1_click方法初始化。因为您希望在按下按钮时初始化它。

答案 1 :(得分:0)

我的错误在于服务器对象是控制台应用程序

TcpChannel tcp = new TcpChannel(1234);
    ChannelServices.RegisterChannel(tcp,false);
    string s = ConfigurationManager.AppSettings["remote"];
    RemotingConfiguration.RegisterWellKnownServiceType(typeof(InitialClass1),s,WellKnownObjectMode.SingleCall);
    Console.WriteLine("Remoting starting...");
    Console.ReadLine(); 

在注册时看到我正在传递s值(即来自app.config文件tcp:// localhost:1234 / TestRemoting的整个url)。那就是问题所在。实际上我们必须根据您的意愿在服务器对象中提供所需的名称,但同样的名称应该只在app.config文件中。

我的网址是tcp://localhost:1234/TestRemoting。在这种情况下,我们需要在服务器对象中将TestRemoting作为

RemotingConfiguration.RegisterWellKnownServiceType(typeof(InitialClass1),"TestRemoting",WellKnownObjectMode.SingleCall);

这只是我的问题。现在我的申请运作良好..