Introduction
Today, every application is expected to be run on its own without the help of users, and window services provide a solution to that problem. Also in distributed environment, WCF provides an almost perfect solution for network calls and data transfer. When both are used together, it provides benefits in many cases.
But when in Window Service, WCF Client object is initialized, it may happen that it takes times to be initialized (usually it takes 15-20 second). Developer may think that network is causing the delay. But the actual problem lies with the Windows 7 user rights. When we initialize the object, it actually establishes a collection over the network to secure a communication passage, but it requires authentication. WCF provides its own level of authentication and user can change its setting via CONFIG file, but it does not serve the purpose in all cases. Windows Service is one such case when an extra level of authentication is performed by the application.
Problem: Object Initialization takes too much time by WCF object initialization in a Windows service application.
Assumption: Developer has basic knowledge of WCF client Proxy and Window Services.
Using the Code
Suppose I have Contract ConCat and I have added its proxy reference to a project which is being used inside a Window Service project.
ContractConcat.ConcatClient obj = new ContractConcat.ConcatClient();
App.Config contains:
<client>
<endpoint address="http://127.0.0.1:4040/ServerCode" binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_IConcat" contract="ContractConcat.IConcat"
name="WSHttpBinding_IConcat">
<identity>
<userPrincipalName value="AnantBeriwal-PC\Anant Beriwal" />
</identity>
</endpoint>
</client>
User may experience a wait of several seconds in this single point of statement. The call to initialize object of WCF client will wait until window does not authenticate it. However, this can be skipped. User only has to provide proper rights to Windows Service.
Solution
The problem lies with the privileges that are being set by OS on the Windows services. WCF has its own authentication but windows service requires separate authentication if a network connection is established.
Providing User Rights
- Go to Run window.
- Type Service.msc, press OK.
- Select your service, right click on it, select Properties.
- On second tab, "Log On", select option this account.
- Type your current user (or admin user) credentials, then click apply.
Window Service Properties
That's all folks. Have a good day.