Skip to content

Commit

Permalink
Only parse the connectionstring for simple configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees Schollaart committed Apr 1, 2018
1 parent 57ccfad commit 18df005
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ This repository contains the code for the CaseOnline.Azure.WebJobs.Extensions.Mq
- Password (optional)
- Port (optional, defaults to 1883)
- ClientId (optional, defaults to a random Guid)

For example:

```
Expand All @@ -26,7 +27,7 @@ This repository contains the code for the CaseOnline.Azure.WebJobs.Extensions.Mq
}
```

- When deploying/running on Azure set/ad the application-setting ```FUNCTIONS_EXTENSION_VERSION``` to ```beta```
- When deploying/running on Azure, make sure the application-setting ```FUNCTIONS_EXTENSION_VERSION``` to ```beta```.
- Add a ```MqttTrigger``` attribute to your function parameters:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class AttributeToConfigConverter
private readonly MqttTriggerAttribute _mqttTriggerAttribute;
private readonly INameResolver _nameResolver;
private readonly ILogger _logger;
private readonly DbConnectionStringBuilder _connectionString;

/// <summary>
/// Initializes a new instance of the <see cref="AttributeToConfigConverter"/> class.
Expand All @@ -39,12 +38,6 @@ public class AttributeToConfigConverter
/// <param name="logger">The logger.</param>
public AttributeToConfigConverter(MqttTriggerAttribute source, INameResolver nameResolver, ILogger logger)
{
var connectionStringKey = nameResolver.Resolve(source.ConnectionString) ?? nameResolver.Resolve(DefaultAppsettingsKeyForConnectionString);
_connectionString = new DbConnectionStringBuilder()
{
ConnectionString = connectionStringKey
};

_mqttTriggerAttribute = source;
_nameResolver = nameResolver;
_logger = logger;
Expand All @@ -65,8 +58,14 @@ public MqttConfiguration GetMqttConfiguration()

private MqttConfiguration GetConfigurationViaAttributeValues()
{
var connectionStringKey = _nameResolver.Resolve(_mqttTriggerAttribute.ConnectionString) ?? _nameResolver.Resolve(DefaultAppsettingsKeyForConnectionString);
var connectionString = new DbConnectionStringBuilder()
{
ConnectionString = connectionStringKey
};

var port = DetaultMqttPort;
var connectionStringHasPort = _connectionString.TryGetValue(ConnectionStringForPort, out var portAsString);
var connectionStringHasPort = connectionString.TryGetValue(ConnectionStringForPort, out var portAsString);
if (connectionStringHasPort && !string.IsNullOrEmpty(portAsString as string))
{
var canParsePortFromConnectionString = int.TryParse(portAsString as string, out port);
Expand All @@ -76,19 +75,19 @@ private MqttConfiguration GetConfigurationViaAttributeValues()
}
}

var clientId = _connectionString.TryGetValue(ConnectionStringForClientId, out var clientIdValue) && !string.IsNullOrEmpty(clientIdValue as string)
var clientId = connectionString.TryGetValue(ConnectionStringForClientId, out var clientIdValue) && !string.IsNullOrEmpty(clientIdValue as string)
? clientIdValue.ToString()
: Guid.NewGuid().ToString();

var server = _connectionString.TryGetValue(ConnectionStringForServer, out var serverValue)
var server = connectionString.TryGetValue(ConnectionStringForServer, out var serverValue)
? serverValue.ToString()
: throw new Exception("No server hostname configured, please set the server via the MqttTriggerAttribute, using the application settings via the Azure Portal or using the local.settings.json"); ;

var username = _connectionString.TryGetValue(ConnectionStringForUsername, out var userNameValue)
var username = connectionString.TryGetValue(ConnectionStringForUsername, out var userNameValue)
? userNameValue.ToString()
: null;

var password = _connectionString.TryGetValue(ConnectionStringForPassword, out var passwordValue)
var password = connectionString.TryGetValue(ConnectionStringForPassword, out var passwordValue)
? passwordValue.ToString()
: null;

Expand Down

0 comments on commit 18df005

Please sign in to comment.