2

I have a C# solution in Visual Studio 2005. There is a web reference in one project that I have to change when I have to build the project for a determined environment, manually deleting the reference from Visual Studio, and adding a new one with the same name, but changing the url.

I would like to make this manual process of adding the web reference not so tedious.

I would like to have in the configuration manager, besides debug and release configurations, development, pre-production and production configurations, each of them determining the correct web-reference associated to the specific environment.

I was thinking of creating a preBuild event (a .bat file maybe), where depending on the Build Configuration, the web reference would be created correctly.

I am not sure about how to do this. I have some questions about it:

1) I could create different "app.config" files, where the properties

<setting name="MyService_serviceagent" serializeAs="String">
      <value>developmentEnvironmentURL</value>
</setting>

would be different depending on the environment, and I could copy the right app.config file depending on the selected configuration.

Would this re-generate the Reference.cs file from the web reference? (I guess not). Would this be a problem even if the interface has not changed, or it would be enough if I regenerate the webreference manually only when I want to re-generate the class?

2) Is a bad idea to do this ? Do you think of any alternatives, or is it easier to just accept that I should delete and add the corresponding web reference when I want to build my solution pointing to a determined environment?

Thank you for reading : )

Regards.

pablof
  • 313
  • 2
  • 4
  • 18
  • Run WSDL.exe to (re)generate proxy classes. (http://msdn.microsoft.com/en-us/library/7h3ystb6(v=vs.80).aspx) – Dmitry Apr 23 '13 at 09:39
  • One solution is to use MSBuild XslTransformation Task in post build event. Then create one xsl file for every configuration. Set url transformation in every xsl file. – emigue Apr 23 '13 at 10:12
  • 1
    @emigue Thank you. But it seems that this MSBuild xslTransformation only works from VS 2012. – pablof Apr 23 '13 at 13:20

1 Answers1

4

There is a web reference in one project that I have to change when I have to build the project for a determined environment, manually deleting the reference from Visual Studio, and adding a new one with the same name, but changing the url. [...] I was thinking of creating a preBuild event where the web reference would be created correctly.

I don't think that is either a good or maintainable solution. Can you explain why you think you need to re-add the reference if all that changes is the URL? Why can't you just change the service URL in the web.config?

Anyway if you must have two separate references then add them both, so each service reference can have its own configuration. Ultimately you can put those service references in a separate library, which you reference from the main project. Then in the project that needs the services you choose in code which implementation to use (this can be dependent on per-project environment variables for example, so you can determine it compile-time, or just a configuration value so the proper service gets selected at runtime).

A third solution could be to not create a reference, but utilize ClientBase<T> where you create a proxy in run-time.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks for answering. But having to choose in code which implmentation to use should not be an option, as the selected environment is a compile and deployment decision, but not a feature that should be coded. I think that it's something that should be configured in the Build process. I'd like to have a way to re-add the reference because the second url, could be pointing to another environment's database (production instead of development, for instance). Everything should be the same, but the recompilation of the program for each environment with the same version of the code could be normal. – pablof Apr 23 '13 at 13:06
  • You still haven't explained why re-adding the service reference is better than just changing the URL in the web.config. – CodeCaster Apr 23 '13 at 13:37
  • (I think I don't have a web.config, I have an app.config. Is it the same?). If I have to change the URL, I re-add the service reference, because the chance of error (copy and paste of the url), is lesser if I select add web reference, paste the url in the bar, click on Go, and if it does not let me click on "Add reference", then the reference is down, or it is incorrect, and I see that error before than if I paste the url in the config file. Thank you for your time CodeCaster : ) – pablof Apr 23 '13 at 13:48