![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| 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 |
|
|
| |
| | #2 (permalink) |
| Newbie ![]() 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. |
| | |
| |
| |