教程:简单的 WCF XML-RPC 客户端

发布于 2024-09-02 07:19:41 字数 557 浏览 7 评论 0原文

更新:我在下面的答案中提供了完整的代码示例。

我已经构建了自己的小型自定义 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,下一步是什么? :)

Update: I have provided complete code example in answer below.

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.

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
}

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, what's next? :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长途伴 2024-09-09 07:19:41

受到 Doobi 回答的启发,我查找了有关该主题的更多信息(示例),并得出了以下发现。

创建简单 WCF XML-RPC 客户端的步骤:

  1. 从此页面下载适用于 WCF 的 XML-RPC: http://vasters.com/clemensv/PermaLink,guid,679ca50b-c907-4831-81c4-369ef7​​b85839.aspx(下载链接位于页面顶部)
  2. 创建一个空项目其目标是 .NET 4.0 Full 框架(否则 System.ServiceModel.Web 稍后将不可用)
  3. Microsoft.Samples.XmlRpc 项目从存档添加到您的项目
  4. 添加对 Microsoft.Samples.XmlRpc 项目的引用
  5. 添加对 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> 

Inspired by Doobi's answer, I looked up some more info (examples) on the subject, and came up with the following findings.

Steps to create simple WCF XML-RPC client:

  1. 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)
  2. Create an empty project which targets .NET 4.0 Full framework (or else System.ServiceModel.Web won't be available later on)
  3. Add Microsoft.Samples.XmlRpc project from the archive to your project
  4. Add reference to Microsoft.Samples.XmlRpc project
  5. Add references to System.ServiceModel and System.ServiceModel.Web

Example code

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");
        }
    }
}

Example request/response messages

Request XML

<?xml version="1.0" encoding="utf-8"?>
<methodCall> 
    <methodName>Hello</methodName> 
    <params> 
        <param> 
            <value> 
                <string>World</string> 
            </value> 
        </param> 
    </params> 
</methodCall> 

Response XML

<?xml version="1.0" encoding="utf-8"?>
<methodResponse> 
    <params> 
        <param> 
            <value> 
                <string>Hello, World!</string> 
            </value> 
        </param> 
    </params> 
</methodResponse> 
稀香 2024-09-09 07:19:41

最简单的方法是使用 WCF ChannelFactory

    IStuffService client = new ChannelFactory<IStuffService>(
        new BasicHttpBinding(),
        *"Stick service URL here"*)
        .CreateChannel();

并通过简单地调用此处执行请求

var response = client.YourOperation(params)

更多详细信息:
http://msdn.microsoft.com/en-us/library/ms734681.aspx

编辑:编辑;)

The easiest way is to use a WCF channelfactory

    IStuffService client = new ChannelFactory<IStuffService>(
        new BasicHttpBinding(),
        *"Stick service URL here"*)
        .CreateChannel();

And execute the request by simply calling

var response = client.YourOperation(params)

More details here:
http://msdn.microsoft.com/en-us/library/ms734681.aspx

edit:edited ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文