Search code examples
c#.netwcfconfiguration

Programatically configure individual WCF operations with different WCF configurations


I am just getting started with WCF and would like to set up a distributable networked system as follows: (but am not sure if it is possible.)

I have a .net client that has business logic. It will need various data from various sources so I would like to add a 'server' that contains an in-memory cache but also WCF capabilities to send/receive and publish/subscribe from data sources for data that is not cached. I think it should be possible for these server applications to be identical in terms of code, but highly configurable so that requests could be dealt with in a peer to peer fashion, or traditional client-server as required. I think it could be done so that essentially a server sends a request to wherever it has the endpoint configured and gets a response.

Essentially a server would be configured as below:

Server A
========

Operation 1 - Endpoint I
Operation 2 - Endpoint II


Server B
========

Operation 1 - Endpoint IV
Operation 2 - Endpoint III

The configuration would be stored for each server in app.config and loaded into memory at startup. So each WCF operation would have its own WCF config (in terms of endpoints etc.) and it would send particular requests to different places according to that configuration.

From what I have read of WCF I think this is possible. I don't know have enough experience to know if this is a standard WCF pattern that I am describing (if so please let me know). Otherwise, my main question is, how do I programatically configure each operation (as above) in WCF?

Please let me know if I have not explained myself clearly.

Thanks in advance for any help, Will


Solution

  • I don't know if this exactly will get you what you are looking for, but I this is what I use to add my WCF endpoints to my Windows Service. This is the code that the service runs to load all the wcf services:

    IDictionary<string, ServiceHost> hosts;
    NetTcpBinding binding;
    CustomBinding mexBinding;
    
    private void AddService(Type serviceImp, Type serviceDef, string serviceName)
        {
            ServiceHost host = new ServiceHost(serviceImp);
    
            string address = String.Format(baseAddress, wcfPort, serviceName); 
            
            string endAdd = address;
            string mexAdd = address + "/mex";
    
            ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
            host.Description.Behaviors.Add(behavior);
            host.AddServiceEndpoint(serviceDef, binding, endAdd);
            host.AddServiceEndpoint(typeof(IMetadataExchange), mexBinding, mexAdd);
    
            host.Open();
    
            hosts.Add(serviceDef.Name, host);
        }
    

    There's a baseAddress string that I didn't copy in, but it just has the net.tcp address for the endpoint. Likewise for the wcfPort. Different baseAddresses and ports are used for debug, testing and production.

    Just in case it isn't clear, serviceImp is the service implementation and serviceDef is the interface that defines the contract. Hope this helps.

    EDIT - Here are some references I used to help me figure all of this stuff out:

    Creating WCF Service Host Programmatically
    Net.Tcp Port Sharing Sample, Part 2
    Service Station: WCF Addressing In Depth