.Net:a client for an IIS-hosted WCF service

This is the third and last post on the basics of WCF usage. I use SoapUI to consume the WSDL which exposes the single method we created in the first post, and hosted using IIS in the second post. None of this covers how to install SoapUI, although hopefully there is enough here to show you how to use it.


 

At the end of the previous post, we had access to the service wsdl via the link in blue:

WCFinIIS15

Click on the link, and from the URL that comes up next, copy that to the clipboard:

WCFinIIS16.JPG

Start SoapUI, and create a project as shown here. My PC is called [i7], so adjust your entries to match your setup, and then click OK:

WCFinIIS17

After that, the left side panel should look something like this:

WCFinIIS18

Double click [Request 1] and the main panel looks like this:

WCFinIIS19

Just to see what happens, click the green play button in the TLH corner:

WCFinIIS20

If you now put e.g. 101 rather than the default [?], you get…

WCFinIIS21

, which is exactly what we expect and want.

 

Advertisements

.Net: Hosting a simple WCF service in IIS

This assumes you have read the previous post, which left us with a simple service. However, it was only hosted/running while we were running in debug mode in Visual Studio. We don’t want to be, and can’t be dependent on a running VS instance. This looks at how to take that service and host it in IIS. It does not involve using Visual Studio.


 

This is the folder that contains our code and binaries for the service:

WCFinIIS01

Turning to IIS, this is how it looks before I change things:

WCFinIIS02.JPG

Add an application…

WCFinIIS03

Actually, I shall interrupt this to bring the service files under the inetpub/wwwroot folders – most of them anyway, and then whittle away at them to confirm the minimum set I need:

WCFinIIS04

In fact, I’m fairly confident about that minimum set, so I’ll just go ahead and cull them giving this (note that I have renamed app.config to web.config):

WCFinIIS05

… and the dll.config may go as well, don’t know yet:

WCFinIIS06

Now we go back to IIS and add all that as a new application to the Manager:

WCFinIIS07

OK, and now we have this:

WCFinIIS08

Now, just naively browsing to the root URL for that will get us nowhere:

WCFinIIS09

Reminding ourselves of the content of that root folder shows only 2 files, both of which are important… and wrong right now:

WCFinIIS05

web.config: both the highlighted entries should be removed (but keep a copy of the HelloAcmeService/HelloService entry for the svc file in the clipboard). You will/may need to be in admin mode to edit web.config under the IIS nodes:

WCFinIIS10

AcmeService.svc:

This is wrong in that the Service tag is pointing at the interface, and should be referencing the implementation, that is, IHelloService needs to change to HelloService:

WCFinIIS11.JPG

After those change, this is now what we see if we Browse Website in IIS:

WCFinIIS12

This location will never have a default or any readable web page – it is all about delivering a Service. If we now put an appropriate entry in the URL, we see…

Hm, interesting… this is symptomatic of specifying the interface, not the implementation:

WCFinIIS13

And wadya know, I hadn’t actually saved the change (or maybe the admin rights prevented it) after taking the screenshot. So making sure I press Save after this edit (in fact I found I needed to stop the web site in IIS first):

WCFinIIS14

Now we go again to the URL:

WCFinIIS15

That is good, if it is not obvious. That’s enough for this post, I think. The next post will talk about the wsdl, and using that in SoapUI to test the service from that angle.

 

 

.Net: simplest WCF service and client

In VS2015, I wanted to create the very simplest possible WCF service, and a client, in the form of a console app, to prove it works.

In VS, use the WCF template to create a WCF service library. As the template is a Hello World, I choose to call it HelloAcmeService (I don’t know why I’m randomly picking fictitious brands):

That creates an interface file, and a class file, which contain more than I want, so we strip those back to a minimum, and do some renaming.

Before:

HelloAcmeService02


HelloAcmeService03


HelloAcmeService04


HelloAcmeService05


 

After…

…removing the stuff I don’t want, and some renaming:

HelloAcmeService06


 

HelloAcmeService07


HelloAcmeService08


HelloAcmeService10

After an OK build let’s make sure we can reference it from the browser, by pressing Start in VS , and then clicking on the BaseAddress hyperlink  in the app.config (or just typing it in):

HelloAcmeService11

Now we could plug the WSDL into SoapUI and get a richer client experience (if those words mean nothing, that can be for another tutorial). For now, we’ll just create a console app to use the service.

Stop the service running in VS, so you can go back to editing. Create a console app under the solution (please note – there is in fact a compile error later using this name for reasons I am not going into here, and I won’t redo the screenshots – please call the console app HelloServiceAClient – note the ‘A’):

HelloAcmeService12

That gives you this new content in the Solution Explorer:

HelloAcmeService13

You now need to add a Service Reference to use the service from this client. Right click on References/Add Service Reference…, and in the dialogue, click Discover:

HelloAcmeService14

Click OK, and Solution Explorer now looks like this:

HelloAcmeService15

Now in Program.cs, add the text highlighted to access the service and use the GetData() method from the service:

HelloAcmeService16

The last action before testing it is to right-click on HelloServiceAClient and select Set as Startup Project.

HelloAcmeService17

Now press the Play button in VS, and you get this exciting output:

HelloAcmeService19

That’s it.