c# - ASP.NET Web 应用程序 - 在部署时,System.Speech.dll 对象未设置为对象的实例

标签 c# asp.net .net

我有一个 ASP.NET Web 应用程序,它使用 System.Speech 将文本转换为 WAV 文件。它在本地工作正常,但是当我将它部署到服务器时,我收到以下错误消息。这是使用 Windows Server 2012、ASP.NET 4.5 和 IIS 8.5:

Object reference not set to an instance of an object.
System.Speech
   at System.Speech.Internal.ObjectTokens.RegistryDataKey..ctor(String fullPath, RegistryDataKey copyKey)
   at System.Speech.Internal.ObjectTokens.SAPICategories.DefaultDeviceOut()
   at System.Speech.Internal.Synthesis.VoiceSynthesis..ctor(WeakReference speechSynthesizer)
   at System.Speech.Synthesis.SpeechSynthesizer.get_VoiceSynthesizer()
   at QuinnSDS.handlerTransform.<>c__DisplayClass6.<ProcessRequest>b__1()

生成此错误消息的代码在服务器上运行:

if (context.Request.ContentLength > 0)
{
    string line = new StreamReader(context.Request.InputStream).ReadToEnd();
    // *********  generate wav file voicing the response *****************
    // Using Microsoft voices
    // initiate new instance of speech synthesizer
    Thread t = new Thread(() =>
    {
        try
        {
            // The object creation works fine
            System.Speech.Synthesis.SpeechSynthesizer synth = new System.Speech.Synthesis.SpeechSynthesizer();  
            if (synth != null)
            {
                // The code breaks at synth.GetInstalledVoices() below. It will break any time I try to do anything with the synth object
                foreach (System.Speech.Synthesis.InstalledVoice voice in synth.GetInstalledVoices())  
                {
                                    System.Speech.Synthesis.VoiceInfo info = voice.VoiceInfo;
                                    string voiceName = info.Name;
                                    ws.WriteLine(voiceName);
                 }
             }
         }
         catch (Exception e)
         {
             ws.WriteLine(e.Message);
             ws.WriteLine(e.Source);
             ws.WriteLine(e.StackTrace);
         }
         //... code continues...

创建语音合成对象时不会中断;每当我尝试以任何方式使用该对象时它都会中断。

我不确定这是否是访问问题,但我是 ASP.NET 和 IIS 的新手,我不知道如何让 Web 应用程序访问 GAC,或者这是否就是问题所在.在部署应用程序之前,我尝试在 Visual Studio 中将 System.Speech 引用的属性 Local Copy 更改为 True,但这没有用。我在网上搜索,虽然“对象引用未设置到对象的实例”似乎很常见,但由于 .NET 框架类库,我找不到任何类似的问题......我已经运行了文本到-服务器本地的语音代码,运行良好。我没有在服务器本地运行整个应用程序,因为 Web 应用程序需要语音输入并且服务器上没有麦克风。

欢迎提出任何尝试的想法!

最佳答案

从 ASP.NET 执行时,代码在哪个用户帐户下运行?如果 Speech API 像调用堆栈建议的那样接触注册表,它可能具有与您用于手动运行代码的帐户不同的权限。

如果您不能只使用您登录计算机时使用的相同帐户运行站点的应用程序池,我已经使用 Process Monitor 取得了一些成功。以前追查过这种问题。基本上,在 Process Monitor 运行时执行失败的代码,并在“Result”列中查找“ACCESS DENIED”(或任何其他看起来可疑的内容)。不过,快速切换应用程序池以使用您的标准用户帐户将是排除安全或权限相关问题的最快方法。

关于c# - ASP.NET Web 应用程序 - 在部署时,System.Speech.dll 对象未设置为对象的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29598849/

相关文章:

c# - 在调用方法之前处理信息的自定义属性

c# - 如何在约束条件下使多个按钮居中?

asp.net - 错误: "is already declared as protected WithEvents"

c# - 如何从构建操作标记为资源的图像中获取 System.Drawing.Image 对象?

jquery - 使用 jQuery 更改可见的 LinkBut​​ton

asp.net - Jquery Ajax Web服务和跨域问题

c# - 将包含多个项目的一种解决方案部署到一个 Azure WebJob

arrays - F# 为什么在映射函数来对数组中的每个数组进行置乱时,数组不会以不同的方式进行置乱

c# - 将 DLL 注入(inject)目标进程 VB.NET

c# - Blazor 数据列表 onchange 事件未触发