0

This is code for NserviseBus bus i got error when running that code. Error "mscorlib.dll Could not extract 'MySqlPassword' from Environment variables"please help me. Thank you....

using System;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
using NServiceBus;
using NServiceBus.Persistence.Sql;

class Program
{
    static async Task Main()
    {
        Console.Title = "Samples.SqlPersistence.EndpointMySql";

        #region MySqlConfig

        var endpointConfiguration = new EndpointConfiguration("Samples.SqlPersistence.EndpointMySql");

        var transport = endpointConfiguration.UseTransport<MsmqTransport>();

 transport.Transactions(TransportTransactionMode.SendsAtomicWithReceive);

        var persistence = endpointConfiguration.UsePersistence<SqlPersistence>();

        var password = Environment.GetEnvironmentVariable("root");
        if (string.IsNullOrWhiteSpace(password))
        {
            throw new Exception("Could not extract 'MySqlPassword' from Environment variables.");
        }
        var username = Environment.GetEnvironmentVariable("root");
        if (string.IsNullOrWhiteSpace(username))
        {
            throw new Exception("Could not extract 'MySqlUserName' from Environment variables.");
        }
        var connection = $"server=localhost;user= 
        {username};database=sqlpersistencesample;port=3306;password= 
        {password};AllowUserVariables=True;AutoEnlist=false";
        persistence.SqlDialect<SqlDialect.MySql>();
        persistence.ConnectionBuilder(
        connectionBuilder: () =>
            {
                return new MySqlConnection(connection);
            });
        var subscriptions = persistence.SubscriptionSettings();
        subscriptions.CacheFor(TimeSpan.FromMinutes(1));

        #endregion

        endpointConfiguration.UseTransport<LearningTransport>();
        endpointConfiguration.EnableInstallers();

        var endpointInstance = await Endpoint.Start(endpointConfiguration)
        .ConfigureAwait(false);

        Console.WriteLine("Press any key to exit");
        Console.ReadKey();

        await endpointInstance.Stop()
            .ConfigureAwait(false);
    }
 }

how to solve mscorlib.dll Could not extract 'MySqlPassword' from Environment variables..? Please help me..

Suramya Patel
  • 81
  • 1
  • 1
  • 8

2 Answers2

0

Given that that error message seems to originate from your code, namely from

        var password = Environment.GetEnvironmentVariable("root");
        if (string.IsNullOrWhiteSpace(password))
        {
            throw new Exception("Could not extract 'MySqlPassword' from Environment variables.");
        }

I'm going to guess that there's either no root environment variable or that there is one and it's set to the empty string.

Also, given that you're later trying to read the username from the same environment variable, I think something has gone wrong here.

Daniel McLaury
  • 4,047
  • 1
  • 15
  • 37
0

The code is trying to extract username and password from the computer's environment variables collection. These will be set differently depending on the operating system the environment is running in.

If you are running in Windows, you can edit the environment variables by searching for "Edit the system environment variables" in the start menu.

Alternatively you can skip the environment variable access by just setting the variables directly in the code:

// ...
var password = "root";
var username = "root";
var connection = $"server=localhost;user={username};database=sqlpersistencesample;port=3306;password={password};AllowUserVariables=True;AutoEnlist=false";
// ...
Mike Minutillo
  • 54,079
  • 14
  • 47
  • 41