by StoneJunction
6. January 2012 23:58
I am just getting up to speed on MVC3 with Razor and when creating a partial view ("_TestPartial.cshtml") and including it on another page through with:
@Html.RenderPartial("_TestPartial")
I got the error:
CS1502: The best overloaded method match for 'System.Web.WebPages.WebPageExecutingBase.Write(System.Web.WebPages.HelperResult)' has some invalid arguments
Looking at RenderPartial it returns a void. Internally it writes directly to the response. A quick razor syntax change to:
@{ Html.RenderPartial("_TestPartial"); }
sorted the problem. Now for the interesting problem of getting MVC partials to render on classic aspx webpages.
by StoneJunction
7. October 2011 20:17
I ran into a problem yesterday with the File.WriteAllText method when saving large text files to disk.
I had a piece of code similar to this:
public void Test(List<String> lines)
{
var output = new StringBuilder();
foreach (String line in lines)
{
output.Append(line).Append(Environment.NewLine);
}
File.WriteAllText(@"c:\test.txt", output.ToString());
}
which worked fine for small string lists. However when the list of strings ran to the 100's of Mb I got System Out of Memory errors. A simple google seemed to show a few people with similar problems but no real solutions. Pushed for time I copped out and replaced it with a good old fashioned StreamWriter:
public void Test(List<String> lines)
{
var file = new StreamWriter(@"c:\test.txt");
foreach (String line in lines)
{
file.WriteLine(line);
}
file.Close();
}
by StoneJunction
23. September 2011 22:28
I was looking for a way to count the messages on a MSMQ and came across and article here. After reading the comments it seems as if the best way is to get all the messages in an array, and look at the length of the array. Passing a filter to the queue stops the overhead of pulling back the message body and so helps performance. Here's the function:
using System.Messaging;
private static int GetQueueLength(string path)
{
if (!MessageQueue.Exists(path))
{
return 0;
}
var countFilter = new MessagePropertyFilter
{
AdministrationQueue = false,
ArrivedTime = false,
CorrelationId = false,
Priority = false,
ResponseQueue = false,
SentTime = false,
Body = false,
Label = false,
Id = false
};
var mq = new MessageQueue(path) {MessageReadPropertyFilter = countFilter};
return mq.GetAllMessages().Length;
}
I have done some quick and dirty performance testing and it seems to perform ok with large quantities of messages being written and read from the queue.