Click here to Skip to main content
15,885,546 members
Articles / Programming Languages / C#
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
7 Jul 2014CPOL2 min read 10K   4  
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.

C#
ContractConcat.ConcatClient obj = new ContractConcat.ConcatClient();

App.Config contains:

XML
<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.

Image 1

Window Service Properties

That's all folks. Have a good day.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Engineer adobe
India India
Software developer and product designer, writer (both technical and non-technical)
Researcher at heart, engineer by mind Smile | :)

Comments and Discussions

 
-- There are no messages in this forum --