It had been very long since I had blogged. I had a peculiar requirement where I needed to parse the configuration of a WCF Service. that is the <System.ServiceModel> tag in the configuration file.
I finally came to know that there was a dedicated namespace System.ServiceModel.Configuration,
Following is code that helps read and update the configuration file
using system.ServiceModel;
using system.ServiceModel.Configuration;
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Read the Service Model Tag
ServiceModelSectionGroup SvCGroup = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(c);
//get the count of the number of Services and the service Model
Console.WriteLine("Number of Services: " + SvCGroup.Services.Services.Count);
Console.WriteLine("Number of ServiceBehaviours: " + SvCGroup.Behaviors.ServiceBehaviors.Count);
//iterate through the services in the config file
foreach (ServiceElement ele in SvCGroup.Services.Services)
{
//Name of the Service
Console.WriteLine("Service Name -" + ele.Name);
//The Host Tag
HostElement hele = ele.Host;
//iterate through the list of base addresses
foreach (BaseAddressElement belement in hele.BaseAddresses)
{
Console.WriteLine(" Base Address -" + belement.BaseAddress);
}
//get the endpoint details
foreach (ServiceEndpointElement sep in ele.Endpoints)
{
Console.WriteLine("------Endpoint -----------");
Console.WriteLine("Address -" + sep.Address.ToString());
Console.WriteLine("Binding -" + sep.Binding.ToString());
Console.WriteLine("Contract -" + sep.Contract);
Console.WriteLine("Changing Contract to IService2");
sep.Contract = "IService2";
Console.WriteLine("DNS -: " + sep.Identity.Dns.Value);
Console.WriteLine();
}
}
// get the list of ServiceBehaviour elements
foreach (ServiceBehaviorElement behavelement in SvCGroup.Behaviors.ServiceBehaviors)
{
Console.WriteLine("-------------ServiceBehaviour--------------");
foreach (BehaviorExtensionElement extele in behavelement)
{
//get the service metadata element
if (extele is ServiceMetadataPublishingElement)
{
Console.WriteLine("---------------------------MetaData Behaviour------------------");
ServiceMetadataPublishingElement behaviour1 = extele as ServiceMetadataPublishingElement;
Console.WriteLine("Behaviour Name: " + behaviour1.ConfigurationElementName);
Console.WriteLine("Property HttpGetEnabled: " + behaviour1.HttpGetEnabled);
}
}
you can also set back the properties and call the configuration.Save(); to change the config file
c.SaveAs("ConsoleApplication1.exe.config", ConfigurationSaveMode.Modified);
this saves on the modified parts of the configuration class. how ever 2 other overloads are provided.
I finally came to know that there was a dedicated namespace System.ServiceModel.Configuration,
Following is code that helps read and update the configuration file
using system.ServiceModel;
using system.ServiceModel.Configuration;
Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
//Read the Service Model Tag
ServiceModelSectionGroup SvCGroup = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(c);
//get the count of the number of Services and the service Model
Console.WriteLine("Number of Services: " + SvCGroup.Services.Services.Count);
Console.WriteLine("Number of ServiceBehaviours: " + SvCGroup.Behaviors.ServiceBehaviors.Count);
//iterate through the services in the config file
foreach (ServiceElement ele in SvCGroup.Services.Services)
{
//Name of the Service
Console.WriteLine("Service Name -" + ele.Name);
//The Host Tag
HostElement hele = ele.Host;
//iterate through the list of base addresses
foreach (BaseAddressElement belement in hele.BaseAddresses)
{
Console.WriteLine(" Base Address -" + belement.BaseAddress);
}
//get the endpoint details
foreach (ServiceEndpointElement sep in ele.Endpoints)
{
Console.WriteLine("------Endpoint -----------");
Console.WriteLine("Address -" + sep.Address.ToString());
Console.WriteLine("Binding -" + sep.Binding.ToString());
Console.WriteLine("Contract -" + sep.Contract);
Console.WriteLine("Changing Contract to IService2");
sep.Contract = "IService2";
Console.WriteLine("DNS -: " + sep.Identity.Dns.Value);
Console.WriteLine();
}
}
// get the list of ServiceBehaviour elements
foreach (ServiceBehaviorElement behavelement in SvCGroup.Behaviors.ServiceBehaviors)
{
Console.WriteLine("-------------ServiceBehaviour--------------");
foreach (BehaviorExtensionElement extele in behavelement)
{
//get the service metadata element
if (extele is ServiceMetadataPublishingElement)
{
Console.WriteLine("---------------------------MetaData Behaviour------------------");
ServiceMetadataPublishingElement behaviour1 = extele as ServiceMetadataPublishingElement;
Console.WriteLine("Behaviour Name: " + behaviour1.ConfigurationElementName);
Console.WriteLine("Property HttpGetEnabled: " + behaviour1.HttpGetEnabled);
}
}
you can also set back the properties and call the configuration.Save(); to change the config file
c.SaveAs("ConsoleApplication1.exe.config", ConfigurationSaveMode.Modified);
this saves on the modified parts of the configuration class. how ever 2 other overloads are provided.
No comments:
Post a Comment