2012-03-09 12 views
7

मैं this एक स्व-होस्टेड webservice (मूल रूप से डब्लूसीएफ वेबएपी में लिखा गया) का उदाहरण देना चाहता था, लेकिन नए एएसपी.नेट वेबएपीआई (जो डब्ल्यूसीएफ वेबएपी के वंशज हैं) का उपयोग करना चाहता था।एएसपी.नेट वेबएपीआई में एचटीपीएस सर्विसहोस्ट के बराबर क्या है?

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

हालांकि, या तो मुझे सही पैकेज नहीं मिला है, या HttpServiceHost AWOL है। (मैंने 'स्वयं होस्टिंग' संस्करण चुना है)।

मुझे क्या याद आ रही है?

+0

[यह] (http://code.msdn.microsoft.com/ASPNET-Web-API- स्वयं- होस्ट-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") 
     };  
    } 
} 

आशा इस मदद करता है।

संबंधित मुद्दे