Thursday, August 14, 2008

MassTransit : Classic WebForms

The Web sample in the MassTransit download that i talked about in the last post used MonoRail. As i am working on a project not using monorail (and there may be a couple of others doing the same) i thought i would make demo project using asp.net, oldskool.

The project contains 6 files (well 9 really)
AsynchWebForm.aspx (marked as Async="true")
Global.asax
Web.config
WebAppContainer.cs
RequestMessage.cs
ResponseMessage.cs

Thats it. It references the mass transit dlls (mt.sb., mt.sb.msmq, mt.WindsorInt) and the appropriate castle dlls.
the global.asax is just like the DashboardApplication in the morails sample and the WebAppContainer.cs again is similar to the monorail sample without any references to monorail! The message are exactly the same and the web config has the castle section definition and castle section inserted into a default web.config file.
I was lazy with my resolving of the IServiceBus, see below.
-----WebApContainer--------

using System;
using Castle.Core.Resource;
using Castle.Facilities.Startable;
using Castle.MicroKernel.Registration;
using Castle.Windsor.Configuration.Interpreters;
using MassTransit.WindsorIntegration;
using MassTransit.ServiceBus;

namespace MassTransitWebTest
{
public class WebAppContainer :
DefaultMassTransitContainer
{
private static IServiceBus bus;
public WebAppContainer()
: base(new XmlInterpreter(new ConfigResource()))
{
RegisterFacilities();
bus = Resolve();
}

protected void RegisterFacilities()
{
AddFacility("startable", new StartableFacility());
}

internal static IServiceBus getBus()//yes im that lazy
{
return bus;
}
}
}


-----AsynchWebForm.aspx.cs--------

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MassTransitWebTest.Server;
using MassTransit.ServiceBus;

namespace MassTransitWebTest
{
public partial class AsynchWebForm : Page, Consumes.For
{

private IServiceBus _bus;
private Guid _correlationId;
private ServiceBusRequest _request;
private ResponseMessage msg;
private RequestMessage pendingMessage;

protected void Page_Load(object sender, EventArgs e)
{
//not much to do here
}

IAsyncResult PageBeginEventHandler(object sender, EventArgs e, AsyncCallback cb, object state)
{
_request = _bus.Request()
.From(this)
.WithCallback(cb, state);

_request.Send(pendingMessage);
pendingMessage = null;

return _request;
}

void PageEndEventHandler(IAsyncResult ar)
{
IAsyncResult r = ar;
this.txtResponse.Text = msg.Text + " (and my response)";

}

#region All Members

public void Consume(ResponseMessage message)
{
msg = message;
_request.Complete();
}

#endregion

#region CorrelatedBy Members

public Guid CorrelationId
{
get { return _correlationId; }
}

#endregion

protected void Button1_Click(object sender, EventArgs e)
{
AddOnPreRenderCompleteAsync(
new BeginEventHandler(PageBeginEventHandler),
new EndEventHandler(PageEndEventHandler));
_bus = WebAppContainer.getBus();

pendingMessage = new RequestMessage(CorrelationId, this.txtRequest.Text);
}

}
}



Happy days! I am sure you can work out the rest, if any one cares the complete solution can be downloaded from :
http://www.fullstack.co.uk/downloads/MassTransitWebTest.zip
NB:See previous post for setting up queues

No comments: