Pro BizTalk 2006 phần 5 ppt

52 147 0
Pro BizTalk 2006 phần 5 ppt

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

P ublic Sub Save(ByVal propertyBag As IPropertyBag, ByVal clearDirty As_ Boolean, ByVal saveAllProperties As Boolean) Dim val As Object v al = CObj(_recipient) WritePropertyBag(propertyBag, "Recipient", val) val = CObj(_PGPBinDirectory) WritePropertyBag(propertyBag, "PGPBinDirectory", val) End Sub #End Region #Region "IComponent Members" Public Function Execute(ByVal pContext As IPipelineContext, ByVal pInMsg As_ Microsoft.BizTalk.Message.Interop.IBaseMessage) As_ Microsoft.BizTalk.Message.Interop.IBaseMessage Try If Not pInMsg Is Nothing Then Dim originalStream As Stream = _ pInMsg.BodyPart.GetOriginalDataStream() pInMsg.BodyPart.Data = Encode(originalStream) pContext.ResourceTracker.AddResource(pInMsg.BodyPart.Data) End If Catch ex As Exception System.Diagnostics.Debug.WriteLine("Exception caught in_ ABC.BizTalk.PipelineComponents.PGPEncodeComponent::Execute: " & ex.Message) End Try Return pInMsg End Function #End Region #Region "IComponentUI Members" ' <summary> 'The Validate method is called by the BizTalk Editor during the build 'of a BizTalk project. '</summary> '<param name="obj">An Object containing the configuration 'properties.</param> '<returns>The IEnumerator enables the caller to enumerate through a 'collection of strings containing error messages. These error messages 'appear as compiler error messages. To report successful property 'validation, the method should return an empty enumerator.</returns> Public Function Validate(ByVal projectSystem As Object) As IEnumerator CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 183 6994ch05final.qxd 10/2/06 12:33 AM Page 183 ' example implementation: ' ArrayList errorList = new ArrayList(); ' errorList.Add("This is a compiler error"); ' return errorList.GetEnumerator(); Return Nothing End Function #End Region #Region "Utility Functions" Public Shared Sub DumpStreamToFile(ByVal fromStream As Stream, ByVal _ toFilename As String) Dim file As FileStream = Nothing Try file = New FileStream(toFilename, System.IO.FileMode.Create) Dim tmpBuff As Byte() = New Byte(4095) {} Dim bytesRead As Integer = 0 bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) memStream.Write(tmpBuff, 0, bytesRead) Do While bytesRead <> 0 bytesRead = fromStream.Read(tmpBuff, 0, tmpBuff.Length) file.Write(tmpBuff, 0, bytesRead) Loop file.Close() file = Nothing Finally If Not file Is Nothing Then file.Close() End If End Try End Sub Public Shared Function ReadFileToMemoryStream(ByVal fromFilename As String)_ As MemoryStream Dim file As FileStream = Nothing Try file = New FileStream(fromFilename, System.IO.FileMode.Open) Dim memStream As MemoryStream = New MemoryStream() Dim tmpBuff As Byte() = New Byte(4095) {} bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) memStream.Write(tmpBuff, 0, bytesRead) Dim bytesRead As Integer = bytesRead = file.Read(tmpBuff, 0, _ tmpBuff.Length) CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES184 6994ch05final.qxd 10/2/06 12:33 AM Page 184 D o While bytesRead <> 0 memStream.Write(tmpBuff, 0, bytesRead) bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) L oop file.Close() file = Nothing memStream.Position = 0 Return memStream Finally If Not file Is Nothing Then file.Close() End If End Try End Function Public Shared Function ReadPropertyBag(ByVal pb As_ Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As _ String) As Object Dim val As Object = Nothing Try pb.Read(propName, val, 0) Catch e1 As ArgumentException Return val Catch ex As Exception Throw New ApplicationException(ex.Message) End Try Return val End Function ''' <summary> ''' Writes property values into a property bag. ''' </summary> ''' <param name="pb">Property bag.</param> ''' <param name="propName">Name of property.</param> ''' <param name="val">Value of property.</param> Public Shared Sub WritePropertyBag(ByVal pb As_ Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As String, ByVal _ val As Object) Try pb.Write(propName, val) Catch ex As Exception Throw New ApplicationException(ex.Message) End Try End Sub CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 185 6994ch05final.qxd 10/2/06 12:33 AM Page 185 # End Region End Class E nd Namespace PGP Decode Component The following code example is the counterpart to the encoding component shown in the pre- ceding subsection. The Decoding component is used on the receive side of BizTalk and is used when encrypted messages are received into BizTalk. Imports Microsoft.VisualBasic Imports System Imports System.ComponentModel Imports System.Collections Imports System.Diagnostics Imports System.Drawing Imports System.IO Imports System.Reflection Imports Microsoft.BizTalk.Component.Interop Imports ABC.BizTalk.PipelineComponents.PGPUtilities Namespace Microsoft.Utility.PipelineGnuPG <ComponentCategory(CategoryTypes.CATID_PipelineComponent),_ ComponentCategory(CategoryTypes.CATID_Decoder),_ System.Runtime.InteropServices.Guid("AEE2E180-8E4F-426d-9E39-C314E09F977E")> _ Public Class PGPDecodeComponent Implements IBaseComponent, Microsoft.BizTalk.Component.Interop.IComponent,_ Microsoft.BizTalk.Component.Interop.IPersistPropertyBag, IComponentUI ' Component information #Region "IBaseComponent" <Browsable(False)> _ Public ReadOnly Property Name() As String Get Return "PGP decoder" End Get End Property <Browsable(False)> _ Public ReadOnly Property Version() As String Get Return "1.0" End Get End Property CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES186 6994ch05final.qxd 10/2/06 12:33 AM Page 186 < Browsable(False)> _ Public ReadOnly Property Description() As String Get R eturn "PGG Decode Pipeline Component" End Get End Property <Browsable(False)> _ Public ReadOnly Property Icon() As System.IntPtr Get Return (CType(resourceManager.GetObject("IconBitmap"),_ Bitmap)).GetHicon() End Get End Property #End Region Private resourceManager As System.Resources.ResourceManager = New_ System.Resources.ResourceManager("ABC.BizTalk.PipelineComponents",_ System.Reflection.Assembly.GetExecutingAssembly()) ' Property: Passphrase Private _passphrase As String Public Property Passphrase() As String Get Return _passphrase End Get Set(ByVal value As String) _passphrase = value End Set End Property ' Property: GnuPGBinDir Private _PGPBinDirectory As String Public Property PGPBinDirectory() As String Get Return _PGPBinDirectory End Get Set(ByVal value As String) _PGPBinDirectory = value End Set End Property Private Function Decode(ByVal inStream As System.IO.Stream) As Stream Dim inFile As String = Path.GetTempFileName() Dim outFile As String = Path.ChangeExtension(inFile, "txt") CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 187 6994ch05final.qxd 10/2/06 12:33 AM Page 187 T ry DumpStreamToFile(inStream, inFile) D im GPG As GnuPGWrapper = New GnuPGWrapper(_PGPBinDirectory) Dim GPGCommand As GnuPGCommand = GPG.Command GPGCommand.Command = Commands.Decrypt GPGCommand.InputFile = inFile GPGCommand.OutputFile = outFile GPGCommand.Passphrase = _passphrase 'TODO: support encrypted passphrase, no passphrase is a security 'risk GPG.Execute(Nothing) outStream = ReadFileToMemoryStream(outFile) Catch ex As Exception System.Diagnostics.Debug.WriteLine(ex) Throw ex Finally If File.Exists(inFile) Then File.Delete(inFile) End If If File.Exists(outFile) Then File.Delete(outFile) End If End Try Return outStream End Function #Region "IPersistPropertyBag Members" Public Sub InitNew() End Sub Public Sub GetClassID(<System.Runtime.InteropServices.Out()> ByRef _ classID As Guid) classID = New Guid("4FC12033-D0BD-4298-BB31-FBDBA72F5961") End Sub Public Sub Load(ByVal propertyBag As IPropertyBag, ByVal _ errorLog As Integer) Dim text As String CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES188 6994ch05final.qxd 10/2/06 12:33 AM Page 188 t ext = CStr(ReadPropertyBag(propertyBag, "Passphrase")) If Not text Is Nothing Then _passphrase = text E nd If text = CStr(ReadPropertyBag(propertyBag, "PGPBinDirectory")) If Not text Is Nothing Then _PGPBinDirectory = text End If End Sub Public Sub Save(ByVal propertyBag As IPropertyBag, ByVal clearDirty As_ Boolean, ByVal saveAllProperties As Boolean) Dim val As Object val = CObj(_passphrase) WritePropertyBag(propertyBag, "Passphrase", val) val = CObj(_PGPBinDirectory) WritePropertyBag(propertyBag, "PGPBinDirectory", val) End Sub #End Region #Region "IComponent Members" Public Function Execute(ByVal pContext As IPipelineContext, ByVal pInMsg As_ Microsoft.BizTalk.Message.Interop.IBaseMessage) As_ Microsoft.BizTalk.Message.Interop.IBaseMessage Try If Not pInMsg Is Nothing Then Dim originalStream As Stream =_ pInMsg.BodyPart.GetOriginalDataStream() pInMsg.BodyPart.Data = Decode(originalStream) pContext.ResourceTracker.AddResource(pInMsg.BodyPart.Data) End If Catch ex As Exception System.Diagnostics.Debug.WriteLine("Exception caught in_ ABC.BizTalk.PipelineComponents.PGPDecodeComponent::Execute: " & ex.Message) Throw ex End Try Return pInMsg End Function #End Region CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 189 6994ch05final.qxd 10/2/06 12:33 AM Page 189 # Region "IComponentUI Members" '<summary> ' The Validate method is called by the BizTalk Editor during the build 'of a BizTalk project. '</summary> '<param name="obj">An Object containing the configuration properties.</param> '<returns>The IEnumerator enables the caller to enumerate through a_ 'collection of strings containing error messages. These error messages appear 'as compiler error messages. To report successful property validation, the 'method should return an empty enumerator.</returns> Public Function Validate(ByVal projectSystem As Object) As IEnumerator ' example implementation: ' ArrayList errorList = new ArrayList(); ' errorList.Add("This is a compiler error"); ' return errorList.GetEnumerator(); Return Nothing End Function #End Region #Region "Utility Functions" Public Shared Sub DumpStreamToFile(ByVal fromStream As Stream, ByVal_ toFilename As String) Dim file As FileStream = Nothing Try file = New FileStream(toFilename, System.IO.FileMode.Create) Dim tmpBuff As Byte() = New Byte(4095) {} Dim bytesRead As Integer = 0 bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) memStream.Write(tmpBuff, 0, bytesRead) Do While bytesRead <> 0 bytesRead = fromStream.Read(tmpBuff, 0, tmpBuff.Length) file.Write(tmpBuff, 0, bytesRead) Loop file.Close() file = Nothing Finally If Not file Is Nothing Then file.Close() End If End Try End Sub CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES190 6994ch05final.qxd 10/2/06 12:33 AM Page 190 P ublic Shared Function ReadFileToMemoryStream(ByVal fromFilename _ As String) As MemoryStream Dim file As FileStream = Nothing T ry file = New FileStream(fromFilename, System.IO.FileMode.Open) Dim memStream As MemoryStream = New MemoryStream() Dim tmpBuff As Byte() = New Byte(4095) {} Dim bytesRead As Integer = 0 bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) memStream.Write(tmpBuff, 0, bytesRead) Do While bytesRead <> 0 bytesRead = file.Read(tmpBuff, 0, tmpBuff.Length) memStream.Write(tmpBuff, 0, bytesRead) Loop file.Close() file = Nothing memStream.Position = 0 Return memStream Finally If Not file Is Nothing Then file.Close() End If End Try End Function Public Shared Function ReadPropertyBag(ByVal pb As_ Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As String) _ As Object Dim val As Object = Nothing Try pb.Read(propName, val, 0) Catch e1 As ArgumentException Return val Catch ex As Exception Throw New ApplicationException(ex.Message) End Try Return val End Function ''' <summary> ''' Writes property values into a property bag. ''' </summary> CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 191 6994ch05final.qxd 10/2/06 12:33 AM Page 191 ' '' <param name="pb">Property bag.</param> ''' <param name="propName">Name of property.</param> ''' <param name="val">Value of property.</param> P ublic Shared Sub WritePropertyBag(ByVal pb As_ Microsoft.BizTalk.Component.Interop.IPropertyBag, ByVal propName As String, ByVal _ val As Object) Try pb.Write(propName, val) Catch ex As Exception Throw New ApplicationException(ex.Message) End Try End Sub #End Region End Class End Namespace The Databased Disassembler Often, people need to get information from a database and submit it to BizTalk or have BizTalk send it out to a third-party destination. The usual response for this is to use the appropriate database adapter, generate the schemas for the SQL statement or the stored procedure, and use some combination of an orchestration/port and adapter to generate the data, publish it to the Messagebox, and send it to the appropriate destination. While this solution works, it often is met with a response like “But I just want to call a stored procedure and have each row be sent to BizTalk as an XML document.” Our solution to this scenario is called the Databased Disassembler (yes, it is a pun on words as the data is based on a database). The walkthrough for how this solution works is as follows: 1. A receive pipeline is created that hosts the custom Disassembler component. 2. The Disassembler only accepts a primer message. A primer message is a message that contains all the parameters needed for the pipeline component to begin execut- ing. It is a primer because it is a message that gets the process going, or “primes” it. The message itself contains data that is not meaningful to any one but the Databased Disassembler pipeline component. 3. The pipeline component examines the primer message and retrieves all the parame- ters needed for execution. These parameters can include a. The connection string for the database b. The stored procedure name c. The par ameters needed to call the stor ed pr ocedur e d. The resulting schema type for the returned data CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES192 6994ch05final.qxd 10/2/06 12:33 AM Page 192 [...]... Microsoft Visual Studio 20 05 2 On the File menu, point to New, and then click Project 3 Click Visual Basic Projects under Project Types, and then click Windows Service under Templates 4 Type BizTalkSuspendedMessageHandlerService in the Name text box Change location if necessary Click OK 5 In the Code Editor window, right-click Design View, and then click Properties 6 In the Properties pane, click the... sequence, and promote that property along with the unique sequence ID and the last message in the sequence property Also in this case, you need to use another message part feature—they have a property bag So instead of promoting the values to the message context of the message, you simply write these values as key/value pairs to the parts property bag Once all the message parts are resequenced properly,... writing a stored procedure and getting a connection string to access the data source There is no need to purchase additional adapters for this solution to be used across different databases This is a key selling point for many projects that might want to use this approach 6994ch05final.qxd 10/2/06 12:33 AM Page 1 95 CHAPTER 5 s PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES Figure 5- 1 Databased Disassembler... messages An implementation of an aggregating pipeline is given in the BizTalk SDK under the Program Files\ Microsoft BizTalk Server 2006\ SDK\Pipelines directory Another choice would be to create a NET component to accept the messages as they are received and aggregate them together 6994ch06final.qxd 10/2/06 12:30 AM Page 203 CHAPTER 6 s BIZTALK DESIGN PATTERNS AND PRACTICES 4 Drag a port to be used as... The standard also has strict requirements about the order in which messages are processed in relation to the order in which they were received An HL7 accelerator is available for BizTalk and is downloadable from www.microsoft.com /biztalk/ evaluation/hl7/default.mspx 6994ch06final.qxd 10/2/06 12:30 AM Page 2 05 CHAPTER 6 s BIZTALK DESIGN PATTERNS AND PRACTICES • What number the message is in the sequence... used to signify the start of the processes: 6 Many other approaches to this problem are available A second potential solution would be to have a BizTalk transformation that maps... implementation 1 This section originally appeared in BizTalk Server 2004 Implementing Dynamic Parallel Orchestrations,” (REED0019 65) .doc Copyright by Microsoft Corporation Reprinted with permission from Microsoft Corporation 197 6994ch06final.qxd 198 10/2/06 12:30 AM Page 198 CHAPTER 6 s BIZTALK DESIGN PATTERNS AND PRACTICES One approach to solve this problem is to use the Recipient List pattern as described... the Add Installer link 7 Change the display name to BizTalk Suspended Message Handler Service 8 In the Properties pane for serviceInstaller1, change the ServiceName property to Service1 s Note The ServiceName property needs to match the name of the service class 9 Change StartType to Automatic 10 In the Code Editor window in Design view, click serviceProcessInstaller1 Note that the account is set to... approach as it served the purpose of illustrating this technique the best 193 6994ch05final.qxd 194 10/2/06 12:33 AM Page 194 CHAPTER 5 s PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES . (www.apress.com). CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES 1 95 Figure 5- 1. Databased Disassembler execution flow 6994ch05final.qxd 10/2/06 12:33 AM Page 1 95 6994ch05final.qxd 10/2/06. Shared Function ReadPropertyBag(ByVal pb As_ Microsoft .BizTalk. Component.Interop.IPropertyBag, ByVal propName As _ String) As Object Dim val As Object = Nothing Try pb.Read(propName, val, 0) Catch. Guid("4FC12033-D0BD-4298-BB31-FBDBA72F5961") End Sub Public Sub Load(ByVal propertyBag As IPropertyBag, ByVal _ errorLog As Integer) Dim text As String CHAPTER 5 ■ PIPELINE COMPONENT BEST PRACTICES AND EXAMPLES188 6994ch05final.qxd

Ngày đăng: 14/08/2014, 11:21

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan