更新:我在下面的回答中提供了完整的代码示例。
我已经构建了自己的小型自定义 XML-RPC 服务器,并且由于我希望在服务器端和客户端都保持简单,所以我想要完成的是创建一个尽可能简单的客户端(最好使用 C# ) 使用 WCF。
假设通过 XML-RPC 公开的服务契约如下:
[ServiceContract]
public interface IContract
{
[OperationContract(Action="Ping")]
string Ping(); // server returns back string "Pong"
[OperationContract(Action="Echo")]
string Echo(string message); // server echoes back whatever message is
}
所以,有两个示例方法,一个没有任何参数,另一个有简单的字符串参数,都返回字符串(仅作为示例)。服务通过 http 公开。
Aaand,下一步是什么? :)
最佳答案
受 Doobi 回答的启发,我查阅了有关该主题的更多信息(示例),并得出以下发现。
创建简单 WCF XML-RPC 客户端的步骤:
- 从此页面下载用于 WCF 的 XML-RPC:http://vasters.com/clemensv/PermaLink,guid,679ca50b-c907-4831-81c4-369ef7b85839.aspx (下载链接在页面顶部)
- 创建一个针对 .NET 4.0 Full 框架的空项目(否则 System.ServiceModel.Web 稍后将不可用)
- 将存档中的Microsoft.Samples.XmlRpc 项目添加到您的项目
- 添加对 Microsoft.Samples.XmlRpc 项目的引用
- 添加对 System.ServiceModel 和 System.ServiceModel.Web 的引用
示例代码
using System;
using System.ServiceModel;
using Microsoft.Samples.XmlRpc;
namespace ConsoleApplication1
{
// describe your service's interface here
[ServiceContract]
public interface IServiceContract
{
[OperationContract(Action="Hello")]
string Hello(string name);
}
class Program
{
static void Main(string[] args)
{
ChannelFactory<IServiceContract> cf = new ChannelFactory<IServiceContract>(
new WebHttpBinding(), "http://www.example.com/xmlrpc");
cf.Endpoint.Behaviors.Add(new XmlRpcEndpointBehavior());
IServiceContract client = cf.CreateChannel();
// you can now call methods from your remote service
string answer = client.Hello("World");
}
}
}
示例请求/响应消息
请求 XML
<?xml version="1.0" encoding="utf-8"?>
<methodCall>
<methodName>Hello</methodName>
<params>
<param>
<value>
<string>World</string>
</value>
</param>
</params>
</methodCall>
响应 XML
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<params>
<param>
<value>
<string>Hello, World!</string>
</value>
</param>
</params>
</methodResponse>
关于c# - 教程 : Simple WCF XML-RPC client,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2878447/