教程:简单的 WCF XML-RPC 客户端 [英] Tutorial: Simple WCF XML-RPC client

查看:37
本文介绍了教程:简单的 WCF XML-RPC 客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

我已经构建了自己的小型自定义 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? :)

推荐答案

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

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:

  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 的引用
  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

示例代码

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