[英]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.