زمانیکه از Templateهای پیش فرض تدارک دیده شده در VS.Net برای اپلیکیشنهای وب خود استفاده میکنید، وب اپلیکیشن و سرور با هم یکپارچه هستند و تحت IIS اجرا میشوند. به وسیله
Owin میتوان این دو مورد را بدون وابستگی به IIS به صورت مجزا اجرا کرد. در این پست قصد داریم سرویسهای Web Api را در قالب یک Windows Service با استفاده از کتابخانهی
TopShelf هاست نماییم.
پیش نیاز ها:
»
Owin چیست
»
تبدیل برنامههای کنسول ویندوز به سرویس ویندوز ان تی
برای شروع یک برنامه Console Application ایجاد کرده و اقدام به نصب پکیجهای زیر نمایید:
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
Install-Package TopShelf
حال یک کلاس Startup برای پیاده سازی Configurationهای مورد نیاز ایجاد میکنیم
در این قسمت میتوانید تنظیمات زیر را پیاده سازی نمایید:
»سیستم Routing؛
»تنظیم Dependency Resolver برای تزریق وابستگی کنترلرهای Web Api؛
»تنظیمات hubهای SignalR(در حال حاضر SignalR به صورت پیش فرض نیاز به Owin برای اجرا دارد)؛
»رجیستر کردن Owin Middlewareهای نوشته شده؛
»تغییر در Asp.Net PipeLine؛
»و...
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);
}
}
* به صورت پیش فرض نام این کلاس باید Startup و نام متد آن نیز باید Configuration باشد.
در این مرحله یک کنترلر Api به صورت زیر به پروژه اضافه نمایید:
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)
{
}
}
کلاسی به نام ServiceHost ایجاد نمایید و کدهای زیر را در آن کپی کنید:
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();
}
}
واضح است که متد Start در کلاس بالا با استفاده از متد Start کلاس WebApp، سرویسهای Web Api را در آدرس مورد نظر هاست خواهد کرد. با فراخوانی متد Stop این سرویسها نیز dispose خواهند شد.
در مرحله آخر باید شروع و توقف سرویسها را تحت کنترل کلاس HostFactory کتابخانه TopShelf در آوریم. برای این کار کافیست کلاسی به نام ServiceHostFactory ایجاد کرده و کدهای زیر را در آن کپی نمایید:
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());
} );
} );
}
}
توضیح کدهای بالا:
ابتدا با فراخوانی متد Run سرویس مورد نظر اجرا خواهد شد. تنظیمات نام سرویس و نام مورد نظر جهت نمایش و همچنین توضیحات در این قسمت انجام میگیرد.
با استفاده از متد ConstructUsing عملیات وهله سازی از سرویس انجام خواهد گرفت. در پایان نیز متد Start و Stop کلاس ServiceHost، به عنوان عملیات شروع و پایان سرویس ویندوز مورد نظر تعیین شد.
حال اگر در فایل Program پروژه، دستور زیر را فراخوانی کرده و برنامه را ایجاد کنید خروجی زیر قابل مشاهده است.
ServiceHostFactory.Run();
در حالیکه سرویس مورد نظر در حال اجراست، Browser را گشوده و آدرس http://localhost:8000/api/values/get را در AddressBar وارد کنید. خروجی زیر را مشاهده خواهید کرد: