/// <summary> /// Gets an encrypted configuration value. /// </summary> /// <param name="key">Name of the configuration item. Key value is case sensitive.</param> /// <param name="defaultValue">Default value to return.</param> /// <returns>Value of the configuration setting.</returns> /// <exception cref="InvalidOperationException">The value of the configuration item is not encrypted.</exception> unsafe public SecureString GetEncryptedConfigurationValue(string key, string defaultValue) { Guard.ArgumentNotNullOrWhitespace(key, nameof(key)); // Check if the configuration provider is null, if it is return the default value. if ((null == _configSection) || (false == _configSection.Parameters.Contains(key))) { char[] chars = defaultValue.ToCharArray(); fixed(char *pChars = chars) { return(new SecureString(pChars, chars.Length)); } } // Get the property and return the value. If the property isn't encrypted, an InvalidOperationException will be thrown. ConfigurationProperty property = _configSection.Parameters[key]; if (true == property.IsEncrypted) { return(property.DecryptValue()); } else { // Return the defaultValue, wrapped in a SecureString object; // do not convert to char[] to allow for empty default strings; // empty source arrays return NULL when pinned below, resulting // in ArgumentNullException (in SecureString ctor). fixed(char *pChars = defaultValue) { return(new SecureString(pChars, defaultValue.Length)); } } }
/// <summary> /// Creates a new <see cref="ServiceInstanceListener"/> capable of listening to a queue containing /// WCF queued messages for the given <typeparamref name="TServiceContract"/>. /// </summary> /// <param name="wcfServiceObject"> /// The WCF service instance to be used for servicing requests. Must be a stateless service. Must not be null. /// </param> /// <param name="netMessagingBinding"> /// The binding to be used to listen to the queue. Must not be null. /// </param> /// <param name="configPackageName"> /// The name of the configuration package used to retrieve configuration values. If null, the default /// value is "Config". /// </param> /// <param name="sectionName"> /// The name of the configuration section used to retrieve configuration values. If null, the default /// value is "ServiceBus". /// </param> /// <param name="listenConnectionStringParameterName"> /// The name of the configuration value that provides a Service Bus connection string with Listen privileges. /// If null, the default is "ListenConnectionString". /// </param> /// <param name="queueNameProvider"> /// A <see cref="Func{TResult}"/> returning the name of the queue to be used. If null, the default behavior /// will be to use the name of <typeparamref name="TServiceContract"/>. /// </param> /// <param name="endpointBehaviors"> /// The <see cref="IEndpointBehavior"/>s that are to be applied to the generated WCF <see cref="ServiceHost"/> /// endpoints. /// </param> /// <typeparam name="TServiceContract"> /// The interface that bears a <see cref="ServiceContractAttribute"/> and <see cref="OperationContractAttribute"/>s /// on its methods to denote it as a ServiceModel (WCF) service. /// </typeparam> /// <returns> /// A new <see cref="ServiceInstanceListener"/> configured to listen to an Azure Service Bus for WCF /// messages for the given <typeparamref name="TServiceContract"/> service contract. Guaranteed not to /// be null. /// </returns> /// <exception cref="ArgumentNullException"> /// Thrown if <paramref name="wcfServiceObject"/> or <see cref="NetMessagingBinding"/> are null. /// </exception> /// <exception cref="InvalidOperationException"> /// Thrown if the endpoint for the Service Bus queue's containing Azure Service Bus namespace could /// not be resolved from the connection string. /// </exception> public static ServiceInstanceListener CreateQueuedServiceBusListener <TServiceContract>( TServiceContract wcfServiceObject, NetMessagingBinding netMessagingBinding, string configPackageName = "Config", string sectionName = "ServiceBus", string listenConnectionStringParameterName = "ListenConnectionString", Func <string> queueNameProvider = null, params IEndpointBehavior[] endpointBehaviors) { if (wcfServiceObject == null) { throw new ArgumentNullException(nameof(wcfServiceObject)); } if (netMessagingBinding == null) { throw new ArgumentNullException(nameof(netMessagingBinding)); } string ResolveConnectionStringFromSFParameters(ServiceContext statelessServiceContext) { if (statelessServiceContext == null) { throw new ArgumentNullException(nameof(statelessServiceContext)); } ConfigurationProperty configurationProperty = statelessServiceContext.CodePackageActivationContext .GetConfigurationPackageObject(configPackageName).Settings.Sections[sectionName] .Parameters[listenConnectionStringParameterName]; return(configurationProperty.IsEncrypted ? new NetworkCredential("junk", configurationProperty.DecryptValue()).Password : configurationProperty.Value); } return(new ServiceInstanceListener( createCommunicationListener: context => ResolveCommunicationListener( context: context, wcfServiceObject: wcfServiceObject, netMessagingBinding: netMessagingBinding, queueNameProvider: queueNameProvider, connectionStringResolver: ResolveConnectionStringFromSFParameters, behaviors: endpointBehaviors ) )); }