0

I have a requirement to accept HTTP MIME request over HTTP in BizTalk.

I created a service by publishing my schema using WCF publishing wizard and it works fine for the SOAP+WSDL Envelope Standard but how do I implement the same for the HTTP/MIME Multipart message?

I tried giving MIME decoder component in the decode stage of pipeline but it throws an error:

_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'._

Here is my sample MIME message which i was using:

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 

Can I use the same service with the same endpoint? if so, What all changes do I have to make in my service?

Do I have to use any custom pipeline component?

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
Chopperla
  • 1
  • 3

1 Answers1

0

You will want to have a Receive Port of Type HTTP and use the BTSHTTPReceive.dll as it looks like there is no SOAP envelope. So you basically want a new endpoint rather than trying to get the WCF one working.

Yes, you will have to use a Custom Pipeline Component.

Also as you are getting MIME message which is multipart/form-data you need to read How to process “multipart/form-data” message submitted to BTSHttpReceive.dll which adds “MIME-Version: 1.0” to the message so you can use the standard MIME decoder pipeline component.

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;
    }

If you want to get fancier with how you process attachments, see this this Blog Processing Binary Documents as XLANGMessages Through BizTalk Via Web Services

First I created a custom pipeline component to; Read the MIME encoded document using a BinaryReader into a byte array. Note you cannot use a StreamReader because the data in the stream is base64 encoded and will contain non-ASCII characters. Convert the byte array to a Base64 encoded string Create the typed XML document and add the base64 encode string to one of the elements. Send the XML document back out. The pipeline component code was;

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;
   }
Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54