최근 웹 API 프로젝트를 OWIN (Visual Studio 2013)을 사용하여 자체 호스팅 서비스로 변환해야했습니다. 나는 다음과 같이했다.
(1) 프로젝트의 루트에 수동으로 Program.cs와 Startup.cs 파일을 추가했다. 여기에 설명 된 코드를 포함하는 두 파일 : http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api.
(2) 웹 API 프로젝트의 속성으로 이동했습니다. "응용 프로그램"섹션에서 "출력 유형"을 "콘솔 응용 프로그램"으로 지정하고 "프로그램"클래스를 "시작 개체"로 설정했습니다.
// Start OWIN host
using (WebApp.Start<Startup>(url: baseAddress))
{
// Create HttpCient and make a request to api/values
HttpClient client = new HttpClient();
var response = client.GetAsync(baseAddress + "api/values").Result;
if (response != null)
{
Console.WriteLine("Information from service: {0}", response.Content.ReadAsStringAsync().Result);
}
else
{
Console.WriteLine("ERROR: Impossible to connect to service");
}
Console.WriteLine();
Console.WriteLine("Press ENTER to stop the server and close app...");
Console.ReadLine();
}
마지막으로 대신 시작에서 config.Routes.MapHttpRoute()를 여러 번 호출하는 : 요구되지는 않지만 다음과 같이
, 나는 약간보고 Program.Main() 내에서 "사용"블록을 수정 .Configuration(), 당신은 이미 웹 API에 대해 쓴 경로를 참조 할 수 있습니다 : 여기
// Configure Web API for self-host.
var config = new HttpConfiguration();
WebApiConfig.Register(config);
app.UseWebApi(config);
몇 가지 예입니다 http://stackoverflow.com/questions/12899360/getting-a-post-endpoint-to -work-in-self-hosted-webservicehost-c-sharp-webservi, http://stackoverflow.com/questions/13165533/deserialize 내 이전 답변에서 -json-object-sent-from-android-app-to-wcf-webservice. –
@ L.B 귀하의 의견을 보내 주셔서 감사합니다. 그러나 제가 누락 된 것이 없으면이 질문에 어떻게 대답 할 수 있는지 잘 볼 수 없습니다. – trnelson
trnelson, 둘 다 답변보기 * 독립 실행 형 * RestFul 서비스 용 클라이언트 및 서버 코드. 당신이 유용하다고 생각하지 않는다면 제 코멘트를 무시하십시오. –