C# • Create the Web Service Methods

Listing 1. The GetPDF method and the InsertPDF method are the sole Web methods in the SecureMTOM Web service. The implementation is simple, but these two examples in combination with some WSE 3.0 configurations demonstrate the significant power of using MTOM and the MutualCertificate security model.

[WebMethod]
public void InsertPDF(string fileName, Byte[] 
	pdfFile) 
{
try
{

// Set to your local connection string
SqlConnection pdfDataConn = new 
	SqlConnection(LOCAL_CONN);
	SqlCommand addPDF = new 
		SqlCommand("INSERT INTO PDF_Files " +
		"(Filename, PDF) Values(@Filename, @PDF)", 
		pdfDataConn);
	addPDF.Parameters.Add("@Filename", 
		SqlDbType.NVarChar, 50).Value = fileName;
	addPDF.Parameters.Add("@PDF", 
		SqlDbType.Image, pdfFile.Length).Value = 
		pdfFile;	
	pdfDataConn.Open();
		addPDF.ExecuteNonQuery();
		pdfDataConn.Close();
}
catch(Exception ex)
{
	throw ex;
}
}

[WebMethod]
public Byte[] GetPDF(string fileName) 
{
	try{

// Set to your local connection string
	SqlConnection pdfConn = 
		new SqlConnection(LOCAL_CONN);
	SqlCommand pdfCMD = new SqlCommand("SELECT 
		Filename, " + "PDF FROM PDF_Files WHERE 
		Filename ='" + fileName + "'", pdfConn);
			pdfConn.Open();
			SqlDataReader myReader = 
				pdfCMD.ExecuteReader();
			myReader.Read();
			byte[] blob = new 
	Byte[(int)myReader.GetSqlBytes(1).Length];
		myReader.GetBytes(1, 0, 
	blob, 0, (int)blob.Length);
		myReader.Close();
		pdfConn.Close();
		return blob;
		}
		catch(Exception ex) 
		{
			throw ex;
		}
}