Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用C#编写一个简单的Web服务并从RubyonRails调用它_C#_Asp.net_Ruby On Rails_Ruby - Fatal编程技术网

用C#编写一个简单的Web服务并从RubyonRails调用它

用C#编写一个简单的Web服务并从RubyonRails调用它,c#,asp.net,ruby-on-rails,ruby,C#,Asp.net,Ruby On Rails,Ruby,我需要用C语言创建一个简单的web服务,但我不确定从哪里开始(我以前用C语言编写过UI应用程序,但我所有的web体验都是用Ruby on Rails)。我从哪里开始 Web服务的唯一客户端将是RubyonRails应用程序,因此不需要任何HTML呈现。我想返回一个XML或YAML格式的字符串,除非有更简单的方法。我对SOAP不太感兴趣,但是如果C C和Ruby很容易/很自然的话,我会考虑它(或者别的什么)。 我使用Visual Studio创建了一个.asmx文件,并将其放在.cs代码中 usi

我需要用C语言创建一个简单的web服务,但我不确定从哪里开始(我以前用C语言编写过UI应用程序,但我所有的web体验都是用Ruby on Rails)。我从哪里开始


Web服务的唯一客户端将是RubyonRails应用程序,因此不需要任何HTML呈现。我想返回一个XML或YAML格式的字符串,除非有更简单的方法。我对SOAP不太感兴趣,但是如果C C和Ruby很容易/很自然的话,我会考虑它(或者别的什么)。 我使用Visual Studio创建了一个.asmx文件,并将其放在.cs代码中

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";
        }
    }
}

如果您有可以部署到的IIS 6或7环境,我只需要创建一个ASP.NET-MVC2应用程序。您可以使用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使用REST API应该很容易。

如果您想要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 ?? ""));
        }
    }
应用程序配置(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" %>

我同意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);
    }
}

通过

请求,实际上我最终还是选择了这样的方式。。但是切换到JSON格式。我用这篇博文作为参考: