Moving WCF service from IIS to a Windows service - wcf

We have an existing WCF service that makes use of wsDualHttpBinding to enable callbacks to the client. I am considering moving it to netTcpBinding for better performance, but I'm quite wary of moving away from the IIS-hosted service (a "comfort zone" we currently enjoy) into having our own Windows service to host it. I was hoping we could still host this on IIS 7 but Win2K8 won't be reality for us for some time.
What things should I watch out for when creating our own Windows service to host our WCF service? Things like lifetime management and request throttling are features that come free with IIS hosting so I'd also like to know how we can effectively host our service on our own without the convenience of having IIS do the hard work for us. Thanks! :)

So as you cannot host using WAS there are a couple of things to realise.
If the service crashes it doesn't restart by default (although you can change this in service properties)
IIS will recycle the application pool if it hangs or grows too big; you must do this yourself if you want the same sort of reliability.
You must create an account for the service to run under, or use one of the default services. Resit the temptation to run the service as SYSTEM or under an administrator account; if you want to use a built in account use NETWORK SERVICE.
It becomes harder to debug in situ.
Consider using a error logger such as log4net
Having said that I deployed a WCF/Windows service combination for a customer 9 months ago; it's heavily used and hasn't died once.
You can request throttle in a Windows service, it's part of the WCF configuration. Note the defaults are very low, it is likely you will have to increase these.

Hosting in a Windows Service Application (http://msdn.microsoft.com/en-us/library/ms734781.aspx) is a good start.
If you can host your service on Vista, you can also benefit from Windows Process Activation Service (WAS). WAS is a generalization of the IIS process activation, which can be used to activate processes over non-HTTP endpoints (TCP, Named Pipe, MSMQ). To learn more about WCF hosted in WAS, read http://msdn.microsoft.com/en-us/library/ms733109.aspx. To learn how to install and configure WAS, read http://msdn.microsoft.com/en-us/library/ms731053.aspx.

Related

Auto-start appfabric seems to work sequentially

I have a couple of wcf services which will call a registration service at startup (through a custom ServiceHostFactory). All these services (including the registration service) are running in the same app pool. I installed appfabric to auto-start all the services. I won't explain all the detailed settings (because I think it's not necessary yet), but in short, the services use net.tcp bindings and is running in IIS 7.5 on Windows 7.
The problem:
When I set all services to auto-start it will begin with one (random) service, this service tries to connect to the registration service. But somehow this registration service won't start-up. It looks like it wants to start all services sequentially.
The (not wanted) solution 1:
When I put the registration service in it's own app pool everything works fine and alle services come online in a matter of seconds and register themself.
The (not wanted) solution 2:
When I disable the auto-start and try to access one of the services after a iisreset, it all works fine. The service will try to register itself and the registration service in his turn will start.
I'm new to appfabric, so it might be some simple configuration issue. The weird thing though is that it won't work whith auto-start, but seems to work fine with a manual start.
Any idea's?
There is no priority in AppFabric Auto Start feature. You can't assume that one service will always be started before the others. Also, you can't assume they will start all at the same time.
For the specific scenario, it's recommanded to use WCF 4.0 Discovery and Announcement. Announcement feature enables service to announce their availability (by sending Hello and Bye announcements) whereas Discovery feature allow clients to discover service address at runtime.
All your services are on the same server ? could be pertinent to use a namedPipeBinding.

Failover mechanisum for WCF services serving enterprise application

We have set of WCF services running on single computer which collectively serves an WPF application which could be on same machine or on remote machine (within same network only). We need failover mechanisum so whenver any of the service crashes or hangs - we want to restart the service and initialize it by calling appropriate method.
Since we are not aware of what is the industry standard for implementing failover for WCF service - we have implemented like this way. We start main WCF service hosted in console app along with one more secondary WCF service which constantly checks health of main WCF service by calling exposed method on given endpoint. If main WCF service fails, it takes role of main WCF service and launches another secondary WCF service.
The above approach is working fine but only problem we have seen is memory since we launch services in pair and every host requires 10MB of memory.
Can anyone help me what is the industry practice for implementing failover for this kind of scenario?

