如何通过BizTalk中的WCF服务通过HTTP接受Http MIME请求

时间:2013-01-03 22:47:55

标签: wcf biztalk wcf-binding biztalk-2010 biztalk-2009

我要求在BizTalk中通过HTTP接受HTTP MIME请求。

我是通过使用WCF发布向导发布我的架构来创建服务的,它适用于SOAP + WSDL Envelope Standard但是如何为HTTP / MIME Multipart消息实现相同的呢?

我尝试在管道的解码阶段提供MIME解码器组件,但它会抛出错误:

_415 Cannot process the message because the content type 'multipart/form-data; boundary=06047b04fd8d6d6866ed55ba' was not the expected type 'application/soap+xml; charset=utf-8'._

以下是我使用的示例MIME消息:

POST /core/Person HTTP/1.1 
Host: server_host:server_port
Content-Length: 244508 
Content-Type: multipart/form-data; boundary=XbCY 
--XbCY
Content-Disposition: form-data; name=“Name“
QWERTY 
--XbCY Content-Disposition: form-data; name=“Phno No" 
12234 
--XbCY 
Content-Disposition: form-data; name=“Address" 
00a0d91e6fa6 

我可以使用相同端点的相同服务吗?如果是的话,我必须在服务中做出哪些改变?

我是否必须使用任何自定义管道组件?

1 个答案:

答案 0 :(得分:0)

您需要具有HTTP类型的接收端口并使用BTSHTTPReceive.dll,因为它看起来没有SOAP信封。所以你基本上想要一个新的端点,而不是试图让WCF工作。

是的,您必须使用自定义管道组件。

此外,当您收到多部分/表单数据的MIME消息时,您需要阅读How to process “multipart/form-data” message submitted to BTSHttpReceive.dll,它会在消息中添加“MIME-Version:1.0”,以便您可以使用标准MIME解码器管道组件。 / p>

public IBaseMessage Execute(IPipelineContext pc, IBaseMessage inmsg)
    {
        IBaseMessagePart bodyPart = inmsg.BodyPart;
        if (bodyPart!=null)
        {
            byte[] prependByteData  = ConvertToBytes(prependData);
            byte[] appendByteData   = ConvertToBytes(appendData);

            string headersString = inmsg.Context.Read("InboundHttpHeaders", "http://schemas.microsoft.com/BizTalk/2003/http-properties").ToString();
            string[] headers = headersString.Split(new Char[] {'\r','\n' }, StringSplitOptions.RemoveEmptyEntries);
            string MimeHead=String.Empty;
            bool Foundit=false;
            for (int i=0;i<headers.Length;i++)
            {
                if (headers[i].StartsWith("Content-type:", true, null))
                {
                    MimeHead = headers[i];
                    Foundit = true;
                    break;
                }
            }
            if (Foundit)
            {
                StringBuilder sb = new StringBuilder();
                sb.Append(prependData);
                sb.Append("\r\n");
                sb.Append(MimeHead);
                sb.Append("\r\n");
                prependByteData = ConvertToBytes(sb.ToString());
            }

               Stream originalStrm            = bodyPart.GetOriginalDataStream();
               Stream strm = null;

               if (originalStrm != null)
               {
                         strm             = new FixMsgStream(originalStrm, prependByteData, appendByteData, resManager);
                         bodyPart.Data    = strm;
                         pc.ResourceTracker.AddResource( strm );
               }
        }

        return inmsg;
    }

如果您希望了解处理附件的方式,请参阅此博客Processing Binary Documents as XLANGMessages Through BizTalk Via Web Services

  

首先我创建了一个自定义管道组件;阅读MIME编码   使用BinaryReader将文档转换为字节数组。注意你不能使用   StreamReader因为流中的数据是base64编码而且会   包含非ASCII字符。将字节数组转换为Base64   encoded string创建类型化的XML文档并添加base64编码   字符串到其中一个元素。发送XML文档。   管道组件代码是;

public Microsoft.BizTalk.Message.Interop.IBaseMessage Execute(Microsoft.BizTalk.Component.Interop.IPipelineContext pc, Microsoft.BizTalk.Message.Interop.IBaseMessage inmsg)
   {
       var callToken = TraceManager.PipelineComponent.TraceIn(“START PIPELINE PROCESSING”);
       //Assumes inmsg.BodyPart.Data is MIME encoded = base64 encoded           
       BinaryReader binReader = new BinaryReader(inmsg.BodyPart.Data);
       byte[] dataOutAsBytes = binReader.ReadBytes((int)inmsg.BodyPart.Data.Length);
       binReader.Close();
       string dataOut = System.Convert.ToBase64String(dataOutAsBytes);
       TraceManager.PipelineComponent.TraceInfo(“Original MIME part received = ” + dataOut,callToken);
       // THIS IS THE AttachedDoc XML MESSAGE THAT WE ARE CREATING
       //<ns0:AttachedDocument xmlns:ns0=http://BT.Schemas.Internal/AttachedDocument>
       //    <ns0:FileName>FileName_0</ns0:FileName>
       //    <ns0:FilePath>FilePath_0</ns0:FilePath>
       //    <ns0:DocumentType>DocumentType_0</ns0:DocumentType>
       //    <ns0:StreamArray>GpM7</ns0:StreamArray>
       //</ns0:AttachedDocument>
       XNamespace nsAttachedDoc = XNamespace.Get(@”http://BT.Schemas.Internal/AttachedDocument”);
       XDocument AttachedDocMsg = new XDocument(
                                       new XElement(nsAttachedDoc + “AttachedDocument”,
                                       new XAttribute(XNamespace.Xmlns + “ns0″, nsAttachedDoc.NamespaceName),
                                           new XElement(nsAttachedDoc + “FileName”, “FileName_0″),
                                           new XElement(nsAttachedDoc + “FilePath”, “FilePath_0″),
                                           new XElement(nsAttachedDoc + “DocumentType”, “DocumentType_0″),
                                           new XElement(nsAttachedDoc + “StreamArray”, dataOut)
                                           )
                                       );
       dataOut = AttachedDocMsg.ToString();
       TraceManager.PipelineComponent.TraceInfo(“Created AttachedDoc msg = ” + AttachedDocMsg, callToken);
       MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes(dataOut));
       IBaseMessage outmsg = pc.GetMessageFactory().CreateMessage();
       outmsg.Context = pc.GetMessageFactory().CreateMessageContext();
       // Iterate through inbound message context properties and add to the new outbound message
       for (int contextCounter = 0; contextCounter < inmsg.Context.CountProperties; contextCounter++)
       {
           string Name;
           string Namespace;
           object PropertyValue = inmsg.Context.ReadAt(contextCounter, out Name, out Namespace);
           // If the property has been promoted, respect the settings
           if (inmsg.Context.IsPromoted(Name, Namespace))
           {
               outmsg.Context.Promote(Name, Namespace, PropertyValue);
           }
           else
           {
               outmsg.Context.Write(Name, Namespace, PropertyValue);
           }
       }
       outmsg.AddPart(“Body”, pc.GetMessageFactory().CreateMessagePart(), true);
       outmsg.BodyPart.Data = ms;
       pc.ResourceTracker.AddResource(ms);
       outmsg.BodyPart.Data.Position = 0;
       TraceManager.PipelineComponent.TraceInfo(“END PIPELINE PROCESSING”, callToken);
       TraceManager.PipelineComponent.TraceOut(callToken);
       return outmsg;
   }