App.Config Layered Project

Asked

Viewed 445 times

3

Gentlemen, I have a layered application. In the business layer App.config I had to add the following code:

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="AtendeClienteServiceSoapBinding">
                <security mode="Transport" />
            </binding>
            <binding name="AtendeClienteServiceSoapBinding1" />
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="https://apps.correios.com.br/SigepMasterJPA/AtendeClienteService/AtendeCliente"
            binding="basicHttpBinding" bindingConfiguration="AtendeClienteServiceSoapBinding"
            contract="WebServiceCorreios.AtendeCliente" name="AtendeClientePort" />
    </client>
</system.serviceModel>

It turns out that when I build the application the App.Config file of the GUI layer is being used.

I would like to use the class I created for Webservice to run several other projects without having to change the App.Config every time I reuse this class.

Is there any way that the application does not use the App.Config of the project set as "Startup Project" and yes of the project that contains the Webservice class?

2 answers

2


As you said yourself, the file App.config using is that of Startup Project. Generally changing this practice is not a good idea, because it can generate future problems, even more if you are working in a team.
I say this because you are wanting to change a standard functionality, that is, other people may not know it, or you may end up forgetting it yourself in the future.

But if you really want to do that, there are a few ways, which I will list below:

1. Defining the Configurationfilemap.

This way you can change the default file from where you will be getting the settings for the App.config.

ConfigurationFileMap fileMap = new ConfigurationFileMap(file); //Path to your config file
Configuration configuration = ConfigurationManager.OpenMappedMachineConfiguration(fileMap);
string value = configuration.AppSettings.Settings["key1"].Value;

2. Add the file as a link.

This way you will add the App.config as if it were an external file. To do this, just follow the steps below:

  1. Right-click your project on Solution Explorer
  2. Select "Add" -> "Existing Item..."
  3. Navigate to the file you want to add to the solution
  4. Instead of pressing Enter or clicking the Add button, you want to click the down arrow icon on the right end of the Add button and select "Add As Link".

inserir a descrição da imagem aqui

3.Add the value in class instead of the App.config.

This form is simple, just add the fixed value in the class.

4. Use the Slowcheetah XML Transformation

With this form you will use the XML Transformation for that reason.

There is a this extension for Visual Studio who can assist you.
At this link You find an excellent explanation of what it is and how to use it. References:

There are other ways to do this, but the simplest ones I know are these.

  • Thank you Randrade, also agree on the generation of future problems. I will think more about it and post an issue on how I will solve. Thank you again very much.

0

  • duardbr, I can even use, however as a beginner I would like to develop my own class for educational purposes, and in the future if it presents itself really useful, make it available to the community.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.