ExpectedException in MSTest does not check the exception message

When you’re writing a unit test to verify that an exception is thrown and you want to check the content of the error message, the ExpectionException attribute doesn’t do it.

This code (which you see a lot):

[TestMethod]
[ExpectedException(typeof(ValidationException), “Postcode must be provided”)]
publicvoid PostcodeIsNullOrWhitespace()
{
//etc.
}

– checks that an exception is thrown, and the test will pass if the exception is of type ValidationException. The second parameter of the ExpectedException attribute is the string that will be written in the MSTest output if the test fails, so this test is not checking the content of the exception message.

If you do want to test the exception message as well as the exception type, this utility method does it for you:

///<summary>
/// Runs the action statement and asserts that it causes an exception with the expected type and message
///</summary>
///<typeparam name=”TException”></typeparam>
///<param name=”action”></param>
///<param name=”expectedMessage”></param>
publicstaticvoid AssertRaisesException<TException>(Action action, string expectedMessage)
where TException : Exception
{
try
{
action();
Assert.Fail(“Call suceeded. Expected exception of type: {0} with message: {1}”.FormatWith(typeof(TException).Name, expectedMessage));
}
catch (Exception ex)
{
if (ex isAssertFailedException)
throw ex;
var exception = ex as TException;
Assert.IsNotNull(exception, “Expected exception of type: {0}, actual type: {1}”.FormatWith(typeof(TException).Name, ex.GetType().Name));
Assert.AreEqual(expectedMessage, exception.Message, “Expected message: {0}”.FormatWith(expectedMessage));
}
}

Usage:

[TestMethod]
[ExpectedException(typeof(ValidationException))]
publicvoid PostcodeIsNullOrWhitespace()
{
var postcode = “”;
AssertRaisesException<ValidationException>(() => ValidatePostcode(postcode), “Postcode must be provided”);
//etc.
}

Uncaught exceptions in Task threads will kill IIS – even with WCF error handling

Yes, all unhandled exceptions will kill the IIS worker process, but in WCF you can tag an IErrorHandler onto your service behavior and all unhandled exceptions will be neatly taken care of. Unless that exception is thrown from a factory task, in which case the error handler is bypassed and the worker process is killed.

So this code:

public ServiceResponse OnBoardClient(Client newClient)
{
if (IsValid(newClient))
{
var client = newClient;
Task.Factory.StartNew(() => OnBoardClientInternal(client));
}
//etc.
}
private static void OnBoardClientInternal(Client newClient)
{
var zero = 0;
var dbz = 10 / zero;
}

– will throw a System.AggregateException in the TaskExceptionHolder.Finalize() method and it will take down the IIS worker process with it (as described in this StackOverflow question). Of course this also kills any other connections being handled by the worker process.

This is in-line with the expected behaviour for ASP.NET applications:

“Exceptions that occur in the context of a request do not cause the worker process to end. However, unhandled exceptions outside the context of a request, such as exceptions on a timer thread or in a callback function, cause the worker process to end”.

So the WCF error handler only applies within the request-response context, and within task code you need to explicitly add error handling:

private static void OnBoardClientInternal(Client newClient)

{
try
{
var zero = 0;
var dbz = 10 / zero;
}
catch (Exception ex)
{
//etc.
}
}

– which can call into your error handler’s HandleError method (if your scenario warrants consistent error handling).

MSMQ and External Certificates

I’ve spent some time lately playing around with MSMQ, Authentication and the use of External Certificates. This is an interesting scenario, but one that I found to be documented in relatively unclear terms, with somewhat conflicting information all over the place. Plus, the whole process isn’t very transparent at first. Normally, when you’re using MSMQ […]
Blog Post by: Tomas Restrepo

BizTalk360 – Recommendation from Jan Eliasen

Last week on 21st September we had the privilege to present the capabilities of BizTalk360 to an enthusiastic audience in Copenhagen, Denmark (You can view the pictures here on our facebook fan page).

We fully understand for any technical user group meetings you are not going to get decision makers who will put the money down and buy the product, it will normally be truly technical chaps, who are prepared to spend some of their spare time either to present or come and learn some new stuff. We don’t treat these presentations as marketing pitch, instead we use this opportunity to spread the word and more importantly get some real feedback from people who are on the field. It proved really valuable for us, and we can see the proof in the evolution of BizTalk360.

