2012-03-09 2 views
7

나는 자체 호스트 된 웹 서비스 (원래 WCF WebApi로 작성)의 예제 인 WCF WebApi의 자손 인 새로운 ASP.NET WebAPI를 사용하여 this을 시험해보고 싶었다.ASP.NET WebAPI의 HttpServiceHost에 해당하는 항목은 무엇입니까?

using System; 
using System.Net.Http; 
using System.ServiceModel; 
using System.ServiceModel.Web; 
using System.Text; 
using Microsoft.ApplicationServer.Http; 

namespace SampleApi { 
    class Program { 
     static void Main(string[] args) { 
      var host = new HttpServiceHost(typeof (ApiService), "http://localhost:9000"); 
      host.Open(); 
      Console.WriteLine("Browse to http://localhost:9000"); 
      Console.Read(); 
     } 
    } 

    [ServiceContract] 
    public class ApiService {  
     [WebGet(UriTemplate = "")] 
     public HttpResponseMessage GetHome() { 
      return new HttpResponseMessage() { 
       Content = new StringContent("Welcome Home", Encoding.UTF8, "text/plain") 
      };  
     } 
    }  
} 

그러나 NuGotten이 올바른 패키지가 아니거나 HttpServiceHost가 AWOL입니다. (나는 '자체 호스팅'변종을 선택했다.)

무엇이 누락 되었습니까?

+0

[This] (http://code.msdn.microsoft.com/ASPNET-Web-API-Self-Host-30abca12/view/Reviews)는 내가 뭔가 도움이되었지만 보이지 않습니다. 엄하게 동등한 것. – Benjol

답변

10

자기 호스팅은이 문서를 참조하십시오 : 다음과 같이 예를 들어

Self-Host a Web API (C#)

전체 다시 작성된 코드는 다음과 같습니다이 도움이

class Program { 

    static void Main(string[] args) { 

     var config = new HttpSelfHostConfiguration("http://localhost:9000"); 

     config.Routes.MapHttpRoute(
      "API Default", "api/{controller}/{id}", 
      new { id = RouteParameter.Optional } 
     ); 

     using (HttpSelfHostServer server = new HttpSelfHostServer(config)) { 

      server.OpenAsync().Wait(); 

      Console.WriteLine("Browse to http://localhost:9000/api/service"); 
      Console.WriteLine("Press Enter to quit."); 

      Console.ReadLine(); 
     } 

    } 
} 

public class ServiceController : ApiController {  

    public HttpResponseMessage GetHome() { 

     return new HttpResponseMessage() { 

      Content = new StringContent("Welcome Home", Encoding.UTF8, "text/plain") 
     };  
    } 
} 

희망을.

관련 문제