Writing a simple web service in C # and calling it from Ruby on Rails

I need to create a simple web service in C #, but I'm not sure where to start (I used to code user interfaces in C #, but my whole web application works in Ruby on Rails). Where to begin?

The only client for the web service will be the Ruby on Rails application, so there is no need to render HTML. I was thinking of simply returning an XML or YAML formatted string if there is no simpler way. I'm not too keen on SOAP, but if it's easy / natural in C # and Ruby, then I would have thought (or something else).

+3
source share
4 answers

WCF, . WCF , , , Windows.

, :

    [ServiceContract]
    public interface ITestService {

        [OperationContract]
        [WebGet(
            BodyStyle = WebMessageBodyStyle.Bare,
            ResponseFormat = WebMessageFormat.Xml            
            )]
        XElement DoWork(string myId);

    }

:

    public class TestService : ITestService {

        public XElement DoWork(string myId) {

            return new XElement("results", new XAttribute("myId", myId ?? ""));
        }
    }

config (web.config app.config) :

    <system.serviceModel>
        <behaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />          
        </behavior>
      </endpointBehaviors>
    </behaviors>
        <services>
            <service name="WebApplication1.TestService">
                <endpoint behaviorConfiguration="WebBehavior"
                  binding="webHttpBinding" 
                  contract="WebApplication1.ITestService">
                </endpoint>             
            </service>
        </services>
    </system.serviceModel>

ASP.NET, TestService.svc :

<%@ ServiceHost Language="C#" Debug="true" 
                Service="WebApplication1.TestService" 
                CodeBehind="TestService.svc.cs" %>
+1

IIS 6 7, , ASP.NET-MVC 2. Visual Studio, :

public class ApiController : Controller {        

        public ActionResult Index(string id) {

            var xml = new XElement("results", 
                            new XAttribute("myId", id ?? "null"));

            return Content(xml.ToString(), "text/xml");
        }

    }

URL-, http://localhost:4978/Api/Index/test, :

<results myId="test"/>

, (JSON ..). ASP.NET MVC REST API, Ruby.

+1

MVC. XML:

public class XmlResult : ActionResult {
    public XmlResult(object anObject) {
        Object = anObject;
    }

    public object Object { get; set; }

    public override void ExecuteResult(ControllerContext aContext) {
        if (aContext == null) throw new Exception("Context cannot be null");
        var response = aContext.HttpContext.Response;
        response.ContentType = "application/xml";
        SerializeObjectOn(Object, response.OutputStream);
    }

    private void SerializeObjectOn(object anObject, Stream aStream) {
        var serializer = new XmlSerializer(anObject.GetType());
        serializer.Serialize(aStream, anObject);
    }
}

public class MyController : Controller {
    public ActionResult Index() {
        return  new XmlResult(object);
    }
}

Request it via http: // localhost / mycontroller

+1
source

here is a simple example.

I used Visual Studio to create a .asmx file and put it in .cs code.

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;

namespace MyNamespace.Newstuff.Webservice
{
    [WebService(Namespace = "http://iamsocool.com/MyNamespace/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    [ScriptService]
    public class MyNamespace : System.Web.Services.WebService
    {
        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}
0
source

Source: https://habr.com/ru/post/1736674/


All Articles