Jan Eliasen doesn’t require any introduction in the BizTalk world, he is a long time BizTalk Server MVP, he is great blogger all things BizTalk, along with few energetic chaps he runs the BizTalk user group in Denmark, he is the first one to write any BizTalk exams that comes out, and more importantly he is the author of the newly released book "Microsoft BizTalk Server 2010 Unleashed"

You can read Jan’s article about BizTalk360 here http://blogs.eliasen.dk/Technical/post/2011/09/25/BizTalk-360.aspx

We are super excited to see such a positive feedback from a great influencer in the community.

Nandri
Saravana Kumar

Social:
Join us on @biztalk360 | http://facebook.com/biztalk360 | http://getsatisfaction.com/biztalk360

Upcoming Melbourne BizTalk Server 2010 Training-November 2011

Following on from the successful BizTalk Server 2010 Developer training that Mexia ran in Melbourne in August 2011, we are doing it again, this time we have added a 2 days BizTalk 2010 Administrator training course.

  • BizTalk Server 2010 Administrator Training – November 21st & 22nd, 2011 –
  • https://bizadmmel1111.eventarc.com/event/view/5767/biztalk-2010-administrator-training—melbourne

  • BizTalk Server 2010 Developer Training – November 28th to 2nd December, 2011 –
  • https://bizdevmel1111.eventarc.com/event/view/5765/biztalk-2010-developer-training—melbourne

For any questions or to organise on-site BizTalk Server 2010 training please Contact Me

ESB Off-Ramp with Dynamic Resolver to WCF-SQL Binding

ESB Off-Ramp with Dynamic Resolver to WCF-SQL Binding

This example is not covered in the ESB Toolkit samples and I bumped my forehead a few times while making it work. I thought it’d be helpful to save other fellow BizTalk’ers from headaches by publishing findings.

I have been fun of dynamic generic messaging for quite a while and went long ways to avoid working with typed messages and static bindings when it made sense. Nowadays, with the ESB Toolkit one does not have to spend much effort to achieve this goal. But one has to learn intricacies of configuration to unleash unlimited flexibility of the Toolkit.

Let’s say we have a requirement to store some messages on service bus to the database. And the number of message types supported is growing plus their schemas can potentially change. If the database is SQL Server we can use an ESB Off-Ramp dynamically bound to WCF-SQL send port.

The send port interface can be implemented different ways. One way is to generate typed WCF-SQL Adapter schemas for database objects (tables or procedures). Then create maps to transform original message to the adapter schema. This may potentially lead to complex maps that perform poorly, difficult to maintain and support. We also have to deploy new schemas every time new type is added.

Other way with minimal BizTalk coding is to use one-way port calling stored procedure that accepts entire message as a parameter and internally maps/saves message to the database. This saves us from creating and deploying stored procedure schema and map, as well as schema for the response message. We also gain performance on the BizTalk end by offloading XML to relational model mapping to the database engine which it does more efficiently. Simply put, stored procedures perform better and simpler to maintain. I chose this approach over the first.

Suppose, we have two message types http://contoso.com/schemas#PurchaseOrder and http://contoso.com/schemas#Invoice. We have created two stored procedures InsertPurchaseOrder and InsertInvoice. Each stored procedure accepts one parameter, the message body:

CREATE PROCEDURE InsertPurchaseOrder 
    @messageBody nvarchar(max)
AS
BEGIN
    SET NOCOUNT ON;
    
    DECLARE @docHandle int
    EXEC sp_xml_preparedocument @docHandle OUTPUT, @messageBody, N'<ns0:PurchaseOrder xmlns:ns0="http://contoso.com/schemas" />'

-- Perform mapping and insert here using OPENXML for example

END

Create BRE policy to resolve endpoint configuration:

In the Action area there are a lot of things being set up. Let’s go through them one by one:

Parameter

Value

Notes

Set Endpoint Message Exchange Pattern

One-Way

Establishes exchange pattern, we don’t need solicit-response in our case

Set End Point Outbound Transport Type

WCF-Custom

This is the binding we are going to use.

