Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 890
  • Last Modified:

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

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
anton_ts
Asked:
anton_ts
  • 3
  • 3
1 Solution
 
avarmaavarmaCommented:
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
 
anton_tsAuthor Commented:
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
 
avarmaavarmaCommented:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
anton_tsAuthor Commented:
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
 
avarmaavarmaCommented:
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
 
anton_tsAuthor Commented:
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

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now