49
University College of Southeast Norway http://home.hit.no/~hansha Web Services with Examples Hans-Petter Halvorsen, 2016.11.01

Web Services - HiT.nohome.hit.no/~hansha/documents/microsoft.net/tutorials/web_services... · 6 Introduction Tutorial: Web Services - with Examples • A Standard defined by W3C •

Embed Size (px)

Citation preview

UniversityCollegeofSoutheastNorway

http://home.hit.no/~hansha

WebServiceswithExamples

Hans-PetterHalvorsen,2016.11.01

2

TableofContents1. Introduction......................................................................................................................4

1.1. TheProblem...............................................................................................................4

1.2. TheSolution................................................................................................................5

1.3. WebServicesOverview..............................................................................................5

1.4. RESTWebServices......................................................................................................9

2. CreatingWebServiceswithVisualStudio.......................................................................10

3. ASMXWebService..........................................................................................................11

3.1. CreateaWebMethod..............................................................................................14

4. IISWebServer.................................................................................................................15

5. WebServiceClientinVisualStudio.................................................................................16

5.1. WindowsForms........................................................................................................16

6. GetDatafromaTemperatureDevice.............................................................................17

6.1. TC-01ThermocoupleDevice.....................................................................................17

7. WebServicewithDatafromaDatabase.........................................................................18

7.1. CreateDatabase.......................................................................................................18

7.2. DataAccessTier........................................................................................................20

7.3. CreatetheWebService............................................................................................23

7.4. UsingtheWebService..............................................................................................27

8. WebServicesinLabVIEW................................................................................................30

8.1. WebService..............................................................................................................30

8.2. LabVIEWClient.........................................................................................................33

3 TableofContents

Tutorial:WebServices-withExamples

8.2.1. TemperatureConversion...................................................................................33

8.2.2. WeatherStationExample..................................................................................36

9. DataDashboardforLabVIEW..........................................................................................44

10. Python..........................................................................................................................47

11. MATLAB........................................................................................................................48

4

1. IntroductionInadditiontothiswrittenTutorial,youwillfindadditionalresourceshere:

http://home.hit.no/~hansha/?tutorial=webservice

1.1. TheProblemOurproblemis:HowtoShareDatabetweenDevicesinaNetwork?

DirectConnectionbetweentheDatabaseandtheClientsthatneedtheDataisnormallynotpossible,duetosecurity,compatibilityissues,etc.(Firewalls,HackerAttacks,etc.).

DirectConnectioninaLocalNetwork(behindtheFirewall)isnormallyOK–butnotovertheInternet.

5 Introduction

Tutorial:WebServices-withExamples

1.2. TheSolutionSolution:WebService.WebServicesusesstandardwebprotocolslikeHTTP,etc.

HTTPissupportedbyallWebBrowser,ServersandmanyProgrammingLanguages

TodayWebServiceshavebeenverypopular.AWebserviceisamethodofcommunicationsbetweentwodevicesovertheWorldWideWeb,andmakesiteasytosharedataoveranetworkortheinternet.

1.3. WebServicesOverviewAWebServiceis

• AWebAPI

6 Introduction

Tutorial:WebServices-withExamples

• AStandarddefinedbyW3C• Cross-platformandPlatform-independentCommunication• DistributedApplicationDevelopment

