views:

159

answers:

3

Hi,

I've currently written code to use the ServiceContractGenerator to generate web service client code based on a wsdl, and then compile it into an assembly in memory using the code dom. I'm then using reflection to set up the binding, endpoint, service values/types, and then ultimately invoke the web service method based on xml configuration that can be altered at run time.

This all currently works fine. However, the problem I'm currently running into, is that I'm hitting several exotic web services that require lots of custom binding/security settings. This is forcing me to add more and more configuration into my custom xml configurations, as well as the corresponding updates to my code to interpret and set those binding/security settings in code.

Ultimately, this makes adding these 'exotic' services slower, and I can see myself eventually reimplementing the 'system.serviceModel' section of the web or app.config file, which is never a good thing.

My question is, and this is where my lack of experience .net and C# shows, is there a way to define the configuration normally found in the web.config or app.config 'system.serviceModel' section somewhere else, and at run time supply this to configuration to the web service client?

Is there a way to attach an app.config directly to an assembly as a resource or any other way to supply this configuration to the client?

Basically, I'd like attach an app.config only containing a 'system.serviceModel' to the assembly containing a web service client so that it can use its configuration. This way I wouldn't need to handle every configuration under the sun, I could let .net do it for me.

Fyi, it's not an option for me to put the configuration for every service in the app.config for the running application.

Any help would be greatly appreciated.

Thanks in advance! Bryan

+1  A: 

Are your proxy classes deriving from ClientBase<T>? If so, then there is a constructor that accepts a Binding and an EndpointAddress. You should be able to use those instead of the corresponding configuration data.

John Saunders
Hi, I'm currently using that constructor, and I'm configuring the Binding and EnpointAddress based on the configurations set in my custom xml configuration. However, it's becoming cumbersome to support the massive number of potential binding/security configurations that is provided by default in the app.config 'system.serviceModel' section. I was hoping that I could leverage this instead of reinventing the wheel. A very very big wheel. Thanks for your answer!
Bryan
In effect, you want to reduce the amount of data necessary to specify the required configurations. That means you'll have to look at your configurations and note what's in common between them and what's different between them. You might then want to refactor these into a hierarchy of custom bindings, where each level only changes slightly from the level above. Use the appropriate custom bindings in the constructors. Some other pattern may suggest itself once you've identified the commonalities and the differences.
John Saunders
+1  A: 

The following couple links talk about loading WCF Configuration settings from config files other than the app.config. May be what you are looking for but not certain.

http://blogs.msdn.com/dotnetinterop/archive/2008/09/22/custom-service-config-file-for-a-wcf-service-hosted-in-iis.aspx

http://weblogs.asp.net/cibrax/archive/2007/10/19/loading-the-wcf-configuration-from-different-files-on-the-client-side.aspx

Tallek
Thanks for the links, I was able to use the second one to load in a config file other than the one defined for the application.
Bryan
+2  A: 
  • Create a custom class deriving from ChannelFactory.
  • Override the protected CreateDescription method. In the override, you need to...
  • Call base.CreateDescription().
  • Read in your custom configuration.
  • Create a custom ServiceEndpoint based on your configuration. Don't forget the bindings, behaviors, etc.
  • Return that custom ServiceEndpoint.

More details HERE

Incognito