coding
WCF Resolving error ‘The caller was not authenticated by the service.’
by brian on Sep.17, 2009, under .NET, WCF, c#, coding
I ran across an easy way to get wsHttpBinding to work on a remote machine. This involves just a little bit of code on the client side to complete the authentication.
VPortalDataServiceClient client = new VPortalDataServiceClient();
client.ClientCredentials.Windows.ClientCredential.UserName = "btomas";
client.ClientCredentials.Windows.ClientCredential.Password = "password123";
The UserName is the windows account user name that is hosting the service. This user may just need to be one on the domain that the machine can see — not exactly sure though. And the Password is the password of that user.
c# Getting a nullable attribute to serialize to xml
by brian on Jul.23, 2009, under .NET, c#, c# coding GUI, coding, linq
It isn’t straight forward to get a nullable field to serialize. Technically it isn’t supported. But I just discovered a way today. You see null strings are supported. The idea is to ignore the real nullable property while substituting the string version as follows:
[XmlRoot("Comment")]
public class Comment
{
[XmlIgnore]
public DateTime? CommentTime { get; set; }
[XmlAttribute("CommentTime")]
public string CommentTimeStr
{
get
{
return (CommentTime.HasValue) ? CommentTime.Value.ToString() : null;
}
set
{
if (string.IsNullOrEmpty(value))
CommentTime = null;
else
CommentTime = DateTime.Parse(value);
}
}
}
Now it can be serialized.
c# finding those blasted predefined IEqualityComparer classes!
by brian on Apr.14, 2009, under .NET, c#, coding
Why there isn’t any reference in the IEqualityComparer documentation to pre-implmented classes that override the function I don’t know.
But here’s one for string comparisons:
StringComparer
This static class contains many different predefined IEqualityComparer instances to be used in functions like “Contains”. Enjoy.
c# Getting your machine’s IP addresses.
by brian on Mar.17, 2009, under .NET, c#, c# coding GUI, coding, networking
Here is the simple way to get all of the IP addresses for you machine. This code filters out everything but IPv4 address, but to truly get everything just remove the if statement.
string hostName = Dns.GetHostName();
var addrs = Dns.GetHostAddresses(hostName);
bool hasIP = false;
for (int i = 0; i < addrs.Length; i++)
{
IPAddress addr = addrs[i];
if (addr.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
{
si.DataIP = addr.ToString();
hasIP = true;
break;
}
}
MS SQL Delete all the data from the tables.
by brian on Nov.21, 2008, under coding, mmsql, sql
Here’s a helpful, and perhaps dangerous, little tidbit of code for SQL Server 2005. It deletes all of the data from all of the tables. To be completely effective, the command may need to be run more than once because of foreign keys. Basically run it until there aren’t any errors. Then the db is clean and empty of all data.
exec sp_MSForeachTable "delete from ?"
the sp_MSForeachTable is a very useful system stored procedure.
c# linq to sql string comparisons
by brian on Oct.27, 2008, under .NET, c#, coding, linq, sql
There are four different string comparisons for handling string fields with linq to sql. They are as follows:
The exact macth
DataContext dc = new DataContext(...) var results = from row in dc.table where row.string_column == "Exact Match" select row;
Begins With:
DataContext dc = new DataContext(...)
var results =
from row in dc.table
where row.string_column.StartsWith("Ex")
select row;
produces an sql match string “Ex%”
Contains
DataContext dc = new DataContext(...)
var results =
from row in dc.table
where row.string_column.Contains("Ex")
select row;
Which is equivalent to “%Ex%”
Ends With
DataContext dc = new DataContext(...)
var results =
from row in dc.table
where row.string_column.EndsWith("Ex")
select row;
Which is equivalent to “%Ex”
c# Reading the contents of a web page
by brian on Sep.10, 2008, under .NET, c#, coding
Reading the contents of a web page can be very useful. Here’s some code I’m developing to read the historical stock prices from http://finance.google.com
string uri = "http://finance.google.com/finance/historical?cid=667226&startdate=Sep+9%2C+2007&enddate=Sep+10%2C+2008&output=csv";
WebRequest request = WebRequest.Create(uri);
WebResponse response = request.GetResponse();
var stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));
string page = reader.ReadToEnd();
There you go. Very simple isn’t it? Just got to love .Net
c# timing functions
by brian on Jun.12, 2008, under .NET, c#, coding
Here are some timing functions that come in the .Net framework. I’ve used these for moderately accurate timing code — at least down to the millisecond level. For any timing less than about 30 milliseconds these work much better than timer events piled on each other which tend to get inaccurate.
int begin = System.Environment.TickCount ... do something int end = System.Environment.TickCount int elapsed = end - begin;
c# getting a list of files from a directory
by brian on Jun.04, 2008, under .NET, c#, coding
Here’s how to get an array of file descriptors (name and other information).
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Program.DataDirectory);
System.IO.FileInfo[] files = di.GetFiles("*.txt");
c# getting the application directory.
by brian on Jun.04, 2008, under .NET, c#, coding
Getting the directory that the application is in at run-time is nice. Here’s the code.
string appDir = AppDomain.CurrentDomain.BaseDirectory;
