English 中文(简体)
远程执行Action<>或Func<>对象
原标题:Remote executing of Action<> or Func<> objects

我想使用WCF将Action或Func对象从C#客户端传输(并执行)到C#服务器应用程序。

这是我的代码:

[ServiceContract]
interface IRemoteExecuteServer
{
    [OperationContract]
    void Execute(Action action);
}

class RemoteExecuteServer : IRemoteExecuteServer
{
    public void Execute(Action action)
    {
        action();
    }
}

服务器代码:

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();
        }
    }
}

客户端代码:

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!");
        });
    }
}

When executing the line "server.Execute" I get a CommunicationException. Does anyone know how to fix this error?

谢谢你的帮助!

问题回答

我会想到两种解决方案,它们本质上都非常疯狂。主要是因为您向服务器发送代码以供执行的请求并不是人们每天都要做的事情(我认为从来没有人做过这样的事情)。

  1. 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.

  2. 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.

但是您仍然无法从任何函数中提取代码。请记住,Action只不过是程序集中硬编码函数的委托(引用)。

我希望能够使用lambda表达式来指定要通过wcf服务返回的值的范围

是我问的一个类似的问题。

我也把它与你联系起来,因为埃里克·利珀特展示了这样一个解决方案是如何可能的。

然而,这是一项相当可观的工作。

可能有现有的解决方案,我不知道。(有一些非常漂亮的运行时字节码注入/操作工具可用,所以…)

但是,如果有足够的权限(和信任级别!这是一个很大的权限),人们可以实时编译和/或通过网络发送各种自我评估表达式(这与发送字节码本身不同,尽管理论上如果可以提取字节码或等效代码,就可以这样做)。

上述方法不会发送Action<>;中;,这将需要字节码提取来进行序列化(包括捕获上下文,如果有的话),但类似于即时编译C#您可以关注它进行有趣的讨论,并最终找到代码DOM

另一方面,LINQ通过使用表达式树。请注意,它不发送字节码,而是使用此方法允许远程执行表达式树(常见的方法是SQL查询)。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签