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 8179403
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:59:33+00:00 2026-06-06T23:59:33+00:00

We have a WCF REST service that connects to a database. In fact, we

  • 0

We have a WCF REST service that connects to a database. In fact, we have several instances of the database, all with the same schema.

We would like to set up one endpoint for each database instance and associate a connection string with the endpoint. The service would read the connection string and connect to the appropriate SQL Server instance.

I’m sure this is possible; is it a good idea? How do I set it up? Is there documentation on MSDN?

Edit: I found this question, where the answer suggests adding connection information on the client in a header. I don’t want to do that—for security reasons, and because I do want to have a distinct uri for each database.

  • 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-06T23:59:35+00:00Added an answer on June 6, 2026 at 11:59 pm

    This was a bit harder than I thought. WCF has so many extensibility points its hard to pick the right one. Please answer or comment if you think there’s a better way, or anything wrong with this.

    I’ve settled on using a custom class that implements IEndpointBehavior and IDispatchMessageInspector. I have a class derived from BehaviorExtensionElement that lets me associate the behavior with an endpoint in configuration. This blog post describes hot do do that.

    My DatabaseConnectionContext class looks like this:

    /// <summary>
    /// An endpoint behavior that associates a database connection string name with the endpoint and adds it to the
    /// properties of incoming messages.
    /// </summary>
    public class DatabaseConnectionContext : IEndpointBehavior, IDispatchMessageInspector
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseConnectionContext"/> class with the provided connection string name.
        /// </summary>
        /// <param name="connectionStringName">The name of the connection string to associate with the endpoint.</param>
        public DatabaseConnectionContext(string connectionStringName)
        {
            this.ConnectionStringName = connectionStringName;
        }
    
        /// <summary>
        /// Gets the name of the connection string to associate with the endpoint.
        /// </summary>
        public string ConnectionStringName { get; private set; }
    
        /// <inheritdoc />
        public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
        {
        }
    
        /// <inheritdoc />
        public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
        {
            throw new NotImplementedException();
        }
    
        /// <inheritdoc />
        public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
        }
    
        /// <inheritdoc />
        public void Validate(ServiceEndpoint endpoint)
        {
        }
    
        /// <inheritdoc />
        public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
        {
            request.Properties["connectionStringName"] = this.ConnectionStringName;
            return null;
        }
    
        /// <inheritdoc />
        public void BeforeSendReply(ref Message reply, object correlationState)
        {
        }
    }
    

    In my service class I have this method:

        /// <summary>
        /// Returns the connection string to use for this service call.
        /// </summary>
        /// <returns>A SQL Server database connection string.</returns>
        private string GetConnectionString()
        {
            string connectionStringName = (string)OperationContext.Current.IncomingMessageProperties["connectionStringName"];
            return ConfigurationManager.ConnectionStrings[connectionStringName].ConnectionString;
        }
    

    My BehaviorExtensionElement class looks like this:

    /// <summary>
    /// Associates a <see cref="DatabaseConnectionContext"/> with an endpoint in configuration.
    /// </summary>
    public class DatabaseConnectionContextBehaviorExtension : BehaviorExtensionElement
    {
        /// <summary>
        /// The name of the <see cref="ConnectionStringName"/> property when it appears in a configuration file.
        /// </summary>
        private const string ConnectionStringNamePropertyName = "connectionStringName";
    
        /// <summary>
        /// Gets or sets the name of the configuration string to associate with the endpoint.
        /// </summary>
        [ConfigurationProperty(ConnectionStringNamePropertyName)]
        public string ConnectionStringName
        {
            get
            {
                return (string)this[ConnectionStringNamePropertyName];
            }
    
            set
            {
                this[ConnectionStringNamePropertyName] = value;
            }
        }
    
        /// <inheritdoc />
        public override Type BehaviorType
        {
            get { return typeof(DatabaseConnectionContext); }
        }
    
        /// <inheritdoc />
        protected override object CreateBehavior()
        {
            return new DatabaseConnectionContext(this.ConnectionStringName);
        }
    }
    

    My web.config contains something like this:

    <behaviors>
    
      <endpointBehaviors>
        <behavior name="DevRestEndpointConfiguration">
          <webHttp helpEnabled="false" />
          <connectionStringInterceptor connectionStringName="myDevConnectionStringName" />
        </behavior>
        <behavior name="ProductionRestEndpointConfiguration">
          <webHttp helpEnabled="false" />
          <connectionStringInterceptor connectionStringName="myProductionConnectionStringName" />
        </behavior>
      </endpointBehaviors>
    
    </behaviors>
    
    <extensions>
      <behaviorExtensions>
        <add name="connectionStringInterceptor" type="DatabaseConnectionContextBehaviorExtension, MyAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
      </behaviorExtensions>
    </extensions>
    

    Each <endpoint /> element in the <services /> section has its behaviorConfiguration set to the name of an appropriate element from the <endpointBehaviors /> section.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a REST service that I would like to require client certificates. The
I have a WCF REST service. I would like to write integration tests for
I have an asp.net 4 application that hosts a WCF REST service via WebServiceHost...
I have a C# REST Service in WCF that sits on top of an
I have developed a sample WCF REST service that accepts that creates an Order
I have a self hosted WCF Rest service that I am using to simulate
I have a REST WCF service that is being hosted in IIS. I have
I have a WCF REST service. I verified Webconfig file and all config items
I have a WCF REST web service that is hosted via a service route
I have a WCF WebApi Rest service that has the following endpoints: [WebGet(UriTemplate =

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.