Consuming Web API in C# Help Need Guru Here

Hello all,

I have a Web API that I am trying to do the following.   I need to pass to it a MemoryStream and then some string params as follows:

In my controller I have:

  public HttpResponseMessage Post(SharepointParameter spParameter)
            var response = SharepointService.UploadSharepointDocument(spParameter.File, spParameter.SitePath, spParameter.FileName);

            return new HttpResponseMessage(HttpStatusCode.Created);

I then have this parameter class defined:

public class SharepointParameter
        public SharepointParameter() { }
        public SharepointParameter(MemoryStream File, string SitePath, string FileName)
            this.File = File;
            this.SitePath = SitePath;
            this.FileName = FileName;

        public MemoryStream File { get; set; }
        public string SitePath { get; set; }
        public string FileName { get; set; }

Something similar I did but without a stream just string and ints I then used Fiddler and the POST under composer I used the URL Route such as:


User-Agent: Fiddler
Content-Type: application/json
Host: localhost:62011
Content-Length: 83

and in Request Body passed the parameter this is from another app I have:

Now I need to consume the sharepoint Web API I am building from an MVC application.   So far I have this in my actionresult in the MVC app:
  PdfGenerate pdf = new PdfGenerate();
            MemoryStream ms = new MemoryStream();
            ms = pdf.CreatePdf(id);

            HttpClient client = new HttpClient();
            client.BaseAddress = new Uri(WebConfigurationManager.AppSettings["SharepointWebAPIPath"]);
            client.DefaultRequestHeaders.Accept.Add(new System.Net.Http.Headers.MediaTypeWithQualityHeaderValue("application/json"));

So I have my Memory stream so now I am not sure how I pass the MemoryStream and two strings SitePath and FileName along with it.   Can the stream also serialize in JSON etc?  The file will be a PDF file.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bob LearnedCommented:
If you try to serialize a MemoryStream, you get this exception:

Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.

This is because the MemoryStream class doesn't support that property, and throws an exception when serialized.  

I found this unit test code that highlights this problem:

    [ExpectedException(typeof(JsonSerializationException), ExpectedMessage = @"Error getting value from 'ReadTimeout' on 'System.IO.MemoryStream'.")]
    public void SerializePropertyGetError()
      JsonConvert.SerializeObject(new MemoryStream());

Open in new window

I haven't found a workaround yet.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aaron JabamaniTechnical ArchitectCommented:
Can't you get the bytes from memory stream and pass it to your web api ? Check in this link where they are posting a memory stream in web request, Hope it helps.
sbornstein2Author Commented:
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.