Nov 9 2010

Required Reading for Creating and Deploying a WCF REST Service on IIS7

Deploying a REST service with Windows Communication Foundation (WCF) is great, because it’s very powerful, but it can also be enormously frustrating.

First of all, start by reading the whitepaper.
http://msdn.microsoft.com/en-us/library/dd203052.aspx

This will give you a great overview on how to construct your methods, and how to label them in order to have them exposed correctly by REST.

Next, you need to create your web.config file. Use webhttpbinding for REST.

Lastly, deployment:

How to use https for WCF services in IIS7

How to rewrite your URLs to get rid of the lame .svc extension

I am posting a small demo project, hopefully it’s useful to someone. Simply implement the API interface as you see fit, and the rest basically does itself. You’ll notice the web.config includes a SOAP binding for legacy Microsoft clients to use if they wish.

Link: API_Template


Aug 11 2010

Select DataSet and number of total rows with one stored procedure

when you want to write a search using .net and MSSQL, it’s a pain. This is because you’re forced to select every row in the table and then only display a small subset of it. This works okay for tables that have a few hundred rows, as query caching can make this faster. But what happens when you’re searching a table with half a million rows?

Unless you’re a complete masochist, you’re doing to want to split this into a more manageable data set, otherwise you’re gonna eat all the memory on your server. But this means that you can no longer use the DataSet.Tables[0].Rows.Count property to figure out how many rows you have. You can write a second stored procedure that’ll count the rows. But who wants to clog up their database with tons of stored procedures for no reason? Let’s consolidate it into one.

So what does this look like?

First: the stored procedure.

We’ll use output parameters to pass the row count back to our code


create procedure [dbo].[Search]
@searchText varchar(512), @recordsToReturn INT, @pageNumber INT, @numberofrows INT OUTPUT
AS

-- get the page we want to view
select * from
(
select *, ROW_NUMBER() OVER (ORDER BY creation_timestamp DESC) AS row from [table] where [table].columnName like '%' + @searchText + '%'
)
AS results WHERE row between (@pageNumber - 1) * @recordsToReturn + 1 and @pageNumber*@recordsToReturn;

-- get the total number of rows, not just the subset we want
set @numberofrows = (select count(*) from [table] where [table].columnName like '%' + @searchText + '%')

END

Now the C# (this’ll work in VB too, but feel free to convert it yourself)


SqlConnection conn = new SqlConnection();
conn.ConnectionString = ".....your connection string here.....";
conn.Open();

DataSet returnData = new DataSet();

SqlDataAdapter da = new SqlDataAdapter( "SearchMessages", conn);
da.SelectCommand.CommandType = CommandType.StoredProcedure;

da.SelectCommand.Parameters.Add("@searchText", SqlDbType.VarChar).Value = "bob";
da.SelectCommand.Parameters.Add("@recordsToReturn", SqlDbType.Int).Value = 10;
da.SelectCommand.Parameters.Add("@pageNumber", SqlDbType.Int).Value = 1;

//number of rows
SqlParameter outputParameter = new SqlParameter("@numberofrows", SqlDbType.Int, 2);
outputParameter.Direction = ParameterDirection.Output;

da.SelectCommand.Parameters.Add(outputParameter);

da.Fill(returnData, "theData");

int numberOfRowsInDataSet = (int)outputParameter.Value;

da.Dispose();
conn.Close();

Best of luck! As always, leave a message in the comments if you have questions


Jul 9 2010

MySQL Duplicate Function

Sometimes you want the ability to duplicate (clone) entities in mySQL. But duplicating their children can be a huge pain! Here’s how:

/* parent table */
insert into [parent table]
select 0, [field1, field2, field3] from [tablename] where [parent table primary key] = [parent table object ID]

/* child table */
insert into [child table]
select 0, [field1, field2, field3], (select max([parent table primary key]) from [parent table]) from [child table] where [parent table primary key] = [parent table object ID]

important note! make sure that you don’t select the primary key of the table. instead, select 0, and the auto_increment function will automatically figure out the correct primary key

As always, if you have questions, post em in the comments!


Jun 23 2010

WordPress Multi Page Posts

So, multi paged posts in WordPress. That would be a handy feature, right?

Well it turns out it’s been around since at least 2007. Just found out about it today though. “How do I use it?” you ask?

Simply put


<!--nextpage-->

into your post. This tag acts as a page break. Done!


Jun 17 2010

Batch Delete Performance SQL Server

Deleting old records from a table with > 3 000 00 rows. What’s the best way to do this?

It seems the fastest way to do this is simply to:

delete from [table] where creation_timestamp < dateadd (mm, -6, getdate())

(deleting anything older than 6 months)

It took 3 hours (10916 seconds to be exact) to delete 1.6 million (1,619,433) records this way. (148.35 / second).

We needed to do a second batch the next day, but wanted to split it into batches to try to get better performance.

Running:


delete from [table] where pk_id in(
select pk_id from (
SELECT ROW_NUMBER() OVER (ORDER BY creation_timestamp desc) AS RowNumber, pk_id
FROM [table]
where creation_timestamp < '2009-12-16 13:52:08.673') _objectsToDelete WHERE RowNumber between 1 and 100000)

takes 12 minutes. (732 seconds) (136.61 / second).

Strangely, using the TOP command with a subquery takes the longest:


delete from [table] where pk_id in (select TOP 100000 pk_id from [table] where creation_timestamp < '2009-12-16 13:52:08.673')

15 minutes (904 seconds) (110.61 / second)

Have a better way? Let me know in the comments!


Apr 15 2010

Xcode Fix Recent Projects Empty

Terminal Command:
defaults write com.apple.Xcode NSRecentDocumentsLimit -int 10

Relaunch Xcode. Done.


Mar 24 2010

New Favourite Terminal Command

Best command ever:

sudo !!

Tells the terminal to run the last command again, but to throw sudo in front of it.

obligatory XKCD link:
Sandwich
http://xkcd.com/149/


Jan 11 2010

Run JavaScript function every n seconds


//tell javascript to run a function in 1 second
setTimeout ("myFunction()", 1000 );

function myFunction(){
//do stuff

//once the function is finished, queue this function up to run again in 1 second
setTimeout ( "myFunction()", 1000 );
}


Sep 12 2009

Get latitude and longitude of an address using google maps

Google doesn’t make it easy to show you the latitude and longitude of an address you search in google maps, but there’s an easy way to get the info.

  1. go to google maps, type the address, and click search
  2. once you’ve found it, go to your address bar and clear what’s in it
  3. paste: javascript:void(prompt('',gApplication.getMap().getCenter())); into the address bar
  4. use the coordinates for whatever you wish!

Aug 13 2009

VirtualHostX and mod_rewrite

To enable mod_rewrite manually, see: OS X server tips but if you are using VirtualHostX it changes your default config file. Add the following code to the Directives box:


Options FollowSymLinks
AllowOverride All AuthConfig
Order deny,allow
Deny from all

if you’re using the new version of VirtualHostX, make sure that you select Directory from the dropdown, then use:


Options FollowSymLinks
AllowOverride All AuthConfig