服务端代码:
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
你想让客户端打开excel 那不是应该再客户端里建立一个domoclass的实例化对象来调用doit吗
你直接remonting服务端的对象,自然是在服务端进行操作
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()
{
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)
{
DemoClass obj = (DemoClass)Activator.GetObject(typeof(DemoClass), "tcp://127.0.0.1:1234/DemoClass");
DemoClass obj2=new DemoClass();
obj2.doit();
Console.ReadKey();
}
}
这样本就是本地打开了吗
“这个不是我要的效果撒。我需要实现客户端远程调用服务端的方法,在客户端电脑上打开Excel”,这个逻辑很奇怪,为什么客户端要调用服务端方法来打开客户端的excel呢?如果客户端想打开excel,把doit方法直接移到客户端执行就好了。暂时没看出来服务端还有什么存在的必要性。
这是属于远程调用方面的东西吧?
远程调用的作用主要是:
如果是EXCEL内容加密,不想让客户端获得解读方法,那也应该将文件内容传送到服务端,解码之后,返回明文内容,而不是直接调用打开文件的方法。你这样直接调用,打开的当然是服务端本地的文件了。
你所说的是安全问题,你可以把算法放到服务端,控件,比如Excel,客户端直接调用即可。你这设计不科学
需求看着感觉怪怪的, 服务端唤起本地客户端打开文件
可以考虑服务端唤起本地客户端然后打开excel文件