Code Monkey home page Code Monkey logo

web-services's Introduction

Web-Services

Build a REST-based XML(JSON) WCF Web Service and a Client to consume the Web Service

  • IDE: Visual Studio Community 2015
  • Programming Language: C#
  • Framework: WCF Service Application

#1. REST-based XML WCF Web Service ##1) Web Service Interface

[ServiceContract]// interface xxx's attribute, indicating that interface xxx defines a service contract in WCF Application
public interface xxx
   {
        // *add an entry to the xx database
        [OperationContract] //method1's attribute, indicating that method1 is an operation which is part of the service contract in WCF Application
        [WebGet(UriTemplate = "/method1/{parameter1}/{parameter2}/{parameter3}")] //method1's attribute, indicating method1 is a retrieval opeartion, can be called by the WCF REST model.
        void method1(string parameter1, string parameter2, string parameter3);

        // *retrieve xx entries with a given property
        [OperationContract]
        [WebGet(UriTemplate = "/method2/{parameter1}")]
        PhoneBookEntry[] method2(string parameter1);
   } // end interface xxx

##2) Web Service Implementation

public class xxxWebService : xxx
    {
        // Create a REST-base web service that stores entries in a database xxx.mdf 
        // and client that consume this service.
        // create a dbcontext object to access xxx database
        private xxxEntities1 dbcontext = new xxxEntities1();

        // add an entry to the xxx database
        public void method1(string parameter1, string parameter2, string parameter2)
        {
            // *create PhoneBook entry to be inserted in database
            PhoneBook content = new PhoneBook();
            content.LastName = lastName;
            content.FirstName = firstName;
            content.PhoneNumber = phoneNumber;

            // *insert PhoneBook entry in database
            dbcontext.PhoneBooks.Add(content);
            //dbcontext.Entry(phoneBookEntry).State = System.Data.EntityState.Added;
            dbcontext.SaveChanges();

        } // end method1

        // retrieve entries with a given parameter1
        // The method2 should return an array of strings
        public PhoneBookEntry[] method2(string parameter1)
        {
            //*retrieve entries from the database according to parameter1 via LINQ
            var selectedByLastName = from phonebook in dbcontext.PhoneBooks
                                     where phonebook.LastName == lastName
                                     select phonebook;

            PhoneBookEntry[] phoneBookList = new PhoneBookEntry[selectedByLastName.Count()];

            //return an array of strings.
            int i = 0;
            foreach (var phonebook in selectedByLastName)
            {

                phoneBookList[i++] = new PhoneBookEntry(phonebook.FirstName, phonebook.LastName, phonebook.PhoneNumber);

            }

            return phoneBookList;
        }//end method2
    }

##3) Deploying a REST-based XML WCF Web Service

  • Open xxxWebService.svc in browser but don't close it while a client consuming the web service.

##4) Create a Client to consume the REST-based XML WCF web service - using HttpClient and getStringAsync

  private HttpClient client = new HttpClient();      // *create an object to invoke to PhoneBookService
  client.GetAsync(new Uri(service/method/parameter)); //method GetAsync(send a GET request to the specified URI as asynchronous operation)
  client.GetStringAsync(new Uri(service/method/parameter)); //method GetStringAsync(send a GET request to the specified URI and return the response body as a string)

#2. REST-based JSON WCF Web Service ##1) Web Service Interface(one place different from XML Web Service)

[OperationContract]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "/method1/{Parameter1}/{Parameter2}/{Parameter3}")]
void method1(string Parameter1, string Parameter2, string Parameter3);

Reference Link

web-services's People

Contributors

ashleycheny avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.