Note: this value is not available in the ESB.TransportTypes vocabulary. Thus, we use literal string value “WCF-Custom”

Set Endpoint Outbound Transport Location

mssql://sqlservername//databasename?

This is standard form of the WCF-SQL uri. The example given is for default SQL instance (notice double slash)

Set End Point WCF Action

{Procedure/dbo/InsertPurchaseOrder}

This is from WCF-SQL adapter schema, but the trick here, it wants to be enclosed in {} which is not obvious

Set End Point Target Namespace

http://schemas.microsoft.com/Sql/2008/05

Set End Point Config

See below

That’s where “magic” is configured and deserves separate consideration

Endpoint configuration details

BindingType=sqlBinding&OutboundXmlTemplate=<InsertPurchaseOrder xmlns=”http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo”><messageBody><bts-msg-body xmlns=”http://www.microsoft.com/schemas/bts2007″ encoding=”string”/></messageBody></InsertPurchaseOrder>&OutboundBodyLocation=UseTemplate&BindingConfiguration=<binding name=”sqlBinding” />

Parameter

Value

Notes

BindingType

sqlBinding

BindingConfiguration

<binding name=”sqlBinding” />

Default binding configuration which can be augmented.

OutboundXmlTemplate

<InsertPurchaseOrder xmlns= http://schemas.microsoft.com/Sql/2008/05/Procedures/dbo>

<messageBody>

<bts-msg-body xmlns=”http://www.microsoft.com/schemas/bts2007″ encoding=”string”/>

</messageBody>

</InsertPurchaseOrder>

This is key parameter to make adapter accept the message and pass to stored procedure. The root element is the procedure name. The immediate child element is the procedure’s parameter. The enclosed element is to tell where to place message body.

OutboundBodyLocation

UseTemplate

Tells adapter to use template rather than raw message body.

This is another parameter which was not apparent since there’s no enumeration to provide possible values. Digging into adapter reference helps.

Add Messaging service to the Itinerary with the BRE endpoint resolver that uses policy created earlier. Then simply use one-way off-ramp. In the dynamic one-way port configuration PassThroughTransmit pipeline will do the job.

Once all these components implemented you have truly generic infrastructure to save any message to the database with minimal overhead. As new messages added, you simply create new stored procedure and rule that associate message type with it. And there are no BizTalk artifacts to deploy.

Windows Azure Service Bus Topics and Queues

A topic that I think BizTalk professional find appealing in Windows Azure is the Service Bus (part of Windows Azure AppFabric). Service Bus provides secure messaging and connectivity capabilities that enable building distributed and disconnected applications in the cloud, as well hybrid application across both on-premises and the cloud.

One of new features is topics and queues. Clemens Vasters did a talk on Windows Azure Service Bus Topics and Queues during Windows Build and an advanced one, which you find on his blog.

You can work with this new feature through Windows Azure AppFabric SDK V1.5 – September 2011. The version 1.5 SDK includes an updated Service Bus assembly that includes support for durable, or “brokered” messaging features, such as queues, topics, and subscriptions. These features require the .NET Framework version 4.0. (Note: If you have a dependency on the .NET Framework version 3.5, you should download or continue to use version 1.0 of the AppFabric SDK.)

If you have virtual machine available or laptop following requirements are applicable:

  • Account Requirements Before running the samples in the SDK, you must have one or more service namespaces. To create a Windows Azure AppFabric service namespace, you have to register or login Windows Azure Platform Portal.
  • Runtime Requirements The Windows Azure AppFabric SDK samples are available as Visual Studio 2010 solutions (C# and VB) and require Microsoft .NET Framework 4.0 higher to run. The SDK is supported on Windows Server 2008, Windows 7, Windows Vista, Windows Server 2003, and Windows XP operating systems. For Windows Server AppFabric integration, IIS7.5 is required.
Unexpected use of keyword: ’task’

Unexpected use of keyword: ’task’

I am currently using BizTalk 2010 as the integration hub between a number of systems, including Microsoft Dynamics CRM Online. The XLANG/s Orchestration compiler will complain with the following: Unexpected use of keyword: ‘task’ One of the core entities in CRM is the ‘Task’, a sub-type of an Activity in the Sales process. However, we […]
Blog Post by: Brett