0

I have been through WCF and related topics, created my first WCF service. It works fine but the problem is that I don't understand hosting concept.

Different tutorials do different things like some create separate console applications for it to host service then using it in Asp.net app but some doesn't host it in any place and just add reference to another project and use it.

I don't understand that when to host where and why?

Please help me in this issue. I am using Visual studio 2013 with .net 4 and asp.net c#.

Ion Aalbers
  • 7,830
  • 3
  • 37
  • 50
user3432348
  • 353
  • 1
  • 5
  • 9
  • Read this article to understand the hosting and consuming WCF service - http://msdn.microsoft.com/en-us/library/bb332338.aspx – Venki Mar 24 '14 at 07:07
  • I think you should read this.. http://stackoverflow.com/questions/1204365/what-is-the-difference-between-wcf-service-application-and-wcf-service-library – Sanu Uthaiah Bollera Mar 24 '14 at 07:08
  • "some doesn't host it in any place and just add reference to another project and use it." That's a misunderstanding and you must correct it before moving on. Without hosting, a WCF service won't work. You probably don't know how to check who hosts it in some scenarios, but it must be hosted. – Lex Li Mar 24 '14 at 09:16

2 Answers2

1

Basically, a WCF service needs to be hosted somewhere, so that it can be accessed from somewhere else. There are several ways to do this (and probably more than I know about too), but two of the most simple and common ways are to host the service in IIS Express, or in IIS (Internet Information Server).

IIS Express

The simplest way to achieve the first (IIS Express), is to simply right click the project in Visual Studio, and select View in Browser. That will open a directory listing in your browser, and in that directory you should see a file ending in .svc. Clicking that file should open the service description page with text like:

You have created a service.

To test this service, you will need to create a client (...)

The URL to that page, is in effect the URL clients will need to connect to your service. It should look something like http://localhost:64835/YourServiceName.svc.

That means the service is hosted locally, at port 64835, and accessible for clients at that address. Since this is in IIS Express however, it will no longer be accessible once you've closed Visual Studio, since it only runs as part of it.

IIS proper

Hosting in IIS means your service will be accessible whenever IIS is running. Once installed, it will usually start up when you log in, and run silently in the background. When it is running, you can start your service simply by accessing the correct URL. If it is not running, it might take a few seconds to start it. The next time it is called, it should respond quickly.

Note that in IIS, an application will by default run on port 80, which is the default port checked by browsers and possibly other clients - which means you don't need to specify it as in the example above. The URL will therefor generally be something more simple, like http://localhost/yourservice/yourservice.svc (although you could configure it to another port, or another protocol (say https://.., or something else if you like).

Once configured, and the relevant port is open however, your service should be accessible to the rest of the world.


Note: From the outside world, the URL will be different; it could be something like:

  • http://123.456.789.123/yourservice/yourservice.svc, if that is your IP address, or
  • http://yourdomain.com/yourservice/yourservice.svc if you've set up a domain.
Kjartan
  • 18,591
  • 15
  • 71
  • 96
  • 1
    Next to that you also have the following hosting options => WAS, Windows Services, Regular Console App, If you want to go wild even a WPF/Winforms application can be used to host WCF services. – woutervs Mar 24 '14 at 08:37
  • Yep, exactly. :) I just mentioned these two since I would guess they are the most common, and are also useful to make a point of the difference between temporary and more permanent hosting of an app. – Kjartan Mar 24 '14 at 08:41
  • great, amazing, fabulous explanation. – user3432348 Mar 24 '14 at 09:40
  • + do i need web.config, app.config to set up manually or it automatically does ? – user3432348 Mar 24 '14 at 09:40
  • @woutervs if i host it in console app then it would remain in running state always ? like you said for asp.net app – user3432348 Mar 24 '14 at 09:55
  • In a console app it would remain in running state as long as the console app is running, thus no. If you want it to be in running state as long as the machine runs, you can better implement it in a Windows Service. – woutervs Mar 24 '14 at 09:59
  • 1
    lemme sum it up to clarify my confused mind. 1. create service 2. Host it in some pace where it can be accessed any time (e.g. in windows service of purchased server's OS) 3. So if somebody e.g. you want to use it then you will just need to add ref to your project and then all yours right ? – user3432348 Mar 24 '14 at 10:07
0

I would suggest you consider hosting under two categories:

  1. Self Hosting
  2. IIS Hosting

As per you question "when to host where and why", I would say that you self host a WCF service only during testing. Mostly, self hosting is not used in live/production environments. For production environments use ISS hosting.

Self-hosting would be useful for testing on the local machine and on the intranet, while IIS hosting would be useful over the internet.

However, one must be aware that there is no rule as such regarding the usage of a particular hosting technique in any particular situation. With experience, the developer will be the best judge.

AnkitMittal
  • 166
  • 2
  • 18