Creating a basic REST Service
13K views
Nov 21, 2023
Creating a basic REST Service
View Video Transcript
0:00
Good morning everyone I am Dhanja Kumar and I am MbP on Connected System
0:04
In this learning video I am going to show you how we can create a basic race service
0:10
To create a basic service just go ahead and open a Visual Studio and create a new project
0:17
From the WCF tab select WCF service application project template in Visual Studio 210
0:23
And if you are using Visual Studio 2008 you will be getting this WCF service application project template inside the
0:29
the web tab so just go ahead and create a WCF service application you can see that
0:48
WCF has created some default code for you go ahead and delete all those default
0:55
code which got created by WCF because you really don't need that from the service implementation class and as well as from your contract also we
1:05
don't need any data contact because purpose of our video is to show you how we can
1:09
create a very basic service now what all you need to do is that I have a string
1:16
gate data it would be taking it would be taking integer value as you know in
1:21
rest service we cannot pass input parameters on any type other than a stream so
1:26
what I'm going to do is that I'm going to modify it as a string fine it will be returning or string to us now we need to construct this using
1:34
we have get fine let me increase the size of the font for you so that will have a
1:46
better view of that so what I'm going to do actually in this video is that I
1:51
be creating two operation contract in one operation contract or in one method
1:56
I'll be constructing the URI for you and other in other method we won't be constructing in URI so the method where we are not
2:04
constructing the URI we will be of accessing that particular method through
2:09
race service using their function name using their method name and on the other
2:14
method we are constructing the URI will be accessing that a method with the URI
2:18
which we are constructing so just go ahead and make it 60 for you
2:29
Fine. Now I'm giving gate data using method name, something like that
2:39
Fair enough. And the other method which I going to create as all we know that in RAS service everything is a made everything is a resource with a unique name unique URI and that unique name is called unique is called URI of that particular method and let us say get data is using URI just say URI and it will be taking a string 2 and value
3:11
Fine and here you change this to only method. Sure enough here I'll be constructing the URI. So URI will be URI template equal to, let us say, get data
3:27
And then since we have one of parameter in this, if you see string value, we need to give exact same name here as the second
3:37
the second path of our URI and make so since this is a parameter it should be in the Calibrillus
3:49
Fine. So we have constructed the URI. So we are going to access this get data using URI with the URI base address slash get data and slash the value
3:58
On the other hand, this particular method we have not constructed, so I'm going to access it using the method name and question mark and I'll be
4:07
passing value input parameter as value equal to the value what we want to pass
4:13
Now go ahead and implement this. Implement this method in our service class and here you are just make it as a public
4:25
just give the default codes and here you can return this is a method in right
4:31
return value on I am returned by method
4:49
Here, enough. Go ahead and implement the other method which we have, this one, and make it as again public, and say
5:07
value plus IMTUR and returned by UR&M
5:17
Now since all we know that to implement WCF service, binding available to us is web HTTP binding
5:29
So there are three ways you can create a service. The first way is that you can configure the end point here in your web
5:36
The other way is that you can configure the waypoint in a host in a console application and host your service in a service in a console app I be showing you that demo in further videos and the third point is the third way is that you can rely on the factory classes which
5:53
comes uh with wcf so what all you need to do is that if you're using uh visual studio 210
5:59
just go ahead and comment service hosting environment rely and if you're using visual studio
6:04
2008 then go ahead and comment all the endpoint configuration make sure that your your your met
6:10
data is not get enabled because this does not support the metadata so here I have
6:16
commented this click on your service file and open view markup frame up and here you have something called factory sorry factory
6:35
equal to system dot service model dot activation dot web service
6:51
host factory shame up system. system. . is a class which comes
7:01
with WCAF which allows you to create a service. If you want to see this class
7:07
just go ahead. In your class here you have system. service model. wave you can see this class here
7:16
system dot service model dot activation dot web service host factory web service hostwactor right
7:29
we are just using this class over there. fine now up to this point we have created our service we have we have created our contract
7:45
here we have created our contract and in the contract one method we are
7:50
constructing the uri in the for this particular method and for the other we are not
7:54
constructing the uri so we are going to access this method using the method name itself
8:00
then we have implemented this this to control these two method over here
8:04
then we configured the endpoint for a service using the factory classes web service for
8:09
factory class which comes as a part of WCF. Now go ahead and run your service
8:16
I don need to run it in the website I don need I don want to run it in WCF test plan I want to run it in the browser So for that what I need to do is that just run it again Oh sorry Let give me a moment
8:43
What resource explore right click on your service file and click in view in browser
8:51
Here you see that endpoint not found. So now if you go to your
8:57
methods you can see you have a base address that get data slash
9:03
value so you can access this gate data using UR are using base address
9:07
slash gate data slash value so what how you can access is that just open the
9:13
notepad I'll show you format format let us take it 28 and now if you see our base address is dot sbc up to here right
9:33
Just go ahead, this is your base address. Now, to access that, this you have given get data slash value, right
9:40
So go here, get data slash any value which you want. Let us say I'm giving value, Dhanenka
9:47
So Dhanja is the input parameter input for the string value. Now go ahead and copy paste this in your browser
9:56
You can see that we are getting Dhanenja, I am returned from your URL
10:02
am returned from uri name here we are using the this method to call get data slash
10:07
dhranjay and here the gate data slash value which we constructed the uri but other other
10:12
method get data using method we have not constructed the uri for this so we'll be accessing this
10:17
using the method name itself so for so for that what we need to do is that we this is our base
10:27
address right Then the metter name, exact method name you need to give here
10:35
The method name is get data using method. Question mark, the exact parameter name you have to give here
10:42
Value and then something let us say, Dhanja. Now go ahead, copy it in your browser
10:55
And you can see Dhanja I am returned by the method name. What I'm going to do is that if you change this value to value one
11:01
not the exact parameter name which you have given in the method, you can see you are not getting the value which you are passing as into parameter
11:08
So we need to make sure that the parameter name and the name you are calling through the URL must be the same
11:17
So here we are done. In this video, I have seen you that how we can create a basic restoration can access them using the method names or using the URL name
11:26
Thank you so much. Looking forward to meet you again. Thank you
#Programming
#Web Services
#Windows & .NET