Home Blog Updating Web.Config Programmatically to Add Services References for Deployments & Publishing

Updating Web.Config Programmatically to Add Services References for Deployments & Publishing

Gerhard van den Heever | January 26, 2021

In the past few projects, I have found that a lot of 3rd party application still use services references as a means to integrate into their systems. For one, it works very well to add the reference to your development project and work with objects as we are all fond of doing. The disadvantage is that you would need to included the reference to your web.config, when exporting your project from your development environment to production.

A quick fix to this is to always update the web.config file on the server. However, the issue is that you may not always have ready access to the server for whatever reason – the most common being that IT controls access and updates to production environments. Or perhaps you would be able to monitor when changes occur – that your customization would still function. In the event the application does receive updates or the web.config reverts back to the original settings file, the configurations can be added back by re-publishing the customization without having to access the web.config file.

After spending some time, and trolling the internet, there is a way to include web.config changes on our customization, and ensure that when it is published, missing references would always be added.

The first step is to add a customization & bind the project.

When you have bound the project, you will now need to create a class and add the ServiceModel references to your project:

GIST: https://gist.github.com/Gerhard-ZA/3d6c7e39deefc54d570e40f78f3a1859

Tip – Add the System.Web.dll, System.ServiceModel.dll & System.Configuration.dll in order to add the references – and don’t forget to add the service reference!

When you have added your DLL & references, you are now able to manage and update your web.config. The sample code here provides a good example:

GIST: https://gist.github.com/Gerhard-ZA/0902c102d21cfaacc6ef1e2391e50900

Lastly, you need to call your code from the plug-in extender you have created:

GIST: https://gist.github.com/Gerhard-ZA/3d6c7e39deefc54d570e40f78f3a1859

The full class .cs file would look something like this:

GIST: https://gist.github.com/Gerhard-ZA/e4db9f18a8cbc8ab3e5da000c1f278bc

Now, you are able to use the service reference in your code for the required tasks, and the plug-in will take care of deploying the references to the required sites.

I hope you have found this information useful.

Happy coding!

Gerhard van den Heever

Gerhard is the Technical Director at Astraia Technology (VAR) & FBA Software (ISV). He is responsible for the successful creation and delivery of the organization’s solutions deployment and products for Acumatica. He has over 10 years’ experience developing software in the ERP space and typically involved as a system’s architect during projects. Being born and raised in Africa, Gerhard enjoys going out to nature and takes every opportunity to go on a safari with his family or enjoy the Winelands of the Cape.

Categories: Developers

Subscribe to our bi-weekly newsletter