Consuming the Adapter from outside BizTalk Server

Exclusive offer: get 50% off this eBook here
SOA Patterns with BizTalk Server 2009

SOA Patterns with BizTalk Server 2009 — Save 50%

Implement SOA strategies for Microsoft BizTalk Server solutions with this book and eBook

$35.99    $18.00
by Richard Seroter | April 2009 | .NET Microsoft

In this article by Richard Seroter, you will learn and understand about consuming the adapter from outside BizTalk Server, WCF service reference, Auto generated IIS hosted service, and finally custom built proxy hosted service.

In addition to infrastructure-related updates such as the aforementioned platform modernization, Windows Server 2008 Hyper-V virtualization support, and additional options for fail over clustering, BizTalk Server also includes new core functionality. You will find better EDI and AS2 capabilities for B2B situations and a new platform for mobile development of RFID solutions.

One of the benefits of the new WCF SQL Server Adapter that I mentioned earlier was the capability to use this adapter outside of a BizTalk Server solution. Let's take a brief look at three options for using this adapter by itself and without BizTalk as a client or service.

Called directly via WCF service reference

If your service resides on a machine where the WCF SQL Server Adapter (and thus, the sqlBinding) is installed, then you may actually add a reference directly to the adapter endpoint.

I have a command-line application, which serves as my service client. If we right-click this application, and have the WCF LOB Adapter SDK installed, then Add Adapter Service Reference appears as an option.

Consuming the Adapter from outside BizTalk Server

Choosing this option opens our now-beloved wizard for browsing adapter metadata. As before, we add the necessary connection string details and browse the BatchMaster table and opt for the Select operation.

Consuming the Adapter from outside BizTalk Server

Unlike the version of this wizard that opens for BizTalk Server projects, notice the Advanced options button at the bottom. This button opens a property window that lets us select a variety of options such as asynchronous messaging support and suppression of an accompanying configuration file.

Consuming the Adapter from outside BizTalk Server

After the wizard is closed, we end up with a new endpoint and binding in our existing configuration file, and a .NET class containing the data and service contracts necessary to consume the service.

We should now call this service as if we were calling any typical WCF service. Because the auto-generated namespace for the data type definition is a bit long, I first added an alias to that namespace. Next, I have a routine, which builds up the query message, executes the service, and prints a subset of the response.

using DirectReference = schemas.microsoft.com.Sql._2008._05.Types.
Tables.dbo;
 …
private static void CallReferencedSqlAdapterService()
{
Console.WriteLine("Calling referenced adapter service");
TableOp_dbo_BatchMasterClient client = new TableOp_dbo_
BatchMasterClient("SqlAdapterBinding_TableOp_dbo_BatchMaster");
try
{
string columnString = "*";
string queryString = "WHERE BatchID = 1";
DirectReference.BatchMaster[] batchResult =
client.Select(columnString, queryString);
Console.WriteLine("Batch results ...");
Console.WriteLine("Batch ID: " + batchResult[0].BatchID.ToString());
Console.WriteLine("Product: " + batchResult[0].ProductName);
Console.WriteLine("Manufacturing Stage: " + batchResult[0].ManufStage);
client.Close();
Console.ReadLine();
}
catch (System.ServiceModel.CommunicationException){client.Abort(); }
catch (System.TimeoutException) { client.Abort(); }
catch (System.Exception) { client.Abort(); throw; }
}

Once this quick block of code is executed, I can confirm that my database is accessed and my expected result set returned.

Consuming the Adapter from outside BizTalk Server

SOA Patterns with BizTalk Server 2009 Implement SOA strategies for Microsoft BizTalk Server solutions with this book and eBook
Published: April 2009
eBook Price: $35.99
Book Price: $59.99
See more
Select your format and quantity:

Auto-generated IIS-hosted service

The option above is quite easy to use, but does rely on the service client having the sqlBinding installed on their machine. That's not a very service-oriented requirement for us to impose on our clients. Instead, we should make this service available through traditional HTTP protocols. We could do this by hand, or, use the handy wizard installed in the WCF LOB Adapter SDK directory.

This wizard takes the SQL Server Adapter and generates a proxy IIS-friendly web service. To access it, right-click your Visual Studio.NET solution file and choose to Add New Web Site. If you have Visual C# chosen as the Language, you should see an option called WCF Adapter Service.

Consuming the Adapter from outside BizTalk Server

What you get next is a wizard, which walks you through the selection of operations to expose as an HTTP service.

Consuming the Adapter from outside BizTalk Server

After selecting our connection string, we once again should choose the Select operation on the BatchMaster table.

Consuming the Adapter from outside BizTalk Server

Next, we are asked to provide a subset of the service and endpoint behavior settings. Notice that I've switched UseServiceCertificate to false which eliminates the requirement to use SSL.