WebServicescanbeimplementedandusedinmostProgrammingLanguages(C#/ASP.NET,PHP,LabVIEW,Objective-C,Java,etc.)

WebServicesusesstandardWebtechnology(Webprotocols)suchasHTTP,REST,SOAP,XML,WSDL,JSON,etc.

WebServicestechnologyusedinWebServices:

• HTTP-HypertextTransferProtocol

• XML–ExtensibleMarkupLanguage

• WSDL-WebServicesDescriptionLanguage

• SOAP-SimpleObjectAccessProtocol

• REST-RepresentationalStateTransfer

AWebServiceistypicallydeployedonawebserver,similiarasordinarywebpages.

7 Introduction

Tutorial:WebServices-withExamples

Wehave2differenttypesofWebServices:

• WebServices1.0:SOAPWebServices

“Complex”

• WebServices2.0:RESTWebServices

– LightwightandFlexible

– AnewandsimplerversionofWS

– AllmajorWSontheInternettodayuseREST

BelowwesummarizeWebServices1.0vs.2.0.

8 Introduction

Tutorial:WebServices-withExamples

Belowweseethedifferent“layers”a“SOAPWebService”consistsof.

XML:

XMLstandsforeXtensibleMarkupLanguage.XMLisdesignedtotransportandstoredata.

BelowweseeanXMLdocumentexample.

<?xml version="1.0"?> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>

9 Introduction

Tutorial:WebServices-withExamples

</note>

1.4. RESTWebServicesBelowweseethedifferent“layers”a“RESTWebService”consistsof.

10

2. CreatingWebServiceswithVisualStudio

VisualStudiohaspowerfulfeaturesforcreatingWebServices.

3waystodoit:

• ASMXWebService(TraditionalWebWerviceusingSOAP)• WCFService:Ageneralapproachusedtocreateallkindofcommunicationincluding

webservices,bothSOAPandREST• ASP.NETWebAPI(ThemodernWebServiceusingREST,Web2.0)

11

3. ASMXWebServiceInthischapterwewillgothroughthestepsinordertocreateaASMX/SOAPWebServiceinVisualStudio.

NewProject:

Selectthe“Empty”template:

12 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

Select“AddNewItem”:

13 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

Selectthe“WebService(ASMX)”item:

Thedeafulttemplatelookslikethis:

14 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

Testthebuilt-inHelloWorld()method–F5

3.1. CreateaWebMethodCreateamethodthatgetsthecurrentdateandtime:

[WebMethod] public string GetTime() {

DateTime time = DateTime.Now; string dateformat = "yyyy.MM.dd HH:MM:ss"; return time.ToString(dateformat); }

15

4. IISWebServerIIS–InternetInformationServer

16

5. WebServiceClientinVisualStudio

Walkthrough:ConnectingtoDatainaWebService(WindowsForms):

http://msdn.microsoft.com/en-us/library/ms171891.aspx

5.1. WindowsFormsCreateaNewWinFormProject.

Select“AddServiceReference”

17

6. GetDatafromaTemperatureDevice

InthischapterwewillpublishtemperaturedatafromatemperaturedeviceviaaWebService.

6.1. TC-01ThermocoupleDeviceBelowweseetheTC-01ThermocoupledevicefromNationalInstruments:

TC-01WebSite:http://sine.ni.com/nips/cds/view/p/lang/no/nid/208177

18

7. WebServicewithDatafromaDatabase

ThemainpurposewithaWebServiceistosharedatafromadatabasebetweendevicesinanetwork.

DirectConnectionbetweentheDatabaseandtheClientsthatneedtheDataisnormallynotpossible,duetosecurity,compatibilityissues,etc.(Firewalls,HackerAttacks,etc.).

DirectConnectioninaLocalNetwork(behindtheFirewall)isnormallyOK–butnotovertheInternet.

7.1. CreateDatabaseWearegoingtocreatetheDatabase/DataTier,including:

• Tables

• Views

• StoredProcedures

19 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

• etc.

Wearegoingtocreatethefollowingexampledatabase:

20 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

ThedatabaseisdesignedusingErwin.

TheDatabaseiscreatedandimplementedusingSQLServerManagementStudio

YoumaydownloadaZipFilewithTables,Views,StoredProcedures,etc.inordertocreatetheDataTierinSQLServer.

http://home.hit.no/~hansha/?tutorial=sql

7.2. DataAccessTierWewillcreateaprojectinVisualStudiowherewecreateallethelogic/codethatdealwiththecommunicationwiththedatabase.

CreateanemptySolutioninVisualStudio:

21 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

AddProjectforDataAccessTier:

AddaNewClasstotheProject:

22 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

CreatetheCode,e.g.,likethis:

MakesuretoimportthenecessaryNameSpaces.YoushouldalsocreateaproperNamespacefortheClassfile.

CreateapropernamefortheAssembly(.dllFile):

23 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

ThenBuildyourProject(hopefullywithnoerrors).ThiswillbetheAssemblyforyourDataAcces(Logic)Tier,thatcanbeimportedandusedinotherprojects.Createonce–useitmanytimes!!

7.3. CreatetheWebServiceNowwewillcreatetheWebServicethatistheconnectionbetweentheDataAccess(Logic)TierandtheDevicethatisgoingtocommunicatewiththedatabase.

CreateanASP.NETProject:

24 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

AddWebService:

WebServiceCode:

25 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

TheDatabaseConnectionStringislocatedinWeb.config:

TesttheWebService:

26 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

Deploy/PublishWebServicetoIIS.CopyWebServiceFilestodefaultIISDirectory:C:\inetpub\wwwroot.

27 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

7.4. UsingtheWebServiceWewilluseWebServiceinaWinFormApplication.

CreateNewWinFormProject:

AddWebServiceReference:

28 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

CreateGUI:

CreateCode:

29 WebServicewithDatafromaDatabase

Tutorial:WebServices-withExamples

Testit:

Itworks!

30

8. WebServicesinLabVIEW

8.1. WebServiceTutorial:CreatingandAccessingaLabVIEWWebService:

http://zone.ni.com/reference/en-XX/help/371361K-01/lvhowto/build_web_service

CreateanewLabVIEWproject:

TheProjectExplorershouldnowlooklikethis:

31 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

CreateaWebServiceMethod(SubVI):

32 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

BelowweseetheWebServicespaletteinLabVIEW

Createthefollowingblockdiagram:

Testit:

33 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

IntheWebbrowseritshouldlooklikethis:

8.2. LabVIEWClient

8.2.1. TemperatureConversion

WewillgetdatafromaWebServiceusingLabVIEW.

WewillusetheTUCWeatherstationWebServiceasexample:

http://www.w3schools.com/WebServices/tempconvert.asmx

34 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

WDSL:

http://www.w3schools.com/WebServices/tempconvert.asmx?WSDL

LabVIEW:

SelectwhichoftheavailibleWebMethodsyouwanttoimport:

35 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

TheProjectExplorershouldlooklikethis:

Createasimpleexample:

36 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

FrontPanel:

8.2.2. WeatherStationExample

WewillgetdatafromaWebServiceusingLabVIEW.

WewillusetheTUCWeatherstationWebServiceasexample:

http://128.39.35.252/WebApi/WebService.asmx

37 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

TheWSDL:

http://128.39.35.252/WebApi/WebService.asmx?WSDL

38 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

LetsstartcreatingthetheWebServiceclientinLabVIEW:

TypetheWebServicepath/URL:

39 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

40 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

41 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

42 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

Finalresults:

43 WebServicesinLabVIEW

Tutorial:WebServices-withExamples

44

9. DataDashboardforLabVIEW

TheDataDashboardforLabVIEWappletsyoucreateacustomdashboardthatcanremotelycontrolandmonitorrunningNILabVIEWapplicationsusingWebServices.

WebSite:www.ni.com/mobile

TheAppisavailibleforIOS,AndroidandWindows8(WindowsStoreApp).

DownloadDashboardAppforiPadhere:https://itunes.apple.com/us/app/data-dashboard-for-labview/id481303987?mt=8

GettingStartedwiththeDataDashboardforLabVIEW:

http://www.ni.com/white-paper/13757/en/

45 DataDashboardforLabVIEW

Tutorial:WebServices-withExamples

ApremaidDataDashboardfortheWeathersystemattelemarkUniversityCollegeisshownbelow:

DownloadpremadeDashboardhere:http://home.hit.no/~hansha/?page=weather

TheDataDashboardfortheWeathersystemisusingthefollowingwebservice:http://128.39.35.252:8080/TUCweather/WebServices

WewillusethisWebServiceinordertocreateasimpleexample(usinganiPhone).

DownloadandopentheLabVIEWforDataDashboardApp:

46 DataDashboardforLabVIEW

Tutorial:WebServices-withExamples

ConnecttotheWebService:

Seelctoneoftheavailiblewebmethods:

47

10. Python

48

11. MATLAB

Hans-PetterHalvorsen,M.Sc.

E-mail:[email protected]

Blog:http://home.hit.no/~hansha/

UniversityCollegeofSoutheastNorway

www.usn.no