Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Store Tags

Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems.

Go Back   Vista Forums > Vista technology newsgroups > Indigo

Assigning behavior configuration dynamically using code

Reply
 
Thread Tools Display Modes
Old 01-22-2008   #1 (permalink)
Sherif ElMetainy
Guest
 
Posts: n/a

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

  Reply With Quote

Old 02-05-2008   #2 (permalink)
Newbie
royc is on a distinguished road
 
Join Date: Feb 2008
Vista Business
Posts: 1

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.
royc is offline   Reply With Quote
 
Reply

Thread Tools
Display Modes









Vistax64.com 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 2005-2008

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 47 48