Consuming the Adapter from outside BizTalk Server

Now we provide additional binding configuration data. I changed the BindingConfiguration property to use the None security setting instead of Transport.

Consuming the Adapter from outside BizTalk Server

The final Summary pane lets us review all of our chosen settings and finally builds the physical service for us. The wizard didn't completely configure our service bindings as much as I'd like, so load the generated configuration file (web.config, in this case) into the WCF Service Configuration Editor. Specifically, I added a metadata browsing behavior (to allow WSDL interrogation), and also set a credential/authorization scheme to use Window integrated security.

Consuming the Adapter from outside BizTalk Server

With all of this in place, I added this service to IIS 7.0 and included a traditional WCF service reference to this endpoint from my Visual Studio.NET project. Instead of the client requiring the sqlBinding to be installed on the local machine, this service now relies on good old cross-platform basic HTTP communication.

As you can tell here, the client code to call the service is remarkably similar to the code needed by a direct adapter invocation demonstrated previously.

private static void CallAutoGeneratedSqlAdapterService()
{
Console.WriteLine("Calling auto-generated adapter service");
AdapterGeneratedSvc.TableOp_dbo_BatchMasterClient client =
new AdapterGeneratedSvc.TableOp_dbo_BatchMasterClient("TableOp_dbo_
BatchMasterEndpoint");
try
{
string columnString = "*";
string queryString = "WHERE BatchID = 1";
AdapterGeneratedSvc.BatchMaster[] batchResults =
client.Select(columnString, queryString);
Console.WriteLine("Batch results ...");
Console.WriteLine("Batch ID: " + batchResults[0].BatchID.ToString());
Console.WriteLine("Product: " + batchResults[0].ProductName);
Console.WriteLine("Manufacturing Stage: " + batchResults[0].
ManufStage);
client.Close();
Console.ReadLine();
}
catch (System.ServiceModel.CommunicationException){client.Abort(); }
catch (System.TimeoutException) { client.Abort(); }
catch (System.Exception) { client.Abort(); throw; }
}

Consuming the Adapter from outside BizTalk Server

Directly exposing the sqlBinding or auto-generating an HTTP endpoint are both fairly straightforward and easy ways to consume the adapter, but they violate the SOA tenets of loose coupling and abstraction. We require intimate knowledge of our underlying data source and are executing low-level queries against it. Ideally, we should be accessing this service in a more business-service friendly way where both the underlying data source and query plumbing are hidden from view.

Custom built proxy IIS-hosted service

Since the WCF SQL Server Adapter does most of the hard work for us, the least we can do is slap a simple façade on the adapter and abstract the implementation details away from the service clients. We are able to do this by writing a wrapper service which does nothing but front the adapter.

Let's start with a simple WCF contract and data type definition housed in a new Visual Studio.NET class library project. Our wrapper service takes in a batch ID and returns back a MaterialBatch canonical object. Notice no tight coupling to where this data actually resides.

[ServiceContract]
public interface IBatchMaster
{
[OperationContract]
MaterialBatch GetBatchByID(int batchID);
}
[DataContract]
public class MaterialBatch
{
[DataMember]
public int? BatchID { get; set; }
[DataMember]
public int? MaterialCode { get; set; }
[DataMember]
public string ProductName { get; set; }
[DataMember]
public int? Quantity { get; set; }
[DataMember]
public string UnitOfMeasure { get; set; }
[DataMember]
public string ManufacturingStage { get; set; }
[DataMember]
public string ManufacturingDate { get; set; }
[DataMember]
public string SiteID { get; set; }
[DataMember]
public int? BuildingID { get; set; }
[DataMember]
public string Status { get; set; }
}

Just like the previous demo, we add a service reference directly to the WCF SQL Server adapter. Now in our custom service implementation, we call the adapter service using the single parameter passed in from the client.

public class BatchMasterService : IBatchMaster
{
public MaterialBatch GetBatchByID(int batchID)
{
TableOp_dbo_BatchMasterClient client = new TableOp_dbo_
BatchMasterClient("SqlAdapterBinding_TableOp_dbo_BatchMaster");
try
{
string columnString = "*";
string queryString = "WHERE BatchID = " + batchID.ToString();
BatchMaster[] batchResults =
client.Select(columnString, queryString);
client.Close();
if (batchResults.Length > 0)
{
MaterialBatch batchResult = new MaterialBatch();
batchResult.BatchID = batchResults[0].BatchID;
batchResult.MaterialCode = batchResults[0].MaterialCode;
batchResult.ProductName = batchResults[0].ProductName;
batchResult.Quantity = batchResults[0].Quantity;
batchResult.UnitOfMeasure = batchResults[0].Unit;
batchResult.ManufacturingStage = batchResults[0].ManufStage;
batchResult.ManufacturingDate = batchResults[0].ManufDate;
batchResult.SiteID = batchResults[0].ManufSiteID;
batchResult.BuildingID = batchResults[0].SiteBuildingID;
batchResult.Status = batchResults[0].Status;
return batchResult;
}
else
{
return null;
}
}
catch (System.ServiceModel.CommunicationException){client.Abort(); }
catch (System.TimeoutException) { client.Abort(); }
catch (System.Exception) { client.Abort(); throw; }
return null;
}
}

Once we're satisfied with this service library, we next create a new WCF Service project in Visual Studio.NET and use the above class as the implementation logic of the service. This service has both client and service binding configurations because it receives inbound HTTP requests and calls into the SQL Server adapter through the sqlBinding binding.

Consuming the Adapter from outside BizTalk Server

After we add a reference to this new WCF service from within our client application, we call our application through a much more refined, business-service friendly way.

private static void CallProxySqlAdapterService()
{
Console.WriteLine("Calling adapter proxy service");
AdapterProxySvc.BatchMasterClient client =
new AdapterProxySvc.BatchMasterClient("basicHttpEndpoint");
try
{
AdapterProxySvc.MaterialBatch batchResult = client.GetBatchByID(1);
Console.WriteLine("Batch results ...");
Console.WriteLine("Batch ID: " + batchResult.BatchID.ToString());
Console.WriteLine("Product: " + batchResult.ProductName);
Console.WriteLine("Manufacturing Stage: " +
batchResult.ManufacturingStage);
client.Close();
Console.ReadLine();
}
catch (System.ServiceModel.CommunicationException){client.Abort(); }
catch (System.TimeoutException) { client.Abort(); }
catch (System.Exception) { client.Abort(); throw; }
}

Once this code is executed, we are able to confirm a successful invocation.

Consuming the Adapter from outside BizTalk Server

We've seen three ways to call the WCF SQL Server Adapter without the help of BizTalk Server. While it is perfectly valid to expose adapter execution directly to clients, there are long-term service-orientation benefits to putting a small layer of abstraction on top of the native adapter operations.

Summary

We have covered consuming the adapter from outside BizTalk Server,WCF service reference, Auto generated IIS hosted service, and finally custom built proxy hosted service.As you can now define SQL Server-based services outside of BizTalk Server, consider whether or not it makes sense to define a particular operation as a standalone service that BizTalk (and others) can consume, or whether you should use the WCF-SQL binding directly within a BizTalk send port. Each situation is different, but it's great to now have such choices.


If you have read this article you may be interested to view :


SOA Patterns with BizTalk Server 2009 Implement SOA strategies for Microsoft BizTalk Server solutions with this book and eBook
Published: April 2009
eBook Price: $35.99
Book Price: $59.99
See more
Select your format and quantity:

About the Author :


Richard Seroter

Richard Seroter is a solutions architect for an industry-leading biotechnology company, a Microsoft MVP for BizTalk Server, and a Microsoft Connected Systems Advisor. He has spent the majority of his career consulting with customers as they planned and implemented their enterprise software solutions. Richard worked first for two global IT consulting firms, which gave him exposure to a diverse range of industries, technologies, and business challenges. Richard then joined Microsoft as a SOA/BPM technology specialist where his sole objective was to educate and collaborate with customers as they considered, designed, and architected BizTalk solutions. One of those customers liked him enough to bring him onboard full time as an architect after they committed to using BizTalk Server as their enterprise service bus. Once the BizTalk environment was successfully established, Richard transitioned into a solutions architect role where he now helps identify enterprise best practices and applies good architectural principles to a wide set of IT initiatives.

Richard maintains a semi-popular blog of his exploits, pitfalls, and musings with BizTalk Server and enterprise architecture at http://seroter.wordpress.com.

The authors have provided a website with further information about the book here: http://appliedarchitecturepatterns.com/

 

Books From Packt

C# 2008 and 2005 Threaded Programming: Beginner's Guide
C# 2008 and 2005 Threaded Programming: Beginner's Guide

Implementing Microsoft Dynamics NAV 2009
Implementing Microsoft Dynamics NAV 2009

VSTO 3.0 for Office 2007 Programming
VSTO 3.0 for Office 2007 Programming

Learning SQL Server 2008 Reporting Services
Learning SQL Server 2008 Reporting Services

Small Business Server 2008 – Installation, Migration, and Configuration
Small Business Server 2008 – Installation, Migration, and Configuration

Software Testing with Visual Studio Team System 2008
Software Testing with Visual Studio Team System 2008

ASP.NET 3.5 Social Networking
ASP.NET 3.5 Social Networking

WCF Multi-tier Services Development with LINQ
WCF Multi-tier Services Development with LINQ

No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
1
D
P
M
d
W
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software