mr.*_*r.b 13 c# wcf client 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
}
Run Code Online (Sandbox Code Playgroud)
因此,有两个示例方法,一个没有任何参数,另一个带有简单的字符串参数,两个都返回字符串(仅为了示例).服务通过http公开.
Aaand,下一步是什么?:)
mr.*_*r.b 13
在Doobi的回答的启发下,我查阅了有关该主题的更多信息(示例),并得出以下结论.
创建简单WCF XML-RPC客户端的步骤:
示例代码
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");
}
}
}
Run Code Online (Sandbox Code Playgroud)
示例请求/响应消息
请求XML
<?xml version="1.0" encoding="utf-8"?>
<methodCall>
<methodName>Hello</methodName>
<params>
<param>
<value>
<string>World</string>
</value>
</param>
</params>
</methodCall>
Run Code Online (Sandbox Code Playgroud)
响应XML
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<params>
<param>
<value>
<string>Hello, World!</string>
</value>
</param>
</params>
</methodResponse>
Run Code Online (Sandbox Code Playgroud)
最简单的方法是使用WCF channelfactory
IStuffService client = new ChannelFactory<IStuffService>(
new BasicHttpBinding(),
*"Stick service URL here"*)
.CreateChannel();
Run Code Online (Sandbox Code Playgroud)
并通过简单地调用来执行请求
var response = client.YourOperation(params)
Run Code Online (Sandbox Code Playgroud)
更多详细信息:http: //msdn.microsoft.com/en-us/library/ms734681.aspx
编辑:编辑;)
| 归档时间: |
|
| 查看次数: |
29649 次 |
| 最近记录: |