none
upload or send the file to WCF web service RRS feed

  • Question

  • Hi All,

    I published a biztalk orchestration as WCF Service hosted by iis. How can I achieve to upload the files from the local folder to this WCF service directly using C#, not use the biztalk receive port, I want to submit the xml or EDI file to this service directly in client application using code as following, is this code OK or not? Does anyone can help me? I am trying to send the xml and EDI files to this web service. And I use the xmlReceive or EDI receive pipeline and passthroughtransmit sendpipeline for this WCF service receivelocation in BizTalk Server.

    ServiceClient client = new ServiceClient("BasicHttpBinding_ITwoWayAsync");
    FileStream originalstream = new FileStream(@"Directory+filename", FileMode.Create, FileAccess.ReadWrite,FileShare.None);
    Stream memStream = new MemoryStream();
    byte[] buffer = new Byte[1024];
    int bytesRead = 1024;
    while (bytesRead != 0)
    {
    bytesRead = originalstream.Read(buffer, 0, buffer.Length);
    memStream.Write(buffer, 0, bytesRead);
    }
    Console.WriteLine(memStream);
    object msg;
    msg = memStream;
    client.TransformEDI(ref msg);
    string EDIout = (string)msg;
    Console.WriteLine(EDIout);
    Console.ReadLine();
    The WCF Service method is as follows:
            [System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
            EDITransformWCF.EDITransform.TransformEDIResponse EDITransformWCF.EDITransform.EDITransformWCFBizTalk_EDITransformWCFOrch_TransformEDI.TransformEDI(EDITransformWCF.EDITransform.TransformEDIRequest request) {
                return base.Channel.TransformEDI(request);
            }
            
            public void TransformEDI(ref object part) {
                EDITransformWCF.EDITransform.TransformEDIRequest inValue = new EDITransformWCF.EDITransform.TransformEDIRequest();
                inValue.part = part;
                EDITransformWCF.EDITransform.TransformEDIResponse retVal = ((EDITransformWCF.EDITransform.EDITransformWCFBizTalk_EDITransformWCFOrch_TransformEDI)(this)).TransformEDI(inValue);
                part = retVal.part;
            }
    The error I got is like this:

    There was an error while trying to serialize parameter :part. The InnerException message was 'Type 'System.IO.MemoryStream' with data contract name 'MemoryStream:http://schemas.datacontract.org/2004/07/System.IO' is not expected. Consider using a DataContractResolver or add any types not known statically to the list of known types - for example, by using the KnownTypeAttribute attribute or by adding them to the list of known types passed to DataContractSerializer.'.  Please see InnerException for more details.

    • Edited by R.Zi Monday, March 18, 2013 4:30 PM
    Monday, March 18, 2013 4:25 PM

Answers

  • Hi R.Zi,

    The problem is MemoryStream Cant be serialized directly since it is class type. Convert the MemoryStream to byte[] and then send to the service. I think there wont be any issues if you flow this approach.

    In your code instead of sending the memStream (MemoryStream) to the Service, send buffer (byte[]).

    ServiceClient client = new ServiceClient("BasicHttpBinding_ITwoWayAsync");
    FileStream originalstream = new FileStream(@"Directory+filename", FileMode.Create, FileAccess.ReadWrite,FileShare.None);
    Stream memStream = new MemoryStream();
    byte[] buffer = new Byte[1024];
    int bytesRead = 1024;
    while (bytesRead != 0)
    {
    bytesRead = originalstream.Read(buffer, 0, buffer.Length);
    memStream.Write(buffer, 0, bytesRead);
    }
    Console.WriteLine(memStream);
    object msg;
    msg = memStream;
    client.TransformEDI(buffer);
    string EDIout = (string)msg;
    Console.WriteLine(EDIout);
    Console.ReadLine();


    srithar

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 19, 2013 12:13 PM
  • Try experimenting with the BizTalk Adapter settings and specifying an XPath into your message body.


    David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 26, 2013 1:37 PM
  • Hi David,

    Thanks for your tips and thanks for your always help. Finally I find the reasons for the errors and fix the issue. That's because the BizTalk published WCF service automatically creates the hidden <part></part> xml element note to wrap the string contents I send to it in the request message and the response message. So inside the message panel of WCF-BasicHttp Transport Properties as you point out, I need change the request Body Path and response Body Path as attached picture. For request, use BodyPath to get the string contents in the <part> element, for response, add the <part> note outside the message body. Then the problems are solved.

    Thanks for your help, and thanks for your nice. I appreciate.

    R.ZI

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 26, 2013 8:19 PM

All replies

  • Hi R.Zi,

    The problem is MemoryStream Cant be serialized directly since it is class type. Convert the MemoryStream to byte[] and then send to the service. I think there wont be any issues if you flow this approach.

    In your code instead of sending the memStream (MemoryStream) to the Service, send buffer (byte[]).

    ServiceClient client = new ServiceClient("BasicHttpBinding_ITwoWayAsync");
    FileStream originalstream = new FileStream(@"Directory+filename", FileMode.Create, FileAccess.ReadWrite,FileShare.None);
    Stream memStream = new MemoryStream();
    byte[] buffer = new Byte[1024];
    int bytesRead = 1024;
    while (bytesRead != 0)
    {
    bytesRead = originalstream.Read(buffer, 0, buffer.Length);
    memStream.Write(buffer, 0, bytesRead);
    }
    Console.WriteLine(memStream);
    object msg;
    msg = memStream;
    client.TransformEDI(buffer);
    string EDIout = (string)msg;
    Console.WriteLine(EDIout);
    Console.ReadLine();


    srithar

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 19, 2013 12:13 PM
  • Hi, srithar,

    Thank you for your reply. It works, but the answer is not what I want. Because this is orchestration published wcf web service, so I hope the xml files or EDI files I sent to the service can be disassembled by the xmlReceive or ediReceive pipeline. I tried to send the files as string, if I use passthroughreceive pipeline, it works. But it showed me "No Disassemble stage components can recognize the data", when I changed to xmlreceive, or edireceive pipelines, that means pipeline can not realize the string as message getting into pipeline, so I am wondering what way I can use to send local files to the service as we drop the files to receive location directly. I tested your way. I got the same error "No Disassemble stage components can recognize the data." So how can I achieve this goal?Any idears?

    Thank you again.

    R.Zi          


    • Edited by R.Zi Tuesday, March 19, 2013 2:03 PM
    Tuesday, March 19, 2013 2:01 PM
  • Try experimenting with the BizTalk Adapter settings and specifying an XPath into your message body.


    David Downing... If this answers your question, please Mark as the Answer. If this post is helpful, please vote as helpful.

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 26, 2013 1:37 PM
  • Hi David,

    Thanks for your tips and thanks for your always help. Finally I find the reasons for the errors and fix the issue. That's because the BizTalk published WCF service automatically creates the hidden <part></part> xml element note to wrap the string contents I send to it in the request message and the response message. So inside the message panel of WCF-BasicHttp Transport Properties as you point out, I need change the request Body Path and response Body Path as attached picture. For request, use BodyPath to get the string contents in the <part> element, for response, add the <part> note outside the message body. Then the problems are solved.

    Thanks for your help, and thanks for your nice. I appreciate.

    R.ZI

    • Marked as answer by R.Zi Tuesday, March 26, 2013 8:19 PM
    Tuesday, March 26, 2013 8:19 PM