Issues with WCF (net.tcp) hosted in IIS 7.5

I built a application with 5 wcf services and hosted them in IIS 7.5. I used the default configuration for the net.Tcp port (808*).
I am not used to host WCF services in IIS (I always hosted in Windows Services) and I found interesting that when I call the service (using tcp binding) I got two different process on the server.
One is SMSvcHost.exe (the one that is actually using the 808 port) and the other is w3wp.exe wich I think is handling a instance of the server I am calling.
I have a lot of questions so I will enumerate them:
Should I use IIS instead of Windows Service to host WCF Services (tcp binding) ?
The fact that I have two processes to answer my request means that I may have cpu impacts ?
Sometimes my services stop answering using the tcp binding. I got a timeout error on my clients but the mex is correctly answering if I go to the http://myServer/Service1.svc. I suspect that this problem is caused by fault connections but I am throwing exceptions correctly (using fault exceptions) and catching them correctly on my clients. Besides, I am also implementing a partial class for every service to dispose corretly (either using Close or Abort methods). Is there any way to figure out what's going on when the services stop answering ?
Shouldn't the w3wp.exe processes be closed after the client ends the request ? They remain on the Task Manager even when no one is using the services. I guess this is the reason of why my Entity Library logging locks the file after my request is completed.
Ideally it would be better hosted in Windows Activation Services (WAS) which is close to what you think of as IIS, but not quite. Here's a good introductory article on MSDN magazine:
http://msdn.microsoft.com/en-us/magazine/cc163357.aspx

Periodic operations in a Self-Hosted WCF Service using Timers

I know that it is not a good idea to have timers inside a WCF service class that is hosted inside IIS since these are meant to have short lifetimes. And from the advice here it also sounds like having a service is the best way to go for that situation.
But has anyone tried using timers inside a self-hosted service in production? We have a windows service that acts as a client and uses timers to do periodic operations at the moment.
This is fine for most cases, but I am concerned about the robustness of the design: some of the operations are critical (financial system calculation triggers). Since the WCF service and the windows service are two components, ensuring both are running is difficult.
If I moved the critical operations to a timer inside the WCF Service I remove that problem, but what else should I be concerned about then?
If I understand correctly, your question is actually about IIS-hosted WCF services, is that right?
IIS controls the application pool that your WCF service runs in. That means that IIS may decide to recycle your application pool and all the apps/services in it. Then your service only gets activated again once it is called by a client. So, scheduling in WCF services or ASP.NET applications cannot be relied on.
The picture of course changes if you can self-host your WCF service. Then there is no IIS application pooling to take into account, and you can schedule at will. Therefore, if you need the combination of WCF + scheduling, it's best to create a Windows service that will include both.

WCF Hosting Options Suggestion

I am looking for suggestion for hosting my WCF enterprise application.
The app. require to run without stopping at the server. It also use TCP to yield the best performance at the intranet environment.
I am thinking to host it at window service because IIS recycle process, and has timeout.
However, I find this from the msdn http://msdn.microsoft.com/en-us/library/ff649818.aspx :
Window service...Lack of enterprise features. Windows services do not have the security, manageability, scalability, and administrative features that are included in IIS.
Does it mean Window Service is not suitable for enterprise application? But How about MS SQL, Oracle, MySQL etc. They all host at Win. Service right?
Regards
Bryan
Windows service is suitable for enterprise application! The quoted text actually means that IIS has a lot of built-in management features which are not available in custom hosting (like windows service) unless you implement them at your own.
One of such features is the recycling you want to avoid which helps application to keep low resource consumption (server is in healthy state). Another such feature is IIS checking of the worker state. If worker process looks stuck (don't process requests for any reason), IIS will start automatically another process and routes new requests to that process.
IIS + WAS + AppFabric can provide very big feature set but they are not good for every scenario. If you have service which requires some background continuous, scheduled or multi threaded processing it is probably better to move to self hosted scenario.