Search code examples
.netwcfapp-configconfigurationsection

Can you override system.serviceModel configuration section?


We are trying to create a "proxy" class that would serve the WCF service with its configuration properties.

Because we can't store those properties in the app.config file, i'm looking for a way to "proxy" it out and use custom configurationSection which would provide all these data upon request. In order to do so, i would need to tell the .NET framework to load system.serviceModel using my own ConfigurationSection, and i'm not sure i can do it.

If anyone can either approve my assumption, or even better, provide me with an idea how can i WCF configuration settings using another source, that would be helpful.

thanks


Solution

  • if you're looking for the way to create and register your wcf service in code pls check the code below for an example:

    below is a method to create the service host object:

    public static ServiceHost RegisterService<T>(object service, int port, string serviceName)
    {
        // Returns a list of ipaddress configuration
        IPHostEntry ips = Dns.GetHostEntry(Dns.GetHostName());
    
        // Select the first entry. I hope it's this maschines IP
        IPAddress ipAddress = ips.AddressList[0];
    
        // Create the url that is needed to specify where the service should be started
        string urlService = "net.tcp://" + ipAddress.ToString() + String.Format(":{0}/{1}", port, serviceName);
    
        // Instruct the ServiceHost that the type that is used is a ServiceLibrary.service1
        ServiceHost host = new ServiceHost(service);
        // define events if needed
        //host.Opening += new EventHandler(HostOpeningEvent);
        //host.Opened += new EventHandler(HostOpenedEvent);
        //host.Closing += new EventHandler(HostClosingEvent);
        //host.Closed += new EventHandler(HostClosedEvent);
    
        // The binding is where we can choose what transport layer we want to use. HTTP, TCP ect.
        NetTcpBinding tcpBinding = new NetTcpBinding();
        tcpBinding.TransactionFlow = false;
        tcpBinding.Security.Transport.ProtectionLevel = System.Net.Security.ProtectionLevel.EncryptAndSign;
        tcpBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows;
        tcpBinding.Security.Mode = SecurityMode.None; 
    
        // Add a endpoint
        host.AddServiceEndpoint(typeof(T), tcpBinding, urlService);
    
        // A channel to describe the service. Used with the proxy scvutil.exe tool
        ServiceMetadataBehavior metadataBehavior;
        metadataBehavior = host.Description.Behaviors.Find<ServiceMetadataBehavior>();
        if (metadataBehavior == null)
        {
            string httpURL = "http://" + ipAddress.ToString() + String.Format(":{0}/{1}", port + 1, serviceName);
    
            // This is how I create the proxy object that is generated via the svcutil.exe tool
            metadataBehavior = new ServiceMetadataBehavior();
            metadataBehavior.HttpGetUrl = new Uri(httpURL);
            metadataBehavior.HttpGetEnabled = true;
            metadataBehavior.ToString();
            host.Description.Behaviors.Add(metadataBehavior);
        }
        return host;
    }
    

    here's how you can use it:

    ServiceHost host = RegisterService<your_service_interface>(your_service, port, "yout_service_name");
    host.Open();
    

    hope this helps, regards