更多内容请查看:BizTalk动手实验系列目录
BizTalk 开发系列
1 课程简介
通过本课程熟悉自定义开始管道组件的流程、各组件接口的功能作用以及自定义管道。
本场景为开发一个消息ZIP压缩的发送管道组件。
2 准备工作
1. 熟悉管道组件各阶段组成
2. 下载Ionic.ZIP 组件(http://dotnetzip.codeplex.com/),使用如下代码部署到GAC中。
"
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe
" /i
"
{Path}\Ionic.Zip.dll
" /F
3 演示
1. 创建空解决方案ZipEncoder
2. 创建组件类库

3. 为类库添加签名
4. 添加自动部署GAC脚本,在项目属性Build Events的Post-build event command line里输入下列命令
"
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe
" /i
"
$(TargetPath)
" /F
5. 引用Microsoft.BizTalk.Pipeline组件(C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\PublicAssemblies\Microsoft.BizTalk.Pipeline.dll)及Ionic.Zip组件(需部署到GAC中)
6. 重命名Class1.cs为ZipEncoderComponent.cs
7. 拷贝以下代码替换ZipEncoder
View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using Microsoft.BizTalk.Message.Interop;
using Microsoft.BizTalk.Component.Interop;
using Ionic.Zip;
namespace ZipEncoderComponent
{
[ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
[ComponentCategory(CategoryTypes.CATID_Encoder)]
[System.Runtime.InteropServices.Guid(
"
bb93175b-ef93-4277-b725-4e835d48a57b
")]
public
class ZipEncoderComponent : IBaseComponent, IComponentUI, IPersistPropertyBag, IComponent
{
#region IBaseComponent
public
string Description
{
get {
return
"
zip encoder component
"; }
}
public
string Name
{
get {
return
"
ZipEncoder
"; }
}
public
string Version
{
get {
return
"
1.0
"; }
}
#endregion
#region IComponentUI
public IntPtr Icon
{
get {
return
new IntPtr(); }
}
public System.Collections.IEnumerator Validate(
object projectSystem)
{
return
null;
}
#endregion
#region IPersistPropertyBag
private
string _password;
private
string _entryExtension;
public
string Password
{
get {
return _password; }
set { _password = value; }
}
public
string EntryExtension
{
get {
return _entryExtension; }
set { _entryExtension = value; }
}
public
void GetClassID(
out Guid classID)
{
classID =
new Guid(
"
62ab1278-2a73-49ea-832c-740e045a2d7d
");
}
public
void InitNew()
{
}
public
void Load(IPropertyBag propertyBag,
int errorLog)
{
object val1 =
null;
object val2 =
null;
try
{
propertyBag.Read(
"
Password
",
out val1,
0);
propertyBag.Read(
"
EntryExtension
",
out val2,
0);
}
catch (ArgumentException)
{
}
catch (Exception ex)
{
throw
new ApplicationException(
"
Error reading PropertyBag:
" + ex.Message);
}
if (val1 !=
null)
_password = (
string)val1;
if (val2 !=
null)
_entryExtension = (
string)val2;
else
_entryExtension =
"
xml
";
}
public
void Save(IPropertyBag propertyBag,
bool clearDirty,
bool saveAllProperties)
{
object val1 = (
object)_password;
object val2 = (
object)_entryExtension;
propertyBag.Write(
"
Password
",
ref val1);
propertyBag.Write(
"
EntryExtension
",
ref val2);
}
#endregion
#region IComponent
public IBaseMessage Execute(IPipelineContext pContext, IBaseMessage pInMsg)
{
IBaseMessagePart bodyPart = pInMsg.BodyPart;
if (bodyPart !=
null)
{
Stream originalStream = bodyPart.GetOriginalDataStream();
if (originalStream !=
null)
{
MemoryStream memStream =
new MemoryStream();
using (ZipOutputStream zipOutputStream =
new ZipOutputStream(memStream))
{
byte[] buffer =
new Byte[
1024];
zipOutputStream.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
if (_password !=
null)
zipOutputStream.Password = _password;
if (_entryExtension !=
null)
if (_entryExtension.Length >
0)
_entryExtension =
"
.
" + _entryExtension;
zipOutputStream.PutNextEntry(pInMsg.MessageID + _entryExtension);
int bytesRead =
1024;
while (bytesRead !=
0)
{
bytesRead = originalStream.Read(buffer,
0, buffer.Length);
zipOutputStream.Write(buffer,
0, bytesRead);
}
}
byte[] buff = memStream.GetBuffer();
MemoryStream ms =
new MemoryStream(buff);
ms.Position =
0;
pInMsg.BodyPart.Data = ms;
}
}
return pInMsg;
}
#endregion
}
}
8. 创建BizTalk项目BTSZipApp(配置应用名称、密钥)
9. 新建发送管道SendZipPipeline
10. 打开SendZipPipeline,在管道设计器右侧工具栏中右键点击“Choose Items”

11. 在BizTalk管道组件中点击浏览选择部署到GAC的管道组件(C:\Windows\Microsoft.NET\assembly\GAC_MSIL\ZipEncoderComponent\v4.0_1.0.0.0__9e391a29d05958dc)

12. 将ZipEncoder托拽到发送管道的Encode阶段

13. 部署BizTalk项目,重启BizTalk实例。
14. 新创建接收端口用于接收XML消息

15. 新建发送端口,用于发送ZIP文件,将管道选为新建的管道

16. 为发送端口添加订阅,以实现接收来自接收端口ReceivePort7的消息(该接收端口的名称请换为实际的名称)

17. 启动应用程序,将测试XML放到接收文件夹,在发送文件夹里可以看到ZIP文件
