Skip to content

Instantly share code, notes, and snippets.

@Gerhard-ZA
Last active January 27, 2021 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Gerhard-ZA/0902c102d21cfaacc6ef1e2391e50900 to your computer and use it in GitHub Desktop.
Save Gerhard-ZA/0902c102d21cfaacc6ef1e2391e50900 to your computer and use it in GitHub Desktop.
Update WebConfig Method
public void updateWebConfig()
{
var configuration = WebConfigurationManager.OpenWebConfiguration("~");
ServiceModelSectionGroup secgroup = (ServiceModelSectionGroup)configuration.GetSectionGroup("system.serviceModel");
bool hasBasicPartyService = false;
bool hasWsPartyService = false;
bool hasUpdate = false;
//Verify if the BasicHttpBinding element exist
foreach (BasicHttpBindingElement binding in secgroup.Bindings.BasicHttpBinding.Bindings)
{
if (binding.Name == "BasicHttpBinding_PartyServ")
hasBasicPartyService = true;
}
//Verify if the WSHttpBinding element exist
foreach (BasicHttpBindingElement binding in secgroup.Bindings.BasicHttpBinding.Bindings)
{
if (binding.Name == "WSHttpBinding_PartyServ")
hasWsPartyService = true;
}
//Basic Service
if (hasBasicPartyService == false)
{
secgroup.Bindings.BasicHttpBinding.Bindings.Add(CreateBasicHttpBinding("BasicHttpBinding_PartyServ",
BasicHttpSecurityMode.Transport, HttpClientCredentialType.None));
secgroup.Client.Endpoints.Add(CreateEndPoint("BasicHttpBinding_PartyServ", "https://ws.domain.com/Party/Service.svc",
"basicHttpBinding", "BasicHttpBinding_INPartyServ", "NewParty.ServiceRef"));
hasUpdate = true;
}
//WS Service
if (hasWsPartyService == false)
{
secgroup.Bindings.WSHttpBinding.Bindings.Add(CreateWSHttpBinding("WSHttpBinding_PartyServ",
SecurityMode.Transport, HttpClientCredentialType.None));
secgroup.Client.Endpoints.Add(CreateEndPoint("WSHttpBinding_PartyServ", "https://ws.domain.com/Party/Service2.svc",
"wsHttpBinding", "WSHttpBinding_PartyServ", "NewParty.ServiceRef2"));
hasUpdate = true;
}
if (hasUpdate == true)
{
configuration.Save();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment