(c) 2002 Visual Studio Magazine 
Fawcette Technical Publications

Issue: March 2002
Section: Web Services
Author: Josh Lane

VB.NET	Handle Help Desk Requests Synchronously
Listing 1	This Web Service defines a single public SubmitRequest() method. The user request is processed immediately, regardless of current server load or database availability. This can limit scalability and robustness.

<WebMethod(TransactionOption := _
	TransactionOption.RequiresNew)> _
	Public Function SubmitRequest(ByVal request _
	As HelpDeskRequest)

	Try

	Dim oldWriter as New OldSystemWriter
	oldWriter.SubmitRequest(request)

	dim newWriter as New NewSystemWriter
	newWriter.SubmitRequest(request)

	ContextUtil.SetComplete()

	Catch ex As Exception

EventLog.WriteEntry("HelpDeskRequestProcessor", _
	ex.Message, EventLogEntryType.Error)

	ContextUtil.SetAbort()

	End Try

VB.NET	The Trigger Invokes This Business Logic
Listing 2	The Trigger calls this method each time a new message arrives in the target queue. Note that the queue path is passed in as a parameter. Also note that all work is performed inside a COM+ transaction, so the MSMQ receive operation and both SQL Server write operations are treated as a single atomic operation.

Public Sub ProcessMessage(ByVal path As String) _
	Implements IMsgHandler.ProcessMessage

	Dim mq As MessageQueue
	Dim m As Message
	Dim oldWriter As IRequestWriter
	Dim newWriter as IRequestWriter

	Try

	mq = New MessageQueue(path)
	m = mq.Receive()

	m.Formatter = New XmlMessageFormatter( _
		New Type() {GetType( _
		VSM_SharedTypes.HelpDeskRequest)})

	oldWriter = New OldSystemRequestWriter()
	oldWriter.WriteRequest(m.Body)

	newWriter = New NewSystemRequestWriter()
	newWriter.WriteRequest(m.Body)

	ContextUtil.SetComplete()

	Catch ex As Exception

	EventLog.WriteEntry("VSM_RequestHandler", _
		ex.Message, EventLogEntryType.Error)
	ContextUtil.SetAbort()

	Finally

		mq.Dispose()

	End Try

End Sub

VB.NET	The Revised Web Service Has Few Responsibilities
Listing 3	Here the public Web Service is responsible only for receiving incoming requests and writing them to the target request queue. This simple, fast operation minimizes the work performed by the Web server. Note the use of a local transaction instead of a COM+ transaction.

<WebMethod()> _
	Public Function SubmitOrder(ByVal order As _
	HelpDeskRequest)

	Dim mqt As New MessageQueueTransaction()
	Dim mq As MessageQueue

	Try

		mq = New MessageQueue(m_QueuePath)

		mqt.Begin()
		mq.Send(order)
		mqt.Commit()

	Catch ex As Exception

		EventLog.WriteEntry("VSM_RequestTaker", _
			ex.Message, EventLogEntryType.Error)
		mqt.Abort()

	Finally

		mq.Dispose()

	End Try

End Function
