Click here to Skip to main content
15,886,806 members
Home / Discussions / C#
   

C#

 
GeneralRe: Send Email Notifications from Server Pin
Bernhard Hiller10-Aug-14 20:06
Bernhard Hiller10-Aug-14 20:06 
Question.net framework Pin
Member 109952356-Aug-14 1:07
Member 109952356-Aug-14 1:07 
AnswerRe: .net framework PinPopular
Pete O'Hanlon6-Aug-14 1:09
mvePete O'Hanlon6-Aug-14 1:09 
GeneralRe: .net framework Pin
Joe Woodbury6-Aug-14 14:00
professionalJoe Woodbury6-Aug-14 14:00 
AnswerRe: .net framework Pin
Akhil Mittal6-Aug-14 20:07
professionalAkhil Mittal6-Aug-14 20:07 
QuestionAfter Call Through WebAPI, Entity Is Null Pin
Kevin Marois5-Aug-14 8:14
professionalKevin Marois5-Aug-14 8:14 
QuestionRe: After Call Through WebAPI, Entity Is Null Pin
Richard Deeming5-Aug-14 8:48
mveRichard Deeming5-Aug-14 8:48 
AnswerRe: After Call Through WebAPI, Entity Is Null Pin
Kevin Marois5-Aug-14 12:40
professionalKevin Marois5-Aug-14 12:40 
It's a wrapper class for REST that I wrote. I'm 100% sure it's not the problem, but here's the code anyhow:

    /// <summary>
    /// Execute Web API calls
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class WebAPIExecutor
    {
        #region Private Fields
        /// <summary>
        /// The client
        /// </summary>
        private RestClient client;
        /// <summary>
        /// The request
        /// </summary>
        private RestRequest request;
        #endregion

        #region Properties
        public string ServerName { get; private set; }
        public string ServerAddress { get; private set; }
        #endregion

        #region CTOR
        /// <summary>
        /// Initializes a new instance of the <see cref="WebAPIExecutor"/> class.
        /// </summary>
        public WebAPIExecutor()
        {
            setupServerURL();
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="WebAPIExecuter{T}"/> class.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <exception cref="System.ArgumentNullException">Url</exception>
        public WebAPIExecutor(string url, Method method = Method.POST)
        {
            if (string.IsNullOrEmpty(url))
            {
                throw new ArgumentNullException("Url");
            }

            // Set up the URL to the server
            setupServerURL();

            client = new RestClient(ServerAddress);

            client.AddHandler("text/plain", new JsonDeserializer());

            request = new RestRequest(url, method)
            {
                RequestFormat = RestSharp.DataFormat.Json
            };
        }
        #endregion

        #region Public Methods
        public void AddParameter(object value, string name = "")
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            Type type = value.GetType();
            bool isPrimitive = type.IsPrimitive;

            //if(!string.IsNullOrEmpty(name) || (isPrimitive || type == typeof(string) || type == typeof(decimal)))
            if(isPrimitive || type == typeof(string) || type == typeof(decimal))
            {
                if (string.IsNullOrEmpty(name) && request.Method == Method.GET)
                {
                    throw new ArgumentNullException("Parameter 'Name' cannot be empty for Get requests");
                }

                request.AddParameter(name, value);
            }
            else
            {
                request.AddBody(value);
            }
        }

        /// <summary>
        /// Executes a command that returns a result
        /// </summary>
        /// <returns>`0.</returns>
        public T Execute<T>() where T : new()
        {
            if (request.Resource.Trim().ToLower().Contains("controller"))
            {
                string message = string.Format("Controller names cannot contain the word 'Controller'. Called from request {0}", request.Resource);
                MessageBox.Show(message, "WebAPI Warning", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                throw new Exception(message);
            }

            var url = client.BaseUrl + request.Resource;

            IRestResponse<T> result = client.Execute<T>(request);

            int resultValue = (int)result.StatusCode;

            if (resultValue >= 299)
            {
                string message = string.Format("An error occured calling the WebAPI. {0} The status code is '{1}'. {2} The error message is {3}", 
                                                Environment.NewLine, result.StatusCode, Environment.NewLine, result.Content );
                MessageBox.Show(message, "WebAPI Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                throw new Exception(message);
            }

            return result.Data;
        }

        /// <summary>
        /// Executes a command that returns no results
        /// </summary>
        public void Execute()
        {
            IRestResponse result = null;

            try
            {
                result = client.Execute(request);

                //TODO: Handle this differently.
                int resultCode = (int)result.StatusCode;

                //if (result.StatusCode != HttpStatusCode.OK)
                if (resultCode > 299)
                {
                    string message = string.Format("An error occured calling the WebAPI. {0} The status code is '{1}'. {2} The error message is {3}", 
                                                    Environment.NewLine, result.StatusCode, Environment.NewLine, result.Content );
                    MessageBox.Show(message, "WebAPI Error", MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    throw new Exception(message);
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Private Methods
        private void setupServerURL()
        {
            ServerName = ConfigurationManager.AppSettings["servername"];
            string ipaddress = ConfigurationManager.AppSettings["ipaddress"];
            string port = ConfigurationManager.AppSettings["port"];

            //string IPAddress = "98.145.182.97";
            //int port = 80;

            //string IPAddress = "localhost";
            //int port = 51295;

            ServerAddress = string.Format("http://{0}:{1}/api", ipaddress, port);
        }
        #endregion
    }
}

If it's not broken, fix it until it is

QuestionRe: After Call Through WebAPI, Entity Is Null Pin
Richard Deeming6-Aug-14 1:33
mveRichard Deeming6-Aug-14 1:33 
Questioni don't know at what is zed using in this code the ''if(mode == 'c')",pls help! Pin
Alex Sturza5-Aug-14 5:41
Alex Sturza5-Aug-14 5:41 
AnswerRe: i don't know at what is zed using in this code the ''if(mode == 'c')",pls help! Pin
Dilan Shaminda5-Aug-14 5:49
professionalDilan Shaminda5-Aug-14 5:49 
GeneralRe: i don't know at what is zed using in this code the ''if(mode == 'c')",pls help! Pin
Alex Sturza5-Aug-14 6:17
Alex Sturza5-Aug-14 6:17 
GeneralRe: i don't know at what is zed using in this code the ''if(mode == 'c')",pls help! Pin
Dilan Shaminda5-Aug-14 6:34
professionalDilan Shaminda5-Aug-14 6:34 
AnswerRe: i don't know at what is zed using in this code the ''if(mode == 'c')",pls help! Pin
Pete O'Hanlon5-Aug-14 5:49
mvePete O'Hanlon5-Aug-14 5:49 
QuestionWebRequest with Cookie and Post data real life question - code included Pin
KergalBerlin5-Aug-14 3:25
KergalBerlin5-Aug-14 3:25 
AnswerRe: WebRequest with Cookie and Post data real life question - code included Pin
Bernhard Hiller5-Aug-14 20:57
Bernhard Hiller5-Aug-14 20:57 
GeneralRe: WebRequest with Cookie and Post data real life question - code included Pin
KergalBerlin5-Aug-14 21:23
KergalBerlin5-Aug-14 21:23 
QuestionWhat is the main objective of using bar code Pin
Tridip Bhattacharjee4-Aug-14 21:51
professionalTridip Bhattacharjee4-Aug-14 21:51 
AnswerRe: What is the main objective of using bar code PinPopular
OriginalGriff4-Aug-14 22:30
mveOriginalGriff4-Aug-14 22:30 
GeneralRe: What is the main objective of using bar code Pin
Tridip Bhattacharjee5-Aug-14 4:21
professionalTridip Bhattacharjee5-Aug-14 4:21 
GeneralRe: What is the main objective of using bar code Pin
OriginalGriff5-Aug-14 4:27
mveOriginalGriff5-Aug-14 4:27 
Questioncreate vcf contact file Pin
Jassim Rahma4-Aug-14 20:56
Jassim Rahma4-Aug-14 20:56 
AnswerRe: create vcf contact file Pin
Richard MacCutchan4-Aug-14 21:02
mveRichard MacCutchan4-Aug-14 21:02 
AnswerRe: create vcf contact file Pin
Ravi Bhavnani5-Aug-14 6:50
professionalRavi Bhavnani5-Aug-14 6:50 
GeneralRe: create vcf contact file Pin
Jassim Rahma5-Aug-14 20:29
Jassim Rahma5-Aug-14 20:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.