Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

WCF and Windows Services: Object Initialization of WCF Client Proxy Takes Too Much Time

0.00/5 (No votes)
7 Jul 2014 1  
How to avoid delay in Object Initialization of WCF client proxy in a Window service

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

  1. Go to Run window.
  2. Type Service.msc, press OK.
  3. Select your service, right click on it, select Properties.
  4. On second tab, "Log On", select option this account.
  5. Type your current user (or admin user) credentials, then click apply.

Window Service Properties

That's all folks. Have a good day.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here