教程:简单的WCF XML-RPC客户端
更新:我在下面的回答中提供了完整的代码示例。
我已经构建了自己的小型自定义XML-RPC服务器,因为我想在服务器端和客户端都保持简单,我想要完成的是使用WCF创建一个最简单的客户端(最好是C#) 。
假设通过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 cf = new ChannelFactory ( 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
Hello World
响应XML
Hello, World!
最简单的方法是使用WCF channelfactory
IStuffService client = new ChannelFactory( new BasicHttpBinding(), *"Stick service URL here"*) .CreateChannel();
并通过简单地调用来执行请求
var response = client.YourOperation(params)
更多细节: http : //msdn.microsoft.com/en-us/library/ms734681.aspx
编辑:编辑;)