Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9032985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:06:49+00:00 2026-06-16T08:06:49+00:00

I have this configuration in my app.config: </binding> </basicHttpBinding> </bindings> <behaviors> <serviceBehaviors> <behavior name=myBehavior>

  • 0

I have this configuration in my app.config:

    </binding>
  </basicHttpBinding>
</bindings>



<behaviors>
  <serviceBehaviors>
    <behavior name="myBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
</behaviors>

I want to expose this service programmatically from my desktop app:

I define the host instance:

ServiceHost host = new ServiceHost(typeof(MyType), new Uri("http://" + hostName + ":" + port + "/MyName"));

Then I add the endpoint with it’s binding:

var binding = new BasicHttpBinding("myBinding");
host.AddServiceEndpoint(typeof(IMyInterface), binding, "MyName");

Now, I want to replace the folowing code with some code that reads the behavior named myBehavior from the config file, and not hard-coding the behavior options.

ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };    
host.Description.Behaviors.Add(smb);   
// Enable exeption details
ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
sdb.IncludeExceptionDetailInFaults = true;

Then, I can open the host.

host.Open();

* EDIT *

Configuring Services Using Configuration Files

You shouldn’t need this way, you should make the host takes its configuration automagically from the config file, and not giving them manually, read this article (Configuring Services Using Configuration Files), it will help you, I have hosted my service in a single line in C# and few ones in config.

This is a second article about (Configuring WCF Services in Code),
my fault is that i was trying to mix the two ways!

I will add this as an answer.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T08:06:50+00:00Added an answer on June 16, 2026 at 8:06 am

    First, you need to open the configuration file by either using

    var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
    

    or

    var map = new ExeConfigurationFileMap();
    map.ExeConfigFilename = "app.config";
    var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
    

    Then you read the behaviors:

    var bindings = BindingsSection.GetSection(config);
    var group = ServiceModelSectionGroup.GetSectionGroup(config);
    foreach (var behavior in group.Behaviors.ServiceBehaviors)
    {
        Console.WriteLine ("BEHAVIOR: {0}", behavior);
    }
    

    Note that these are of type System.ServiceModel.Configuration.ServiceBehaviorElement, so not quite what you want yet.

    If you don’t mind using a private APIs, you can call the protected method BehaviorExtensionElement.CreateBehavior() via reflection:

    foreach (BehaviorExtensionElement bxe in behavior)
    {
        var createBeh = typeof(BehaviorExtensionElement).GetMethod(
            "CreateBehavior", BindingFlags.Instance | BindingFlags.NonPublic);
        IServiceBehavior b = (IServiceBehavior)createBeh.Invoke(bxe, new object[0]);
        Console.WriteLine("BEHAVIOR: {0}", b);
        host.Description.Behaviors.Add (b);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this App.config in my wcf service library: <?xml version=1.0?> <configuration> <appSettings> <add
I have this msbuild target in my csproj file: <Target Name=AfterBuild Condition= '$(Configuration)' ==
well i have a configuration like this in the components part of my config
I have a WCF service hosted in IIS7. This is the app.config for my
Configuration details are as follows: Web.config of Wcf service which is hosted. <basicHttpBinding> <binding
I have the following property: protected BasicHttpBinding Binding { get { var config =
I need to create an address string in app.config as: <client> <endpoint address=http://ServerName/xxx/yyy.svc binding=basicHttpBinding
I have this configuration: A page containing a search field, at the submit in
I have this configuration of nginx : server { listen 80; server_name example.com www.example.com;
In my DefaultRegistry I have this configuration: ForRequestedType<INHUnitOfWork>().CacheBy(InstanceScope.HttpContext) .TheDefault.Is.OfConcreteType<NHibernateUnitOfWork>(); At some point in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.