我在C#/ ASP.NET 4应用程序中使用Booksleeve库.目前,RedisConnection对象是我的MonoLink类中的静态对象.我应该保持这个连接打开,还是应该在每次查询/事务后打开/关闭它(正如我现在所做的那样)?只是略显困惑.这就是我现在使用它的方式:
public static MonoLink CreateMonolink(string URL)
{
redis.Open();
var transaction = redis.CreateTransaction();
string Key = null;
try
{
var IncrementTask = transaction.Strings.Increment(0, "nextmonolink");
if (!IncrementTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Monolink index increment timed out.");
}
// Increment complete
Key = string.Format("monolink:{0}", IncrementTask.Result);
var AddLinkTask = transaction.Strings.Set(0, Key, URL);
if (!AddLinkTask.Wait(5000))
{
transaction.Discard();
throw new System.TimeoutException("Add monolink creation timed out.");
}
// Run the transaction
var ExecTransaction = transaction.Execute();
if (!ExecTransaction.Wait(5000))
{
throw new System.TimeoutException("Add monolink transaction timed out."); …Run Code Online (Sandbox Code Playgroud) 我在这里有一个问题,我希望一些超级专家能够解释.我有一个小的HttpHandler.它从查询字符串中获取文件ID,查找相应的文档路径(来自数据库),并返回响应中的文件内容.
相关代码如下所示:
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment;filename=" + file.DisplayName);
context.Response.AddHeader("content-type", mimetype);
string path = context.Server.MapPath(DocumentUploadPath + "/" + file.FileName);
using (FileStream stream = File.Open(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[32768];
int bytesRead = 0;
while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0)
{
context.Response.OutputStream.Write(buffer, 0, bytesRead);
}
}
context.Response.OutputStream.Flush();
Run Code Online (Sandbox Code Playgroud)
在今天检查了我们的错误日志之后,我们注意到我们正在获得一些IoExceptions:
该进程无法访问文件'file_path_here',因为它正被另一个进程使用.
为了测试,我设置了一个简单的Parallel.For循环来尝试访问该文件.果然,错误是非常可重复的.它看起来像这样:
static void Main(string[] args)
{
Parallel.For(0, 1000, ctr =>
{
GetFile();
});
}
static void GetFile()
{
string fileUrl = "url_to_file.ashx?Id=blah";
HttpWebRequest request = WebRequest.Create(fileUrl) as HttpWebRequest;
HttpWebResponse …Run Code Online (Sandbox Code Playgroud) 我有一个非常基本的扩展方法:
namespace PHPImport
{
public static class StringExtensionMethods
{
public static bool IsNullEmptyOrWhiteSpace(this string theString)
{
string trimmed = theString.Trim();
if (trimmed == "\0")
return true;
if (theString != null)
{
foreach (char c in theString)
{
if (Char.IsWhiteSpace(c) == false)
return false;
}
}
return true;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我试图在同一个项目中使用它(单独的.cs文件),在同一个命名空间中,我收到一个'string' does not contain a definition for 'IsNullEmptyOrWhiteSpace'错误.
namespace PHPImport
{
class AClassName: AnInterface
{
private void SomeMethod()
{
if (string.IsNullEmptyOrWhiteSpace(aStringObject)) { ... }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我已经尝试重建/清理解决方案,并重新启动visual …
asp.net ×2
c# ×2
.net-2.0 ×1
.net-4.0 ×1
booksleeve ×1
c#-4.0 ×1
httphandler ×1
iis-7 ×1
redis ×1