简体   繁体   English

Twilio SMS 无法通过部署到 Web 服务器的 ASP.NET MVC C# web 工作(但在本地测试环境中工作正常)

[英]Twilio SMS not working via ASP.NET MVC C# web deployed to Web Server (but works fine in local test environment)

I had a very strange situation where SMS messages sent via Twilio were working when my team tested it in the local host ASP.NET MVC & C# testing environment (VS2022).当我的团队在本地主机 ASP.NET MVC 和 C# 测试环境 (VS2022) 中对其进行测试时,我遇到了一个非常奇怪的情况,即通过 Twilio 发送的 SMS 消息正在工作。

But the deployed version on the web server throws an initialization error and the SMS is never sent.但是在 Web 服务器上部署的版本会引发初始化错误,并且永远不会发送 SMS。

The code is in a static class called Common .该代码位于一个名为Common的静态类中。 After hours of attempts, I discovered (to my horror) that Twilio does not properly initialize from a static class when running on the web server.经过数小时的尝试,我发现(令我震惊)Twilio 在 Web 服务器上运行时没有从静态类正确初始化。 But it works fine in the local host test environment.但它在本地主机测试环境中运行良好。

My solution was to change the Common class from a static class to a dynamic public IDisposable class and remove all references to 'static'.我的解决方案是将Common类从静态类更改为动态公共 IDisposable 类,并删除对“静态”的所有引用。 How that is done is so fundamental and verbose that I don't feel the need to explain it.这是如何完成的是如此基本和冗长,以至于我觉得没有必要解释它。

I know, I know... why (the hell) was I using a static class in a webpage?我知道,我知道......为什么(见鬼)我在网页中使用静态类? Because I stole a bunch of working code from an old WinForms app and it was working just fine for everything else.因为我从一个旧的 WinForms 应用程序中窃取了一堆工作代码,而它在其他所有应用程序中都运行良好。

Here's how you call a dynamic IDisposable class called Common :以下是您如何调用名为Common的动态 IDisposable 类:

        Common thisCommon = new Common();
        thisCommon.Method();
        thisCommon.Dispose();

After this terrible experience, I decided to NEVER ever use static classes in C# / ASP.NET MVC web pages ever again.在这次可怕的经历之后,我决定永远不再在 C# / ASP.NET MVC 网页中使用静态类。 It's not reliable in all situations.它并非在所有情况下都可靠。 And, to be frank, it probably was a terrible idea in the first place.而且,坦率地说,这可能是一个糟糕的想法。

As I said above, you shouldn't be so lazy and use a static class in an MVC 5.0 C# web app environment.正如我上面所说的,你不应该这么懒惰并在 MVC 5.0 C# Web 应用程序环境中使用静态类。 Make it a dynamic iDisposible class and things will work properly.让它成为一个动态的 iDisposible 类,一切都会正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.net c# 文件上传工作正常,但在服务器中部署时出错 - asp.net c# File uploading is working fine but error when I deployed in server Asp.Net Web Api 在 Godaddy 服务器中不工作,但在本地工作正常 - Asp.Net Web Api is not working in Godaddy server but its working fine in local 部署到Web服务器时,ASP.NET Identity不起作用 - ASP.NET Identity not working when deployed to web server asp.net mvc将本地文件添加到Web服务器 - asp.net mvc add local file to web server 使用C#ASP.NET将Word文档内容传输到服务器端的Web环境 - Transfer Word Document Contents to Web Environment on Server Side Using C# ASP.NET 通过ASP.NET MVC和C#询问其他网站 - Ask other web site via ASP.NET MVC and C# asp.net C#Web窗体应用程序中的密码恢复在localhost上有效,但在Godaddy服务器上不起作用 - Password recovery in asp.net c# web forms application works on localhost but not on Godaddy server C#Web服务在控制台中工作,但不在IIS服务器上的asp.net网站上工作 - C# Web service works in console but not from asp.net website on IIS server C#Asp.NET内部Web应用程序:从其他本地服务器复制文件 - C# Asp.NET Internal Web App: Copy file from a different local server 通过c#ASP.Net中的网页运行TSQLScripts - Run TSQLScripts via the Web Page in c# ASP.Net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM