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

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

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:

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.

/* 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]

Pure PHP Pagination

So, for a while I’ve been looking for a ridiculously simple way to paginate data stored in a table. And while I love PHP, it just doesn’t come with some of the free things I take for granted in .Net

Today, while working on a pretty simple plugin, I wanted to add this functionality, and I didn’t want to waste a bunch of time. I was given a link to this great script: http://www.warkensoft.com/2009/12/paginated-navigation-bar/

It took me about a minute to implement the functionality. I then spent another 5 minutes doing CSS and that was it. Done. I’m not usually one for link sharing, but this made my life much easier. Hopefully it can make yours easier too.

The solution is pure PHP, no javascript required. Some day, I may try to expand upon this to include javascripty goodness, because that’s pretty much the only thing that could make it better