Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
657 views
in Technique[技术] by (71.8m points)

c# - Tutorial: Simple WCF XML-RPC client

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? :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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> 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...