Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > Indigo

Vista - Assigning behavior configuration dynamically using code

 
 
Old 01-22-2008   #1 (permalink)
Sherif ElMetainy


 
 

Assigning behavior configuration dynamically using code

Greetings,

I am writing an application where I use ChannelFactory to create client
channels. I want to be able to choose one of the endpoint behaviors defined
in my config file. I didn't find any API to apply an endpoint behavior
configutation to my factory's service endpoint. I don't have any client
endpoints defined in the application config file. These are created
dynamically from code. I came up with a solution which seems to work (I
haven't extensively tested it yet) but I don't feel comfortable using it.
The code for my solution is below:

ChannelFactory<IMyContract> channelFactory = new
ChannelFactory<IMyContract>();
ApplyEndpointBehaviorConfiguration(behaviorConfigurationName,
channelFactory.Endpoint); /* this method implemention shown below*/

static void ApplyEndpointBehaviorConfiguration(string
behaviorConfigurationName, ServiceEndpoint serviceEndPoint) {
/* open application configuration */
Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
/* get behaviors configuration section */
BehaviorsSection behaviorsSection =
ServiceModelSectionGroup.GetSectionGroup(config).Behaviors;
/* get endpoint behavior element from configuration */
EndpointBehaviorElement endpointBehaviorElement =
behaviorsSection.EndpointBehaviors[behaviorConfigurationName];
/* clear existing behaviors because ClientCredentials would cause errors
because it is already defined */
serviceEndPoint.Behaviors.Clear();
/* Get CreateBehavior method using reflection because it is a protected
internal virtual method */
MethodInfo createBehaviorMethodInfo =
typeof(BehaviorExtensionElement).GetMethod("CreateBehavior",
BindingFlags.Instance | BindingFlags.NonPublic);
foreach (BehaviorExtensionElement behaviorExtensionElement in
endpointBehaviorElement) {
/* Loop the behavior extensions and call CreateBehavior for each */
serviceEndPoint.Behaviors.Add((IEndpointBehavior)createBehaviorMethodInfo.Invoke(behaviorExtensionElement,
null));
}
}


The reasons I am not happy with this solution are
- Using reflection to access and call non public methods can cause problems
when running in non full trust scenarios (not a big deal in my case, but my
code is likely to be reused in other projects where this might be a problem)
- Most importantly, I am not using the CreateBehavior method they way it is
intended to be used since it is supposed to be called by the WCF runtime and
not from my code. I am worried about any undefined behavior or unexpected
hard to fix bugs this might cause.
- I feel I am shooting myself in the foot by using this unsupported solution
that might not work in future versions (CreateBehavior method is protected
virtual and not private though, so it would be a breaking change if it is
removed or changed in future versions)

I would greatly appreciate if someone can guide me to a cleaner and
supported way to do this.

Sorry for the long post, and thanks a lot in advance.

Best regards,
Sherif El-Metainy


My System SpecsSystem Spec
Old 02-06-2008   #2 (permalink)


Vista Business
 
 

Re: Assigning behavior configuration dynamically using code

Hi, any luck finding a better solution yet? I'm in the exact same situation where I feel kind of forced to use this protected internal CreateBehavior method.

There seems to be no elegant way to combine the programmatic creation of channels and configurating parts of it from config file.
My System SpecsSystem Spec
 

Thread Tools


Similar Threads
Thread Forum
dynamically create variable? PowerShell
Creating a hashtable dynamically PowerShell
Smart-quote behavior: Is current behavior ideal? PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46