I am using google text to speech and everything works in debug. when I push to production, if the method is called by a user using web frontend, everything works but if the method is called under HangFIre scheduler, I get an error saying object reference is not set to an instance of an object on any line. Does anybody know which account Hangfire scheduler run under and if it has something to do with it, how to fix it. Here is the code that fails on first line if scheduler does the scheduling.
public static void TextToVoice(string text, Guid guid)
{
//Fails right here asp.net object reference not set to an instance of an object
string path1 = @"C:\inetpub\Email\files\tcx1-d73434edb97e.json"; //HttpContext.Current.Server.MapPath("~/app_data/tcx1-d73434edb97e.json");
if (Environment.GetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS") == null)
System.Environment.SetEnvironmentVariable("GOOGLE_APPLICATION_CREDENTIALS",path1);
var client = TextToSpeechClient.Create();
var input = new SynthesisInput
{ Text = text };
var voiceSelection = new VoiceSelectionParams
{
LanguageCode = "en-US",
SsmlGender = SsmlVoiceGender.Female
};
var audioConfig = new AudioConfig
{
AudioEncoding = AudioEncoding.Mp3
};
var response = client.SynthesizeSpeech(input, voiceSelection, audioConfig);
using (var output = File.Create(HttpContext.Current.Server.MapPath("~/files/" + guid.ToString() + ".mp3")))
{
response.AudioContent.WriteTo(output);
}
}