在ASP.NET中异步发送电子邮件的正确方法…(我做得对吗?)

当用户在我的网站上注册时,我不明白为什么我需要让他“等待”smtp通过才能获得激活电子邮件。 我决定异步启动这段代码,这是一次冒险。 让我们想象一下我有一个方法,例如: private void SendTheMail() { // Stuff } 我的第一个虽然是线程。 我这样做了: Emailer mailer = new Emailer(); Thread emailThread = new Thread(() => mailer.SendTheMail()); emailThread.Start(); 这工作……直到我决定测试它的error handling能力。 我故意破坏了我的web.config中的SMTP服务器地址并尝试了它。 可怕的结果是IIS基本上BARFED与w3wp.exe上的未处理exception错误(这是一个Windows错误!多么极端……)ELMAH(我的错误记录器)没有捕获它并且IIS重新启动所以网站上的任何人都有他们的会话被删除了。 完全不可接受的结果! 我的下一个想法是,对异步代表进行一些研究。 这看起来效果更好,因为在异步委托中处理exception(与上面的线程示例不同)。 但是,我担心的是我做错了还是我可能导致内存泄漏。 这就是我正在做的事情: Emailer mailer = new Emailer(); AsyncMethodCaller caller = new AsyncMethodCaller(mailer.SendMailInSeperateThread); caller.BeginInvoke(message, email.EmailId, null, null); // Never EndInvoke… 我这样做了吗?

Google Calendar API – 错误请求(400)尝试交换访问令牌代码

获得Google的授权代码以访问用户的日历后,我现在尝试将其替换为访问令牌。 根据自己的文档: 实际请求可能如下所示: POST /o/oauth2/token HTTP/1.1 Host: accounts.google.com Content-Type: application/x-www-form-urlencoded code=4/v6xr77ewYqhvHSyW6UJ1w7jKwAzu& client_id=8819981768.apps.googleusercontent.com& client_secret={client_secret}& redirect_uri=https://oauth2-login-demo.appspot.com/code& grant_type=authorization_code 我尝试访问它的方法如下(C#): string url = “https://accounts.google.com/o/oauth2/token”; WebRequest request = HttpWebRequest.Create(url); request.Method = “POST”; request.ContentType = “application/x-www-form-urlencoded”; string body = “code=&\r\n” + “client_id=&\r\n” + “client_secret=&\r\n” + “redirect_uri=http://localhost:4300\r\n” + “grant_type=authorization_code&\r\n” ; byte[] bodyBytes = Encoding.ASCII.GetBytes(body); request.ContentLength = bodyBytes.Length ; Stream bodyStream = request.GetRequestStream(); […]

登录系统的IPAddress

使用波纹管代码。 protected string GetUserIP() { string strUserIP = string.Empty; if (HttpContext.Current.Request.ServerVariables[“HTTP_X_FORWARDED_FOR”] != null) { strUserIP = HttpContext.Current.Request.ServerVariables[“HTTP_X_FORWARDED_FOR”].ToString(); } else if (HttpContext.Current.Request.UserHostAddress.Length != 0) { strUserIP = HttpContext.Current.Request.UserHostAddress; } return strUserIP; } 我得到格式::1的IPaddress。 如何获取系统的正确IP地址。

包含System.IO.Compression 时,当前上下文中不存在名称“ZipFile”

我目前正在运行.net 4.5版,我正在尝试使用它的“新”zip函数。我包括System.IO.Compression ,我正在尝试运行以下代码: using System.IO.Compression; string startPath = @”c:\example\start”; string zipPath = @”c:\example\result.zip”; string extractPath = @”c:\example\extract”; ZipFile.CreateFromDirectory(startPath, zipPath); ZipFile.ExtractToDirectory(zipPath, extractPath); 我得到的问题是The name ‘ZipFile does not exist in the current context 。 如果我已经在使用需要它的东西,我不知道它为什么不存在。

来自HRESULT的exception:0x80040013:当用C#隔离R时:

我正在尝试将R与C#web应用程序接口。我尝试了许多解决方案,但无法通过。我得到的错误是:来自HRESULT的exception:0x80040013 我遵循的步骤是: 为Windows XP安装R 2.15.1(32位) 已安装R_Scilab_DCOM3.0-1B5 在R / R.15.1 /库中加载了rscproxy_1.3-1包 将系统变量路径设置为C:/ Program Files / R / R.15.1 / bin 添加了3个COM引用-STATCONNECTORCLNTLib,StatConnectorCommonLib,STATCONNECTORSRVLib代码: private StatConnectorClass _statconnector = new STATCONNECTORSRVLib.StatConnectorClass(); _statconnector.Init(“R”); – >行显示HRESULT的exception:0x80040013 请帮我找出我错的地方!

对于有经验的C ++程序员来说,C#开发中最常见或最恶毒的错误

当有经验的C ++程序员在C#中开发时,最常见或最恶毒的错误是什么?

游戏设计/理论,Loot Drop Chance / Spawn Rate

我有一个非常具体和冗长的问题。 这个问题是关于编程和游戏理论的。 我最近在我的回合制战略游戏中添加了可产卵的矿石: http ://imgur.com/gallery/0F5D5Ij(对于那些看起来请原谅开发纹理的人)。 现在,谈到我一直在考虑的谜。 在我的游戏中,每次创建新地图时都会生成矿石。 每级创建生成0-8个矿石节点。 我已经有了这个工作; 除此之外它只生成“绿gem”,这让我想到了问题。 程序员如何才能使节点具有特殊的稀有性? 考虑这个简短的模型,它实际上不是游戏数据: (Pseudo Chances节点将是以下之一) Bloodstone 1 in 100 Default(Empty Node) 1 in 10 Copper 1 in 15 Emeraldite 1 in 35 Gold 1 in 50 Heronite 1 in 60 Platinum 1 in 60 Shadownite 1 in 75 Silver 1 in 35 Soranite 1 in 1000 […]

无法重新启动服务

我有这个代码重新启动服务,但这不起作用。 我可以单独启动和停止但不重启,这涉及到我先停止并启动服务。 try { //service.Stop(); //service.Start(); int millisec1 = Environment.TickCount; TimeSpan timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds); service.Stop(); service.WaitForStatus(ServiceControllerStatus.Stopped, timeout); // count the rest of the timeout int millisec2 = Environment.TickCount; timeout = TimeSpan.FromMilliseconds(timeoutMilliseconds – (millisec2 – millisec1)); service.Start(); service.WaitForStatus(ServiceControllerStatus.Running, timeout); } catch { // … } 它只是进入捕获部分。 我不知道我哪里出错了。 有什么建议。?? 更新: 所以我从下面的正确答案中得出了这个想法: 这是需要做的事情> public static void RestartService(string serviceName, […]

使用getAttribute时返回System .__ ComObject

我正在运行此代码: HtmlElement.GetAttribute(“onClick”) 要尝试访问该元素的onClick属性,但所有这些都返回System .__ ComObject。 我不知道为什么会发生这种情况,所有其他GetAttribute调用都返回实际的字符串。 谢谢。

使用SymmetricDifference比较两个不同文件的文件夹?

我有两个文件夹A和B ..在那里有两个文件夹,有很多文件夹和文件…我正在比较这两个文件夹中是否存在对称差异的相同文件,并将名称和目录名称写入文本文件…我用过这段代码 public class FileInfoNameLengthEqualityComparer : EqualityComparer { public override bool Equals(FileInfo x, FileInfo y) { if (x == y) return true; if (x == null || y == null) return false; // 2 files are equal if their names and lengths are identical. return x.Name == y.Name && x.Length == y.Length && x.LastWriteTime== y.LastWriteTime; […]