هاست سرویس های Asp.Net Web Api با استفاده از OWIN و TopShelf
نویسنده: مسعود پاکدل
تاریخ: ۱۳۹۳/۰۲/۲۷ ۲۳:۱۵
آدرس: www.dntips.ir
| مطالب | ۳۶۹۴ |
| نویسندگان | ۲۷۶ |
| گروههای مطالب | ۱۰۲۴ |
| نقشههای راه | ۱۱۹ |
| دورهها | ۱۴ |
| اشتراکها | ۱۷۹۱۴ |
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost Install-Package TopShelf
public class Startup
{
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
appBuilder.UseWebApi(config);
}
} public class ValuesController : ApiController
{
public IEnumerable<string> Get()
{
return new string[] { "value1", "value2" };
}
public string Get(int id)
{
return "value";
}
public void Post([FromBody]string value)
{
}
public void Put(int id, [FromBody]string value)
{
}
} public class ServiceHost
{
private IDisposable webApp;
public static string BaseAddress
{
get
{
return "http://localhost:8000/";
}
}
public void Start()
{
webApp = WebApp.Start<Startup>(BaseAddress);
}
public void Stop()
{
webApp.Dispose();
}
} public class ServiceHostFactory
{
public static void Run()
{
HostFactory.Run( config =>
{
config.SetServiceName( "ApiServices" );
config.SetDisplayName( "Api Services ]" );
config.SetDescription( "No Description" );
config.RunAsLocalService();
config.Service<ServiceHost>( cfg =>
{
cfg.ConstructUsing( builder => new ServiceHost() );
cfg.WhenStarted( service => service.Start() );
cfg.WhenStopped( service => service.Stop());
} );
} );
}
} ServiceHostFactory.Run();