Answered by:
Configuring WCF Services in Code WCF 4.5

Question
-
User-1716690740 posted
Hi am trying to configure wcf using code below is the code
public static void Configure(ServiceConfiguration config)
{
string configPath=ConfigurationManager.AppSettings["wcfconfigDBPath"];
// Enable “Add Service Reference” support
config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true });
// set up support for http, https, net.tcp, net.pipe
if (isEnabled(configPath, "enablehttp"))
config.EnableProtocol(new BasicHttpBinding());
if (isEnabled(configPath, "enablenettcp"))
config.EnableProtocol(new NetTcpBinding());
if (isEnabled(configPath, "enablepipe"))
config.EnableProtocol(new NetNamedPipeBinding());
// add an extra BasicHttpBinding endpoint at http:///basic
//config.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "basic");
}private static bool isEnabled(string path,string elementName)
{
try
{
string elementValue = string.Empty;
bool returnVal = false;
using (XmlTextReader reader = new XmlTextReader(path))
{reader.ReadToFollowing(elementName);
if(reader.Read())
elementValue = reader.Value;
}
if (!string.IsNullOrEmpty(elementValue))
{
bool.TryParse(elementValue, out returnVal);
}
return returnVal;
}
catch(Exception ex)
{
return false;
}
}Am trying the enable/disable protocol using a XMl file ; Am not sure when "static void Configure" gets fired ; Is there any way to do it ?
Tuesday, August 13, 2013 3:44 PM
Answers
-
User260886948 posted
Hi,
>>Am not sure when "static void Configure" gets fired . Is there any way to do it?
You can put the method static void Configure in service like below:
public class Service1 : IService1 { public static void Configure(ServiceConfiguration config) { string configPath=ConfigurationManager.AppSettings["wcfconfigDBPath"]; // Enable “Add Service Reference” support config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); // set up support for http, https, net.tcp, net.pipe if (isEnabled(configPath, "enablehttp")) config.EnableProtocol(new BasicHttpBinding()); if (isEnabled(configPath, "enablenettcp")) config.EnableProtocol(new NetTcpBinding()); if (isEnabled(configPath, "enablepipe")) config.EnableProtocol(new NetNamedPipeBinding()); // add an extra BasicHttpBinding endpoint at http:///basic //config.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "basic"); } }
For more information, please try to refer to:
#Configuring WCF Services in Code:
http://msdn.microsoft.com/en-us/library/hh205277.aspx .Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 14, 2013 6:00 AM -
User-1662538993 posted
You can use the code Amy send us and have a bit field in your database.
For Example -
If that bit field is 0 means disable the protocol.
You can read the value from db or xml and then depends on value put your logic to enable/disable.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 15, 2013 5:28 PM
All replies
-
User260886948 posted
Hi,
>>Am not sure when "static void Configure" gets fired . Is there any way to do it?
You can put the method static void Configure in service like below:
public class Service1 : IService1 { public static void Configure(ServiceConfiguration config) { string configPath=ConfigurationManager.AppSettings["wcfconfigDBPath"]; // Enable “Add Service Reference” support config.Description.Behaviors.Add(new ServiceMetadataBehavior { HttpGetEnabled = true }); // set up support for http, https, net.tcp, net.pipe if (isEnabled(configPath, "enablehttp")) config.EnableProtocol(new BasicHttpBinding()); if (isEnabled(configPath, "enablenettcp")) config.EnableProtocol(new NetTcpBinding()); if (isEnabled(configPath, "enablepipe")) config.EnableProtocol(new NetNamedPipeBinding()); // add an extra BasicHttpBinding endpoint at http:///basic //config.AddServiceEndpoint(typeof(IService), new BasicHttpBinding(), "basic"); } }
For more information, please try to refer to:
#Configuring WCF Services in Code:
http://msdn.microsoft.com/en-us/library/hh205277.aspx .Best Regards,
Amy Peng- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, August 14, 2013 6:00 AM -
User-1716690740 posted
Amy , Thanks for the link and info. I dont see a problem in making that code work.
My question is there any way to enable/disable protocol beased on DB/xml configuration without bringing down the service
Thursday, August 15, 2013 10:05 AM -
User-1662538993 posted
You can use the code Amy send us and have a bit field in your database.
For Example -
If that bit field is 0 means disable the protocol.
You can read the value from db or xml and then depends on value put your logic to enable/disable.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Thursday, August 15, 2013 5:28 PM