App.Config Layered Project

3

Gentlemen, I have a layered application, the business layer contains a reference to the WebServiceCorreios. In the App.config of the business layer 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 compiling the application the App.Config file of the GUI layer is being used.

I would like to use the class I created for the WebServiceCorreios in several other projects without having to change the App.Config every time it will reuse this class.

Is there any way the application can not use the project's App.Config set as "StartUp Project" and yes the project that contains the Webservice class?

    
asked by anonymous 20.06.2016 / 15:49

2 answers

2

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

But if you really want to do this, there are some forms, which I'll list below:

1. Set the ConfigurationFileMap.

This way you can change the default file where you will be getting the settings for 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.

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

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

    Thisformissimple,justaddthefixedvalueintheclass.

    4.Use SlowCheetah XML Transformation

    With this form you will use XML Transformation to do this.

    this extension for Visual Studio that might help you. In 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.

        
    20.06.2016 / 16:34
    0

    Would not you rather use the link that already contains ready-made webservice calls?

        
    20.06.2016 / 16:40