Tutorial: Simple WCF XML-RPC client

前端 未结 2 796
时光说笑
时光说笑 2020-12-08 05:40

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

相关标签:
2条回答
  • 2020-12-08 06:03

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

    0 讨论(0)
  • 2020-12-08 06:16

    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> 
    
    0 讨论(0)
提交回复
热议问题