2010-03-12 2 views
3

C#으로 간단한 webservice를 생성해야하지만 어디서부터 시작해야할지 모르겠다. Ruby on Rails에서). 어디서부터 시작해야합니까?간단한 웹 서비스를 C#으로 작성하고 Ruby on Rails에서 호출하기

webservice의 유일한 클라이언트는 Ruby on Rails 앱이므로 HTML 렌더링이 필요 없습니다. 더 쉬운 방법이 아니면 그냥 XML 또는 YAML 형식 문자열을 반환하는 생각했다. 나는 SOAP에 너무 열중하지 않지만 C#에서 쉬운/자연 스럽다면 # & 루비 (Ruby)를 고려해 볼 것입니다.

답변

1

WCF의 유연성을 원할 경우 다음 코드를 시작해야합니다. WCF는 다른 답변보다 복잡 할 수 있지만 유연성이 향상되고 Windows 서비스에서 서비스를 호스팅 할 수있는 것과 같은 몇 가지 이점을 제공합니다.

[ServiceContract] 
    public interface ITestService { 

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

    } 

그리고 implentation은 다음과 같습니다 :

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 사이트에서 호스팅하려면, 파일 이름이 calle 은 IT에 다음과 D TestService.svc :

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

통해

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

요청을. 이 블로그 게시물을 참고 자료로 사용했습니다. http://dotnetninja.wordpress.com/2008/05/02/rest-service-with-wcf-and-json/ – ideasasylum

0

여기에 간단한 예가 나와 있습니다.

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

당신은 당신이에 배포 할 수있는 IIS 6 또는 7 환경을 사용하는 경우, 내가 그냥 ASP.NET MVC-2 응용 프로그램을 만들 것입니다. 당신은 비주얼 스튜디오 템플릿을 만들 수 있습니다 다음과 같은 컨트롤러가 있습니다

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

    } 

http://localhost:4978/Api/Index/test 같은 URL의 출력입니다 : 당신은 쉽게 돌아갑니다을 확장 할 수

<results myId="test"/> 

어떤 형식을 원하는 (JSON 등). 어쨌든 ASP.NET MVC를 사용하면 Ruby에서 쉽게 사용할 수있는 REST API를 쉽게 만들 수 있습니다.

1

은 내가 MVC 노선에 동의합니다. 다음은 XML로 밖으로 객체 제가 킥 사용할 수 있습니다 : 나는 실제로 결국 이런 일에 들어갑니다 ..하지만 JSON 형식으로 전환 http://localhost/mycontroller

관련 문제