教程:简单的WCF XML-RPC客户端 [英] Tutorial: Simple WCF XML-RPC client
问题描述
更新:我在下面的答案中提供了完整的代码示例.
我已经构建了自己的小型自定义XML-RPC服务器,并且由于我想保持简单,因此无论是在服务器端还是客户端上,我要完成的工作都是创建一个尽可能简单的客户端(最好使用C#) )使用WCF.
I have built my own little custom XML-RPC server, and since I'd like to keep things simple, on both server and client side, what I would like to accomplish is to create a simplest possible client (in C# preferably) using WCF.
让我们说通过XML-RPC公开的服务合同如下:
Let's say that Contract for service exposed via XML-RPC is as follows:
[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公开.
So, there are two example methods, one without any arguments, and another with simple string argument, both returning strings (just for sake of example). Service is exposed via http.
Aaand,接下来要做什么? :)
Aaand, what's next? :)
推荐答案
受杜比的回答启发,我查找了有关该主题的更多信息(示例),并得出以下发现.
Inspired by Doobi's answer, I looked up some more info (examples) on the subject, and came up with the following findings.
创建简单的WCF XML-RPC客户端的步骤:
Steps to create simple WCF XML-RPC client:
- 从此页面下载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的引用
- Download XML-RPC for WCF from this page: http://vasters.com/clemensv/PermaLink,guid,679ca50b-c907-4831-81c4-369ef7b85839.aspx (download link is at the top of page)
- Create an empty project which targets .NET 4.0 Full framework (or else System.ServiceModel.Web won't be available later on)
- Add Microsoft.Samples.XmlRpc project from the archive to your project
- Add reference to Microsoft.Samples.XmlRpc project
- Add references to System.ServiceModel and 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>
这篇关于教程:简单的WCF XML-RPC客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!