Returning an image using WCF Service
10K views
Nov 7, 2023
Returning an image using WCF Service
View Video Transcript
0:00
Good morning everyone, I am Dhananjay Kumar and I am MVP on Connected System
0:04
In this tutorial video I am going to show you that how we can return an image using WCFS service
0:10
So to do so just go ahead and open your Visual Studio and create a new project
0:21
From WCFS tab select WCFS application and if you are using Visual Studio 2008 select
0:27
uh we'll be getting this wcf service application of project template inside the web tab
0:32
so go ahead and create and just give some meaningful name let us say get my image or something
0:43
what i'm going to do is that i'm going to return image which is on my hard drive
0:48
uh using a wcfos service Now if you see that by default WCF has created some code for us which we really don't need
1:03
Let's go ahead and delete this code. WCF created for you. Now, I am going to create an operation contract method which will be returning image for us
1:29
just get an operation contract. Let us say get image. Fine. The return type of operation contract would be stream because you want to return an image. Whenever you want to return an image on with your file, the return type should be image
1:47
So to work with image go ahead and add a system.io image class inside the system.io name space and then return stream
1:56
Now since this is a WCFRES service we are going to construct the URI for that and this wavegate and just say URI template equal to let us say get image How to construct URI template and how to work with URI template and for all detailed description on that you can see my other tutorial videos or you can read my articles on
2:29
the same blog so here I have constructed operation contract which is going to return us and a simple
2:36
stream that is team will be containing the image and here in web gate I've constructed a URI which
2:41
is saying that I am going to excel this particular resource or method of the service using the
2:46
base address slash get image now just go ahead and implement this inside your service class
2:51
for that again you need to include the namespace system dot io now here our method name is
3:00
stream get image this is public make it public and first what we need to do is that we need to open the file
3:12
stream file stream let us say fs equal to file open file dot open read it will be returning an stream to us
3:25
find and from where so we want to read it from d drive and a.jpg file which I want to
3:35
read it if you see in my d drive I can show you that I have that a.jpg file over here I'm
3:42
going to return this using our WCF service now just return this stream now the problem
3:51
is that we need to tell explicitly to our we need to tell explicitly to in our
3:56
WCF service that we are going to return and means so for that web
4:01
operation contacts just work we have present contacts then in current and ordering response dot content we need to explicitly say that the content
4:15
content type is image slash jpeg so here we are telling explicitly that
4:27
sorry its content type actually content type is image slash jpeg so we are telling explicitly to our wcf that we are returning image as a string now to make it rest enable service go ahead and open your web file
4:46
If you are using Visual Studio 2.8 you will have a default endpoint
4:50
Just disable that and if you are using 2.10 just comment out that multiple site binding enable is true
4:59
now open your markup of the service file and here we need to add a factory to work as a
5:06
wcf race so between make it as a race service factory equal to system dot service model dot
5:19
So, I have created RwCF service
5:34
I will again explain that first I created an operation contract and here I am saying
5:39
that I am going to return an stream. In the service implementation, I am saying that in my outgoing response of web operation
5:45
contacts that I'm returning an image of the type jpeg and then I have disabled
5:52
all the end point the default endpoint is got created through the WCF I have and
5:56
I'm using the factory class system or service model activation of web service
6:00
was factory to enable our WCF services service now just rebuild it
6:06
So we have built it successfully. Go ahead and run your service here
6:23
Since Internet Explorer is able to understand what is the JPEG format, so we should be able to get the output in the browser itself
6:34
So this is our base address and as we see here that we constructed that base address
6:38
slash get image will give us the output slash get image and here we are getting output in
6:47
the browser. So this was the purpose of this video. Just go ahead and click one more thing in your service Here we are saying that our content type is image slash jpeasy Right Jpe is the format which is understandable by the Internet Explorer browser
7:04
Just change it to some other format, let us say ABC. What is the ABC format
7:08
Extension of the image Internet Explorer does not understand. Now let's go ahead and run your service again
7:21
Viewing browser. Run your service again. if you can see here is get image your internet explorer is not able to
7:31
understand what is the extension ABC for image which is telling you that you want
7:36
to save the file if you save this file here I save that file now I save that
7:46
file on my desktop if you go ahead if you go there get image and open your paint
7:58
just open from the desktop just get image and here we are able to get that because
8:09
paint is able to understand or able to we can say integrate.extension
8:13
but our browser is not able to understand that what is the exactly the type
8:17
ABC for the image so it was not able to able to render that and it told and it
8:22
gave the message to the user to download or save that particular files again if
8:27
you change to the format which is understandable by your browser that's JPJ you can get the output in the IE itself
8:37
go ahead by default my WSIP client is getting open so here if you see get image so
8:47
getting output in the browser. So this was the purpose of this video to show you how we can return
8:51
image using WCF-Rage service. Again, I explained that I did nothing. I just created an operation
8:57
contract which is returning any stream. Stream is inside the name of space system.io. And while
9:02
the service is implementing, I am saying that waveOperationContacts.current.outgoingResponse content type is image with the extension jpez. And thank you so much for watching this video
9:14
Looking forward to meet you again
#Photo Printing Services
#Web Services