none
File Upload fails on W10 Universal app RRS feed

  • Question

  • Hi,

    I have a piece of code that is working like a charm from a console application, but the same code fails from a universal app.

    Here is the code of the webservice - datacontracts:

    namespace TransmissionService.DataContracts
    {
        [MessageContract]
        [KnownType(typeof(UploadRequest))]
        public class UploadRequest {
            [MessageHeader(MustUnderstand = true)]
            public string FileName { get; set; }
    
            [MessageHeader(MustUnderstand = true)]
            public string DocType { get; set; }
    
            [MessageHeader(MustUnderstand = true)]
            public string Folder { get; set; }
    
            [MessageHeader(MustUnderstand = true)]
            public string SubFolder { get; set; }
    
            [MessageBodyMember(Order = 1)]
            public Stream Stream { get; set; }
        }
    
        [MessageContract]
        [KnownType(typeof(UploadResponse))]
        public class UploadResponse {
            [MessageBodyMember(Order = 1)]
            public bool UploadSucceeded { get; set; }
        }
    }

    The implementation is as follows :

    [OperationContract]
    UploadResponse Upload(UploadRequest request);
    
    public UploadResponse Upload(UploadRequest request) {
        .... // File transfer
    }
    

    This is the code to test the file transfer, after adding a service reference (from a simple console application) :

    FileStream stream = new FileStream(@"c:\temp\picfrom.png",    
        FileMode.Open);
    stream.Position = 0;
    var response = client.Upload("Project", "picto.png", 
        "Folder1", "Folder2", stream);
    

    Works like a charm

    However, I am unable to do the same thing from a universal app. After adding the service reference, the function call shows up like this :

    UploadAsync(byte[] stream)

    Same service, same implementation, results in a totally different parameter. I was expecting the function call from a universal app would look like this :

    await client.UploadAsync("Project", "picto.png", 
        "Folder1", "Folder2", stream);

    But no way, it's expecting a parameter byte[] stream.

    From a console application, it's working like a charm, so I guess I was doing it the right way. Or not? Is tcp protocol better to upload files? Files can be pictures or small movies.

    Many thanks in advance,

    S.

    Tuesday, September 13, 2016 2:58 PM

Answers

  • Hi SteveDeclerck,

    I made a test with your description, and I could reproduce your issue. It seems the generated library are different between UWP and Console application. Since all of the library are automatically generated by VS, and I did not find any settings to change it. I am afraid we could not change the generated library.

    To achieve uploading File from UWP to WCF, I would suggest you pass byte, and define the file name and path in service side.

    For another way, I would suggest you try to add headers in message from client side. At present, I achieve this by below code.

    //client
    Service1Client client = new Service1Client();
                using (new OperationContextScope(client.InnerChannel)){
                    MessageHeader outBoundHeader
                  = MessageHeader.CreateHeader(
                    "FileName",
                    "Test",
                    "Test"
                  );
                    OperationContext.Current.OutgoingMessageHeaders.Add(outBoundHeader);
                    await client.GetDataAsync(123);
    //service
                MessageHeaders m = OperationContext.Current.IncomingMessageHeaders;
                foreach (MessageHeaderInfo h in m)
                {
                    if (h.Name == "FileName")
                        Console.WriteLine(h.Namespace);
                }

    In above code, you may be confused that I get Namespace instead of value. I try to get header value in service side, but I failed, so I get Namespance for a workaround.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Proposed as answer by Tony---- Friday, September 16, 2016 6:55 AM
    • Marked as answer by SteveDeclerck Wednesday, September 21, 2016 9:11 PM
    Wednesday, September 14, 2016 8:32 AM

All replies

  • Hi SteveDeclerck,

    I made a test with your description, and I could reproduce your issue. It seems the generated library are different between UWP and Console application. Since all of the library are automatically generated by VS, and I did not find any settings to change it. I am afraid we could not change the generated library.

    To achieve uploading File from UWP to WCF, I would suggest you pass byte, and define the file name and path in service side.

    For another way, I would suggest you try to add headers in message from client side. At present, I achieve this by below code.

    //client
    Service1Client client = new Service1Client();
                using (new OperationContextScope(client.InnerChannel)){
                    MessageHeader outBoundHeader
                  = MessageHeader.CreateHeader(
                    "FileName",
                    "Test",
                    "Test"
                  );
                    OperationContext.Current.OutgoingMessageHeaders.Add(outBoundHeader);
                    await client.GetDataAsync(123);
    //service
                MessageHeaders m = OperationContext.Current.IncomingMessageHeaders;
                foreach (MessageHeaderInfo h in m)
                {
                    if (h.Name == "FileName")
                        Console.WriteLine(h.Namespace);
                }

    In above code, you may be confused that I get Namespace instead of value. I try to get header value in service side, but I failed, so I get Namespance for a workaround.

    Best Regards,

    Edward


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    • Proposed as answer by Tony---- Friday, September 16, 2016 6:55 AM
    • Marked as answer by SteveDeclerck Wednesday, September 21, 2016 9:11 PM
    Wednesday, September 14, 2016 8:32 AM
  • Hello Edward,

    Sorry for the late reply. There is indeed a difference between the generated code. Thank you for the sample code! Problem solved.

    Thanks and best regards, S.

    Wednesday, September 21, 2016 9:11 PM