ServiceStackHost.cs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright (c) Service Stack LLC. All Rights Reserved.
  2. // License: https://raw.github.com/ServiceStack/ServiceStack/master/license.txt
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Threading.Tasks;
  7. using MediaBrowser.Model.Logging;
  8. using MediaBrowser.Model.Services;
  9. using ServiceStack.Host;
  10. namespace ServiceStack
  11. {
  12. public abstract partial class ServiceStackHost : IDisposable
  13. {
  14. public static ServiceStackHost Instance { get; protected set; }
  15. protected ServiceStackHost(string serviceName)
  16. {
  17. ServiceName = serviceName;
  18. ServiceController = CreateServiceController();
  19. RestPaths = new List<RestPath>();
  20. Metadata = new ServiceMetadata();
  21. GlobalRequestFilters = new List<Action<IRequest, IResponse, object>>();
  22. GlobalResponseFilters = new List<Action<IRequest, IResponse, object>>();
  23. }
  24. public abstract void Configure();
  25. public abstract object CreateInstance(Type type);
  26. protected abstract ServiceController CreateServiceController();
  27. public virtual ServiceStackHost Init()
  28. {
  29. Instance = this;
  30. ServiceController.Init();
  31. Configure();
  32. ServiceController.AfterInit();
  33. return this;
  34. }
  35. public virtual ServiceStackHost Start(string urlBase)
  36. {
  37. throw new NotImplementedException("Start(listeningAtUrlBase) is not supported by this AppHost");
  38. }
  39. public string ServiceName { get; set; }
  40. public ServiceMetadata Metadata { get; set; }
  41. public ServiceController ServiceController { get; set; }
  42. public List<RestPath> RestPaths = new List<RestPath>();
  43. public List<Action<IRequest, IResponse, object>> GlobalRequestFilters { get; set; }
  44. public List<Action<IRequest, IResponse, object>> GlobalResponseFilters { get; set; }
  45. public abstract T TryResolve<T>();
  46. public abstract T Resolve<T>();
  47. public virtual MediaBrowser.Model.Services.RouteAttribute[] GetRouteAttributes(Type requestType)
  48. {
  49. return requestType.AllAttributes<MediaBrowser.Model.Services.RouteAttribute>();
  50. }
  51. public abstract object GetTaskResult(Task task, string requestName);
  52. public abstract Func<string, object> GetParseFn(Type propertyType);
  53. public abstract void SerializeToJson(object o, Stream stream);
  54. public abstract void SerializeToXml(object o, Stream stream);
  55. public abstract object DeserializeXml(Type type, Stream stream);
  56. public abstract object DeserializeJson(Type type, Stream stream);
  57. public virtual void Dispose()
  58. {
  59. //JsConfig.Reset(); //Clears Runtime Attributes
  60. Instance = null;
  61. }
  62. protected abstract ILogger Logger
  63. {
  64. get;
  65. }
  66. public void OnLogError(Type type, string message)
  67. {
  68. Logger.Error(message);
  69. }
  70. public void OnLogError(Type type, string message, Exception ex)
  71. {
  72. Logger.ErrorException(message, ex);
  73. }
  74. }
  75. }