示例#1
0
 public void Add(MethodInfo method, string basePath)
 {
     foreach (RestRouteAttribute routeAttr in method.GetRouteAttributes())
     {
         string path = CreatePath(basePath, routeAttr.Path);
         _logger.Information("Registering Route {0} {1}", routeAttr.HttpMethod, path);
         IRoute route = _routeFactory.Create(method, routeAttr.HttpMethod, path);
         if (!Routes.Add(route))
         {
             throw new DuplicateRouteException("Duplicate Route {0}", route);
         }
     }
 }
示例#2
0
        public void Start()
        {
            if (IsListening || _starting)
            {
                return;
            }

            _starting = true;

            try
            {
                if (!_hasRun)
                {
                    _router.Initialize();
                    _httpListener.Prefixes.Add(_uriBuilder.Uri.ToString());
                }

                _httpListener.Start();
                _httpListenerTask = Task.Factory.StartNew(async() =>
                {
                    while (IsListening)
                    {
                        HttpContext context = await _httpListener.GetContextAsync();
                        _logger.Information("Received Request {0} {1}", context.Request.HttpMethod,
                                            context.Request.Path);
                        try
                        {
                            IResult result = _router.Route(context);
                            result.Execute(context);
                        }
                        catch (Exception ex)
                        {
                            context.Response.StatusCode = HttpStatusCode.InternalServerError;
                            context.Response.Send();
                            _logger.Error("Internal Server Error ({0}): {1}", ex.GetType(), ex.Message);
                        }
                    }
                }, TaskCreationOptions.LongRunning);
            }
            catch (Exception ex)
            {
                _logger.Error($"RestFulServer: {ex.Message}\r\n{ex.StackTrace}");
            }
            finally
            {
                _starting = false;
                _hasRun   = true;
            }
        }