Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
887 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
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 

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 375 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
Starting up a Project
Loops Section Overview

972 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