服务端代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using System.Collections;
using System.Runtime.Serialization.Formatters;
using Microsoft.Office.Interop.Excel;
[Serializable]
public class DemoClass : MarshalByRefObject
{
public void Doit()
{
Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
excelapp.Visible = true;
Workbook book1 = excelapp.Workbooks.Add(Type.Missing);
Worksheet sheet1 = (Worksheet)book1.Sheets[1];
Range range = sheet1.get_Range("A1", Type.Missing);
range.Value2 = "Hello World!";
}
}
class Program
{
static void Main(string[] args)
{
BinaryServerFormatterSinkProvider provider = new BinaryServerFormatterSinkProvider();
provider.TypeFilterLevel = System.Runtime.Serialization.Formatters.TypeFilterLevel.Full;
IDictionary props = new Hashtable();
props["port"] = 1234;
ChannelServices.RegisterChannel(new TcpChannel(props, null, provider), false);
RemotingConfiguration.RegisterWellKnownServiceType(typeof(DemoClass), "DemoClass", WellKnownObjectMode.Singleton);
Console.WriteLine("服务已启动...");
Console.ReadKey();
}
}
客户端代码:
using System.Text;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Channels.Tcp;
using System.Runtime.Remoting;
using Microsoft.Office.Interop.Excel;
[Serializable]
public class DemoClass : MarshalByRefObject
{
public void Doit()
{
}
}
class Program
{
static void Main(string[] args)
{
DemoClass obj = (DemoClass)Activator.GetObject(typeof(DemoClass), "tcp://127.0.0.1:1234/DemoClass");
obj.Doit();
Console.ReadKey();
}
}
代码在本机测试是没问题的,两者都是控制台应用程序,可以实现功能;但放在局域网中测试问题就来了 ,先执行服务端所在电脑,客户端所在电脑运行之后居然在服务端电脑上打开了Excel,这个不是我要的效果撒。我需要实现客户端远程调用服务端的方法,在客户端电脑上打开Excel