Solved

WCF in Windows Service. How do I change the endpoint address using app.config and apply an updated data to wcf in windows service?

Posted on 2010-08-24
6
850 Views
Last Modified: 2013-11-27
Hello!

I'd developed a WCF Service using netTCPBinding and embedded it in a Windows Service.

The configuration data of wcf is located in app.config file.

After that i had created an installation project and include my code (wcf in widows service) there. So I have the setup.exe file to install my windows service with wcf on board.

I've installed the windows service to my local drive (C:\Programs Files\...\Service) and started it using the services page (pane) of my OS.

It works fine.

The app.config file was copied to the local drive and located across the same folder (C:\Programs Files\...\Service).

One day i've tried to change the part of my endpoint address (PORT) in app.config (C:\Program Files\...\app.config) and resterted the service using an appropriate page of services (services.msc) in windows.

I thought that the service would be working on another port (I've setted in app.config) but i was wrong. The service continued working on the past PORT. As I've understood, it continued working using the copy of starting version of app.config. It store the copy of this file in another path.

 I need the ability to change the app.config file and get the updated windows service after it's restarting.

I am going to let my users set the specified port number where the developed wcf service will listening to.

Where am I wrong? Could you help me to find the solution of my problem?

Thank you for your help.


0
Comment
Question by:anton_ts
  • 3
  • 3
6 Comments
 
LVL 4

Expert Comment

by:avarmaavarma
ID: 33516650
I am guessing you are reading the config file in your OnStart method - using the name "app.config" for the config file.  If so, you need to use a different name - the name you need to use is YOURSVCNAME.EXE.CONFIG

If you are not specifying the filename, you could read the settings directly as shown in the code below:
// app.config
<appSettings>
    <add key="Portnumber" value="8000" />
 </appSettings>

// to get the value of the Portnumber key
string value = ConfigurationManager.AppSettings["Portnumber"];


// to get ALL values

foreach(string key in ConfigurationManager.AppSettings)
        {
           string value = ConfigurationManager.AppSettings[key];
         }

Open in new window

0
 

Author Comment

by:anton_ts
ID: 33518671
Avarma, thanks a lot for your help, but you are not right.

The configuration data of wcf is located in app.config file. I don't read data from config file in OnStart() method and don't do it at all.

I use app.config (external) to configure my wcf service. I store this file in the same project with wcf and copy it to output folder (using installation project).

I want to configure my wcf with app.config. That's why i need to change data in this file only.

If I'll take your idea => I'll have to configure wcf manually in code using so methods as AddEndpoint, but i don't want to do it. As additional solution- you are right, but i need a bit another.
0
 
LVL 4

Expert Comment

by:avarmaavarma
ID: 33522574
So if I understand correctly, your app.config is correctly read by the WCF service the very first time it starts up. However, when you make changes to the app.config, the changes are not picked up?
Is that correct?
Also - you mention that you do not read data from the config file at all. How are you getting the portNumber (in the app.config) data into your web service class?

Thanks
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:anton_ts
ID: 33522853
Yes, you are absolutely correct. The changes aren't pick up.

But, as i said, i don't read data from the config file. The WCF do it for me itself automatically.

WCF works fine after instalation and listening to predifined port which I've setted during deveping process. I haven't to get port manually and include it to my web-service.

I wanna change data in app.config and get an updated configuration after my windows service restarting.

May be I change another config file? I've copied my app.config to output folder but i have AppName.exe.config at the same folder.
0
 
LVL 4

Accepted Solution

by:
avarmaavarma earned 125 total points
ID: 33523469
Yes - app.config is a meaningless name outside of Visual Studio. When the service is deployed, the 'app.config' is renamed to AppName.exe.config.  Also - ensure that when your right click the properties on the app.config - it is set to 'Copy if Newer'

I am curious as to what your app.config in the project looks like. Does it have the following hierarchy of XML nodes (see a sample config file in the code snippet attached)?

Thanks

<configuration>
  <system.serviceModel>    
       <services>
        <service name="Microsoft.ServiceModel.Samples.CalculatorService"
               behaviorConfiguration="...">
            <host>
   ....
<?xml version="1.0" encoding="utf-8" ?>

<configuration>

  <system.serviceModel>    <services>

      <!-- This section is optional with the new configuration model

           introduced in .NET Framework 4. -->

      <service name="Microsoft.ServiceModel.Samples.CalculatorService"

               behaviorConfiguration="CalculatorServiceBehavior">

        <host>

          <baseAddresses>

            <add baseAddress="http://localhost:8000/ServiceModelSamples/service"/>

          </baseAddresses>

        </host>

        <!-- this endpoint is exposed at the base address provided by host: http://localhost:8000/ServiceModelSamples/service  -->

        <endpoint address=""

                  binding="wsHttpBinding"

                  contract="Microsoft.ServiceModel.Samples.ICalculator" />

        <!-- the mex endpoint is exposed at http://localhost:8000/ServiceModelSamples/service/mex -->

        <endpoint address="mex"

                  binding="mexHttpBinding"

                  contract="IMetadataExchange" />

      </service>

    </services>

    <behaviors>

      <serviceBehaviors>

        <behavior name="CalculatorServiceBehavior">

          <serviceMetadata httpGetEnabled="true"/>

          <serviceDebug includeExceptionDetailInFaults="False"/>

        </behavior>

      </serviceBehaviors>

    </behaviors>

  </system.serviceModel>

</configuration>

Open in new window

0
 

Author Closing Comment

by:anton_ts
ID: 33523680
It's really that app.config was renamed to AppName.exe.config. If i change data in this file and restart the service it change the behavior. It's great.

My app.config lokks like your's.

Thank you for your help a lot.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

A short article about problems I had with the new location API and permissions in Marshmallow
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now