MemoryStream is empty
I have a service witch transfers file using WCF. I have used it with .NET 4.5 and everything is OK. I want to migrate it to .NET Core and I have used this library to generate the client. Everything is working perfectly except the file transfer. The stream is always empty.
@stamo thanks for reporting the issue. Would you mind to share the code snippet that does not work for you?
@zhenlan thanks for your quick reply. Here is a sample of code, where the client is generated with dotnet/wcf (core version)
var client = new GetInvoiceClient(GetInvoiceClient.EndpointConfiguration.BasicHttpsBinding_IGetInvoice); var request = new GetInvoicePDFRequest("164235", new DateTime(2017, 1, 25), 1, false, 0, 217); var result = client.GetInvoicePDFAsync(request).Result; byte[] ms = result.GetInvoicePDFResult.ToArray();
And here is the same sample in .Net 4.5
var client = new GetInvoiceClient("BasicHttpsBinding_IGetInvoice"); var result = client.GetInvoicePDFAsync("164235", new DateTime(2017, 1, 25), 1, false, 0, 217).Result; byte[] ms = result.ToArray();
You can see the result on these screen shots

@stamo can you please let us know, in your dotnet core code, what's the type of this line?
var result = client.GetInvoicePDFAsync(request).Result;
As well, what's the type of GetInvoicePDFResult in the line below?
byte[] ms = result.GetInvoicePDFResult.ToArray();
@zhenlan sorry for the unclear code.
Here is the GetInvoicePDFAsync method signature:
System.Threading.Tasks.Task<InvoiceServiceReference.GetInvoicePDFResponse> GetInvoicePDFAsync(InvoiceServiceReference.GetInvoicePDFRequest request);
so it is
GetInvoicePDFResponse result = client.GetInvoicePDFAsync(request).Result;
and here is the GetInvoicePdfResult property definition:
public System.IO.MemoryStream GetInvoicePDFResult;
so result.GetInvoicePDFResult is System.IO.MemoryStream
Here is the generated Reference.cs file Reference.zip
@mconnew could this be a failure of MemoryStream.ToArray() on dotnet core?
@stamo did you try to read from the memory stream instead of calling ToArray?
@zhenlan Yes, ofcourse. This was the first thing I've tried, but it never worked. Even in the old library, when I try to read from the stream, an exception is thrown. Something like "Can't read from closed stream.". And in the new library the stream is empty. It has 0 length and when I try to read it, I get 0 bytes.
I will try to get code sample from he service, but it is not mine service, so I am not sure that I will be able to.
@zhenlan Hi, again. I've asked my coleagues to change the return type of the service to byte[] (from MemoryStream) and everithing worked. So this is a good workaround.
Thanks @stamo for the update and sharing the workaround!
@stamo, I believe changing the return type to byte[] will significantly increase the size of the data sent over the wire. An array of anything is treated like a collection of that item, so I would expect a byte[] would be serialized something like this:
<ArrayOfByte>
<byte>25</byte>
<byte>17</byte>
<byte>65</byte>
</ArrayOfByte>
Which as I'm sure you can imagine will make everything a lot bigger. When the service returns a Stream, WCF serializes this as base64 encoded binary which is more compact than an array of bytes.
The issue is the defined contract on the client is incorrect on .Net Core. On the full framework, it looks like the return type for the GetInvoicePDFAsync operation is a Stream, and as you are using buffered transfer mode, WCF will use a MemoryStream. WCF has some extra logic when the return type is a Stream which makes this all work. On .Net Core, the return type is a composite object which contains a property called GetInvoicePDFResult that is a Stream. The special Stream handling logic in WCF only applies when there return type is directly a Stream and doesn't work for a composite object. You probably want to change the service back to returning a Stream and modify the generated client to have the same contract definition as you have on the full framework. You should be able to copy the code from the contract definition on the full framework to the client. This looks like a bug in the WCF Connected Services code generation. If you have difficulty applying the full framework contract to be used in .Net Core, you could share 2 gist's with the two respective generated clients and I should be able to help you get it working from there. If you would be willing to share the WSDL as well, that would really help us track down the issue.
I'm assigning this to @hongdai to investigate the Connected Services bug.
@hongdai has reproduced the issue. Further investigation is needed to understand why is this behavior.
@hongdai has root caused the issue and she will look into a fix as the next step.
I have compared the behavior with the full framework and found it's consistent with the full framework:
- When Always generate message contracts is Checked, both full framework and .net core WILL wrap the MemoryStream in the composite object.
- When Always generate message contracts is NOT Checked(default), both full framework and .net core will directly use MemoryStream.
Apparently the customer should keep Always generate message contracts unchecked when generating the proxy in .Net Core.
I still cannot get this to work. Always generate message contracts is NOT Checked and only full framework is returning the right result, .net core still returns an empty stream. Any ideas on what I might be doing wrong? @hongdai @mconnew :) anyone :D