Implementing a XML-RPC service with ASP. NET MVC

Source: Internet
Author: User

I need ed a couple of emails recently asking how to implement an XML-RPC service in an ASP. NET MVC application. In case anyone is interested this is how to do it

Define an interface for your XML-RPC service, for example:

using CookComputing.XmlRpc;public interface IStateName{  [XmlRpcMethod("examples.getStateName")]  string GetStateName(int stateNumber);} 

Implement the service:

using CookComputing.XmlRpc;public class StateNameService : XmlRpcService, IStateName{  public string GetStateName(int stateNumber)  {    if (stateNumber < 1 || stateNumber > m_stateNames.Length)      throw new XmlRpcFaultException(1, "Invalid state number");    return m_stateNames[stateNumber - 1];  }  string[] m_stateNames    = { "Alabama", "Alaska", "Arizona", "Arkansas",        "California", "Colorado", "Connecticut", "Delaware", "Florida",        "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa",         "Kansas", "Kentucky", "Lousiana", "Maine", "Maryland", "Massachusetts",        "Michigan", "Minnesota", "Mississipi", "Missouri", "Montana",        "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico",         "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma",        "Oregon", "Pennsylviania", "Rhose Island", "South Carolina",         "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia",         "Washington", "West Virginia", "Wisconsin", "Wyoming" };} 

Implement a custom route handler:

using System.Web;using System.Web.Routing;public class StateNameRouteHandler : IRouteHandler{  public IHttpHandler GetHttpHandler(RequestContext requestContext)  {    return new StateNameService();  }} 

Register the custom route in global. asax. CS:

public static void RegisterRoutes(RouteCollection routes){  routes.IgnoreRoute("{resource}.axd/{*pathInfo}");  routes.Add(new Route("api/statename", new StateNameRouteHandler()));  // ...} 

Check that everything is working by pointing your browser to the URL for the handler, for example something like http: // localhost: 33821/API/statename in this case when running from Visual Studio. you shoshould then see an automatically generated help page for the service. if this is OK then point your XML-RPC client to the service and start making CILS.

Previous: Python notes: tuples, lists, ing (dictionary) | next: Support for live writer in Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.