WCF Client Configuration: how can I check if endpoint is in config file, and fallback to code if not?
Asked Answered
I

2

11

Looking to make a Client that sends serialized Message objects back to a server via WCF.

To make things easy for the end-developer (different departments) would be best that they didn't need to know how to edit their config file to set up the client end point data.

That said, would also be brilliant that the endpoint wasn't embedded/hard-coded into the Client either.

A mix scenario would appear to me to be the easiest solution to roll out:

IF (described in config) USE config file ELSE fallback to hard-coded endpoint.

What I've found out is:

  1. new Client(); fails if no config file definition found.
  2. new Client(binding,endpoint); works

therefore:

Client client;
try {
  client = new Client();
}catch {
  //Guess not defined in config file...
  //fall back to hard coded solution:
  client(binding, endpoint)
}

But is there any way to check (other than try/catch) to see if config file has an endpoint declared?

Would the above not fail as well if defined in config file, but not configured right? Would be good to distinguish between the two conditions?

Id answered 17/6, 2009 at 22:4 Comment(0)
A
8

here is the way to read the configuration file and load the data into an easy to manage object:

Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup csg = c.GetSectionGroup("system.serviceModel");
if (csg != null)
{
    ConfigurationSection css = csg.Sections["client"];
    if (css != null && css is ClientSection)
    {
        ClientSection cs = (ClientSection)csg.Sections["client"];
        //make all your tests about the correcteness of the endpoints here
    }
}

The "cs" object will expose a collection named "endpoints" that allows you to access all the properties that you find in the config file.

Make sure you also treat the "else" branches of the "if"s and treat them as fail cases (configuration is invalid).

Aussie answered 18/6, 2009 at 6:41 Comment(2)
Thanks Alex for figuring out a solution. (Wish MS would add these kinds of methods to the Framework itself.)Id
The problem (for me) with Alex' approach is that Silverlight doesn't seem to have the System.Configuration DLL... :( So you can't read the clientconfig in an easy way. Anyone who has any ideas on how to do this in Silverlight, /without/ hardwiring stuff into the code? Thanks in advance!Esperanto
V
11

I would like to propose improved version of AlexDrenea solution, that uses only special types for configuration sections.

Configuration configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
        ServiceModelSectionGroup serviceModelGroup = ServiceModelSectionGroup.GetSectionGroup(configuration);
        if (serviceModelGroup != null)
        {
            ClientSection clientSection = serviceModelGroup.Client;
            //make all your tests about the correcteness of the endpoints here

        }
Vambrace answered 14/4, 2010 at 16:16 Comment(1)
+1 Nice. LINQified: if ( serviceModelGroup != null && serviceModelGroup .Client.Endpoints.Cast<ChannelEndpointElement>().Any( e => e.Contract == "Services.IContract" ) ) using ( var contract = new ContractClient() ) { } else use normalHouk
A
8

here is the way to read the configuration file and load the data into an easy to manage object:

Configuration c = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSectionGroup csg = c.GetSectionGroup("system.serviceModel");
if (csg != null)
{
    ConfigurationSection css = csg.Sections["client"];
    if (css != null && css is ClientSection)
    {
        ClientSection cs = (ClientSection)csg.Sections["client"];
        //make all your tests about the correcteness of the endpoints here
    }
}

The "cs" object will expose a collection named "endpoints" that allows you to access all the properties that you find in the config file.

Make sure you also treat the "else" branches of the "if"s and treat them as fail cases (configuration is invalid).

Aussie answered 18/6, 2009 at 6:41 Comment(2)
Thanks Alex for figuring out a solution. (Wish MS would add these kinds of methods to the Framework itself.)Id
The problem (for me) with Alex' approach is that Silverlight doesn't seem to have the System.Configuration DLL... :( So you can't read the clientconfig in an easy way. Anyone who has any ideas on how to do this in Silverlight, /without/ hardwiring stuff into the code? Thanks in advance!Esperanto

© 2022 - 2024 — McMap. All rights reserved.