Search code examples
c#mysqlasp.netnservicebus

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


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..


Solution

  • 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";
    // ...