7

假设我有一个用 Python 实现的简单 XML-RPC 服务:

from SimpleXMLRPCServer import SimpleXMLRPCServer  # Python 2

def getTest():
    return 'test message'

if __name__ == '__main__' :
    server = SimpleXMLRPCServer(('localhost', 8888))
    server.register_function(getTest)
    server.serve_forever()

谁能告诉我如何getTest()从 C# 调用该函数?

4

3 回答 3

3

感谢您的回答,我尝试了来自 darin 链接的 xml-rpc 库。我可以使用以下代码调用 getTest 函数

using CookComputing.XmlRpc;
...

    namespace Hello
    {
        /* proxy interface */
        [XmlRpcUrl("http://localhost:8888")]
        public interface IStateName : IXmlRpcProxy
        {
            [XmlRpcMethod("getTest")]
            string getTest();
        }

        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                /* implement section */
                IStateName proxy = (IStateName)XmlRpcProxyGen.Create(typeof(IStateName));
                string message = proxy.getTest();
                MessageBox.Show(message);
            }
        }
    }
于 2009-07-07T08:19:15.917 回答
3

不要自吹自擂,而是: http: //liboxide.svn.sourceforge.net/viewvc/liboxide/trunk/Oxide.Net/Rpc/

class XmlRpcTest : XmlRpcClient
{
    private static Uri remoteHost = new Uri("http://localhost:8888/");

    [RpcCall]
    public string GetTest()
    {
        return (string)DoRequest(remoteHost, 
            CreateRequest("getTest", null));
    }
}

static class Program
{
    static void Main(string[] args)
    {
        XmlRpcTest test = new XmlRpcTest();
        Console.WriteLine(test.GetTest());
    }
}

这应该可以解决问题...注意,上面的库是 LGPL,它可能对你来说不够好,也可能不够好。

于 2009-07-07T07:31:21.397 回答
2

为了从 c# 调用 getTest 方法,您需要一个 XML-RPC 客户端库。XML-RPC就是这种库的一个例子。

于 2009-07-07T07:30:34.143 回答