This is a migrated thread and some comments may be shown as answers.

Need a own method to get data from an openaccess webserver

0 Answers 29 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Volker
Top achievements
Rank 2
Volker asked on 20 Apr 2017, 11:33 AM
Hy,
i have an WPF Client which consumes data from an webserver (openaccess)
I need now a new server function which give me not all datasets of an table (GetAll).
The function should give me only datasets which are createt by an specified user.
In the SerialsController of the webserver I added a new function:
        publicvirtualIQueryable<DataLayer.Serial> GetUserSerials(Int32 id)
        {
            List<DataLayer.Serial> allEntities = repository.GetAll().ToList();
            List<DataLayer.Serial> userEntities = allEntities.Where(s => s.ActivatedBy == id).OrderByDescending(s => s.ActivatedAt).ToList();

            return userEntities.AsQueryable();
        }

Now i would call this function from the WPF Client.
I Added in publicinterfaceIWebClientWrapper the new function:
IEnumerable<Serial> GetAllUserSerials(Int32 id);
The problem is now to implement the method GetAllUserSerials in
publicclassWebClientWrapper: IWebClientWrapper
I copied the function for GetAll
        // GET ALL Definitionen
        protectedvirtualIEnumerable<T> GetAll<T>(string controller)
        {
            try
            {
                HttpResponseMessage responseGetEfka = this.client.GetAsync(controller).Result; //Blocking!
                if (responseGetEfka.IsSuccessStatusCode)
                {
                    // Parse the response body. //Blocking!
                    IEnumerable<T> allObjects = responseGetEfka.Content.ReadAsAsync<IEnumerable<T>>().Result;
                    return allObjects;
                }
                else
                {
                    //Couldn't get any User
                    returnEnumerable.Empty<T>();
                }
            }
            catch (AggregateException exception)
            {
                WebException webException = exception.GetWebExceptionFromAggregateException();

                if (webException != null)
                {
                    MessageBox.Show(webException.Message, MessageConstants.ErrorDataTitle,
                        MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    returnEnumerable.Empty<T>();
                }
                else
                {
                    throw;
                }
            }
        }

        publicIEnumerable<User> GetAllUsers()
        {
            returnthis.GetAll<User>(WebClientWrapper.UsersController);
        }

        publicvirtualIEnumerable<Serial> GetAllSerials()
        {
            returnthis.GetAll<Serial>(WebClientWrapper.SerialsController);
        }

and tried to call the new method in the webserver with his id
But I don’t know how:
HttpResponseMessage responseGetEfka = this.client.GetAsync(WebClientWrapper.SerialsController + "GetUserSerials/",id).Result; //Blocking!
This works not..
Can you help me how I have to implement this method, that he called the new method in the webserver and passes the id from the user which I need?

        // Own Definitions
        publicvirtualIEnumerable<Serial> GetAllUserSerials(Int32 id)
        {
            try
            {
                HttpResponseMessage responseGetEfka = this.client.GetAsync(WebClientWrapper.SerialsController + "GetUserSerials/",id).Result; //Blocking!
                if (responseGetEfka.IsSuccessStatusCode)
                {
                    // Parse the response body. //Blocking!
                    IEnumerable<Serial> allObjects = responseGetEfka.Content.ReadAsAsync<IEnumerable<Serial>>().Result;
                    return allObjects;
                }
                else
                {
                    //Couldn't get any User
                    returnEnumerable.Empty<Serial>();
                }
            }
            catch (AggregateException exception)
            {
                WebException webException = exception.GetWebExceptionFromAggregateException();

                if (webException != null)
                {
                    MessageBox.Show(webException.Message, MessageConstants.ErrorDataTitle,
                        MessageBoxButton.OK, MessageBoxImage.Exclamation);

                    returnEnumerable.Empty<Serial>();
                }
                else
                {
                    throw;
                }
            }
        }
Tags
General Discussions
Asked by
Volker
Top achievements
Rank 2
Share this question
or