Action<>的远程执行或 Func<>物体
我想使用 WCF 将 Action 或 Func 对象从 C# 客户端传输(并执行)到 C# 服务器应用程序。
这是我的代码:
[ServiceContract]
interface IRemoteExecuteServer
{
[OperationContract]
void Execute(Action action);
}
class RemoteExecuteServer : IRemoteExecuteServer
{
public void Execute(Action action)
{
action();
}
}
Servercode:
class Program
{
static void Main(string[] args)
{
using (ServiceHost host = new ServiceHost(typeof(RemoteExecuteServer), new Uri("net.tcp://localhost:8000")))
{
host.AddServiceEndpoint(typeof(IRemoteExecuteServer), new NetTcpBinding(), "RES");
host.Open();
Console.WriteLine("Server is running!");
Console.WriteLine("Press any key to exit...");
Console.ReadKey(true);
host.Close();
}
}
}
Clientcode:
class Program
{
static void Main(string[] args)
{
IRemoteExecuteServer server = new ChannelFactory<IRemoteExecuteServer>(new NetTcpBinding(), new EndpointAddress("net.tcp://localhost:8000/RES")).CreateChannel();
server.Execute(delegate()
{
Console.WriteLine("Hello server!");
});
}
}
当执行“server.Execute”行时,我得到一个 CommunicationException。 有谁知道如何修复这个错误?
感谢您的帮助!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会想到两种解决方案,它们本质上都非常疯狂。主要是因为您发送代码到服务器执行的请求并不是人们每天都会做的事情(我认为没有人做过类似的事情)。
DLL解决方案:
将代码编译到单独的 DLL 中。将此 DLL 作为流发送到服务器。使用服务器上的反射从此 DLL 加载一些带有接口的类。然后就可以在创建的类中运行代码了。
代码解决方案:
基本上与第一个相同,但您只需将代码作为字符串发送,然后使用编程 C# 编译器来编译并运行该代码,而不是发送预编译的 DLL。
但您仍然无法从任何函数中提取代码。请记住,操作只不过是程序集中硬编码函数的委托(引用)。
I would think of two solutions, both being pretty crazy in their nature. Mainly because your request of sending code to server for execution is nothing people do every day (and I think noone ever did something like that).
DLL solution:
Compile your code into separate DLL. Send this DLL as stream to server. Load some class with interface using reflection on server from this DLL. Then you can run code in created class.
Code solution:
Basicaly same as first one, but instead of sending precompiled DLL, you just send your code as string and then use programatic C# compiler to compile and run that code.
But you still cant extract your code from any function. And remember Action is nothing more than delegate (reference) for hard-coded function in your assembly.
我希望能够使用 lambda 表达式来指定通过 wcf 服务返回的值范围
是我提出的类似问题。
我也将您链接到它,因为埃里克·利珀特展示了如何实现这样的解决方案。
然而,这是一项相当大的工作量。
I want to be able to use a lambda expression to specify a range of values to return over a wcf service
is a similar question i asked.
I link you too it as eric lippert showed how such a solution would be possible.
However its a decent amount of work.
可能有现有的解决方案,我不知道。 (有一些非常漂亮的运行时字节码注入/操作工具可用,所以......)
但是如果有足够的权限(和信任级别!——这是一个很大的权限),人们可以即时编译并/或通过网络发送各种自评估表达式(这与发送字节码本身不同,尽管如果可以提取字节码或等效内容,理论上可以完成)。
上述方法不会发送 Action<...>,这需要字节码提取以进行序列化(包括捕获上下文,如果有的话),而是发送类似于 即时编译 C# 您可以关注其中的有趣讨论最后找到CodeDOM。
另一方面,LINQ 通过使用 表达式树。请注意,它不发送字节码,而是使用此方法来允许远程执行表达式树(常见的是 SQL 查询)。
There may be existing solutions out there, I know not. (There are some pretty nifty run-time byte-code injection/manipulation tools available, so...)
But given sufficient permissions (and level of trust! -- that's a big one) one can compile-on-the-fly and/or send various self-evaluation expressions across the network (this is different than sending byte-code, per-se, although that could theoretically be done if the byte-code or an equivalent can be extracted).
The approaches above wouldn't be sending an Action<...>, which would require byte-code extraction for serialization (including capturing the context, if any), but rather something like Compiling C# on the Fly which you can follow to an interesting discussion and finally find CodeDOM.
On the other hand, LINQ "gets about" the issue of byte-code extraction/de-compilation with the use of Expression trees. Note that it doesn't send over byte-code, but rather uses this method to allow remote execution of expression trees (a common one being an SQL query).