WCF: What is a ServiceHost? - c#

As I'm currently learning to use WCF Services, I am constantly encountering tutorials on the internet which mention using a ServiceHost when using a WCF Service.
What exactly is this ServiceHost ?
In my current project I am using a WCF Service and having a reference to it from my app and whenever I want to consume it from my app I just instantiate its ServiceClient like such:
new MusicRepo_DBAccess_ServiceClient(new InstanceContext(instanceContext), customBinding, endpointAddress);
And then access my web methods (OperationContracts) from that instance (obviously opening it before consuming the method and closing it afterwards with Open and Close)
My WCF service is host in my IIS and I just access the .svc from my app to instantiate the ServiceClient.
So why and where is ServiceHost used?

A ServiceHost basically provides you everything you need to host a WCF service in a non-IIS or WAS setting. A common place for a ServiceHost would be in a console app or Windows service. See the example code from MSDN for how to setup a ServiceHost in a console app.

Your service implementation is just a .NET class - you need to have a runtime environment for it, so it can be executed somehow. That's what the ServiceHost is for - it will load your service class, set up the endpoints and channel listeners and all that stuff, and thus give your service class an "ecosystem" to live and operate in.
You can either instantiate a ServiceHost class yourself in a console app, a Windows service, or even a Winforms app, and thus make your WCF service class available to the outside world - or you can delegate that work to IIS or WAS. Even IIS or WAS will use a ServiceHost to host your WCF service - they just do it automagically behind the scenes, and "on demand" - whenever a request for your WCF service comes in.
Marc

Related

dynamically add the base address of wcf service at run time?

i have created wcf service and client. both wcf service and client are implemented using c#. i have hosted this wcf service in both iis and windows service. while i am hosting it, i need to specify the base address either in web.config file or in design time. is it possible to specify base address at run time. how to provide it at run time?
like this can i change the service reference address at client side. how to do it in c# ?
If you're using self-hosting, then you can specify the base address in the constructor of the ServiceHost class:
Uri baseAddress = new Uri("http://........");
ServiceHost host = new ServiceHost(typeof(YourServiceClass), baseAddress);
With IIS, things get a bit trickier.... you might be able to provide a custom factory to create your service host - but that seems like a whole lot of work. In addition, with IIS, the virtual directory where your *.svc file exists really dictates the URL of your service - so providing a base address really isn't of much value.

can't find main static main method in WCF

I created WCF service and faced with a problem. I need to update database periodically, but i couldn't find static method like Main, that whould do it without client interaction. What can i do??? What wold you suggest in such case?
There is no Main method (or similar entry point) in WCF. You need to host your WCF service in another process (such as a Windows service, or IIS or self host) to "activate" it and make it available to other processes.
One of the concepts in WCF is that you write your service code to do the function you need without having to worry about infrastructure and hosting. Once you have written your service logic, you can then decorate and configure your service to expose it to other processes. Using this approach means you can change how your service is exposed to other processes without re-writing the actual service logic - you essentially just change your configuration. Hence, a main entry point is specific to how you choose to host and expose your WCF service to the outside world.
Just Google around for "WCF hosting" and you will find lots of information.
If you don't need to expose your service logic to an external process (which sounds like maybe the case from your question) then maybe you don't need to use WCF and you can just write a plain old Windows Service.
If your wcf service is self hosted then you can do it in your application before publishing the service.
If it is in IIS then there really isn't application_start kind of thing since the host may be created on first request. See WCF application start event

How to assess a Windows Service hosted WCF service from another WCF service hosted in IIS7?

I have done a work which contains two parts
WCF service which is hosted in Windows Service
WCF service project hosted in IIS7
I have done both comfortably,But i got a problem?
I want to call a method from WCF service hosted in WS from the 2nd WCF service hosted in IIS, but i was unable to do that? Can anybody help in this?
You should be able to configure a client endpoint in the service being hosted in IIS to the service being hosted as a Windows service, and make your call through a proxy just as you would normally make a call to a WCF service.
Well, if your service hosted in IIS7 wants to call the other service, it needs to become a client of that other service.
So basically, in Visual Studio or on the command line, you need to create a service reference to your second service, so that you'll get a client-side proxy class, which you can then use to call the second service. Also, you will need to add some configuration settings to your web.config for the service hosted in IIS7 in order for it to be able to call the other service (in a Windows service).
So where exactly are you stuck??

calling a WCF service object method in another WCF service

I using two WCF services. WCF service A is hosted in my .NET Winform application and WCF Service B is hosted on a Windows Service.
I am able to instantiate a client for WCF Service B and use the methods - i.e. call the WCF service hosted on Windows service from the .NET Winform app.
I am not able to accomplish the reverse with WCF Service A - i.e. call the WCF Service hosted on the .NET Winform application from the Windows Service. The call to the method times out.
I have used the WCF Test client from the Visual Studio command prompt and it can successfully make calls to WCF Service A.
Is this due to a security issue or something from the Windows Service?
Please advise.
Thanks in advance!
Subbu
I think the only viable approach (without the extreme of having some messaging infrastructure), is to have the service invoke operations back on your client via a WCF callback. A good example of this can be found here:
What steps do I need to take to use WCF Callbacks?
This is good for dealing with events that happen server side and allowing the client to respond to them. If events isn't what you're looking for, then your client could just register with the server (specifying the callback contract), and then the server can invoke your client at will.

Can you communicate between a WCF service and the Windows service hosting it?

I have a WCF service I use for configuration stuff hosted in a windows service that will be used to maintain a database. Is there any way that I can access the WCF service inside of the hosting service? Or should I move the database functionality to another WCF service and host them both inside of a windows service?
The service class (the one implementing your service interface) has a property
OperationContext.Current.Host
which gives you access to the ServiceHost instance which is hosting your service. You can access that service host pretty easily.
There's no built-in way to reach beyond the service host and manipulate or query the NT service containing the service host. But you could always create your own custom ServiceHost descendant class which would give you the necessary access to the NT service itself, and then use that custom service host for your service implementation.
With a custom service host, you can basically do whatever you feel is necessary and useful - just create a descendant from ServiceHost and do whatever you need to do!
If by "access" you mean to ask whether you can call the service, then yes, the service can be a client of itself.
What might be better would be for you to separate the service into those parts that are specific to the fact that it's a web service, and all the other parts that do the real work. Have the Windows Service call the latter parts.
The way I do this is to pass a shared object into the constructor of the WCF Service by using a custom InstanceProvider (this allows you to use non-default constructors for the WCF Service).
The shared object then allows the WCF Service to "talk" with the Windows Service (or any other object that can get access the shared object).

Categories