예제 #1
0
        public ServerResponse CreateMessage(Message message)
        {
            // This is how error handling is done with WCF.
            // We throw an exception back to the client.
            // I don't like this way, I'd rather wrap everything in a try/catch and return a ServerResponse object (like I do in all my other methods).
            if (string.IsNullOrWhiteSpace(message.Text) ||
                message.Text.Length > 20)
            {
                throw new FaultException("The message must be between 1 and 20 characters.");
            }

            _messengerService.CreateMessage(message);

            return(new ServerResponse {
                Success = true
            });
        }
예제 #2
0
        public HttpResponseMessage CreateMessage([FromBody] Message message)
        {
            try
            {
                _messengerRepository.CreateMessage(message);

                var response = Request.CreateResponse(HttpStatusCode.Created);

                var uri = Url.Link("GetMessageRoute", new { id = message.Id });
                response.Headers.Location = new Uri(uri);

                return(response);
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, ex));
            }
        }