2009-07-07 41 views
5

Giả sử tôi có dịch vụ XML-RPC đơn giản được thực hiện với Python:Làm thế nào để giao tiếp giữa Python và C# bằng cách sử dụng XML-RPC?

from SimpleXMLRPCServer import SimpleXMLRPCServer 

    def getTest(): 
     return 'test message' 

    if __name__ == '__main__' : 
     server = SimpleThreadedXMLRPCServer(('localhost', 8888)) 
     server.register_fuction(getText) 
     server.serve_forever() 

bất cứ ai có thể cho tôi biết làm thế nào để gọi getTest) chức năng (từ C#?

Trả lời

2

Để gọi phương thức getTest từ C#, bạn sẽ cần một thư viện máy khách XML-RPC. XML-RPC là một ví dụ về thư viện như vậy.

3

Không thổi còi còi của riêng tôi, nhưng: 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()); 
    } 
} 

Điều đó sẽ làm các trick ... Lưu ý, các thư viện trên là LGPL, mà có thể hoặc không có thể đủ tốt cho bạn.

3

Cảm ơn bạn đã trả lời, tôi thử thư viện xml-rpc từ liên kết darin. Tôi có thể gọi hàm getTest bằng mã sau

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); 
      } 
     } 
    } 
Các vấn đề liên quan