这是NserviseBus总线的代码,我在运行该代码时遇到错误。 错误" mscorlib.dll无法提取' MySqlPassword'来自环境变量"请帮助我。 谢谢....
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);
}
}
如何解决mscorlib.dll无法提取&#39; MySqlPassword&#39;来自环境变量..? 请帮帮我..
答案 0 :(得分:0)
鉴于该错误消息似乎来自您的代码,即来自
var password = Environment.GetEnvironmentVariable("root");
if (string.IsNullOrWhiteSpace(password))
{
throw new Exception("Could not extract 'MySqlPassword' from Environment variables.");
}
我猜测没有root
环境变量或者有一个环境变量,并且它被设置为空字符串。
另外,鉴于您稍后尝试从同一环境变量中读取用户名,我认为这里出了问题。
答案 1 :(得分:0)
代码试图从计算机的环境变量集合中提取用户名和密码。根据运行环境的操作系统,这些设置将有所不同。
如果您在Windows中运行,则可以通过搜索&#34;编辑系统环境变量&#34;来编辑环境变量。在开始菜单中。
或者,您可以直接在代码中设置变量来跳过环境变量访问:
// ...
var password = "root";
var username = "root";
var connection = $"server=localhost;user={username};database=sqlpersistencesample;port=3306;password={password};AllowUserVariables=True;AutoEnlist=false";
// ...