Can not use memory stream from my web service

jwblair2009
jwblair2009 used Ask the Experts™
on
I have created a web service returning a memory stream. The client interfaces to the web service via a service reference. When I try to use the stream in my client from the web service, I get the following error:

Cannot convert type 'interchangeCalculations.iccServiceReference.MemoryStream' to 'System.IO.MemoryStream'

This makes no sense to me since a memory stream is a just a memory stream. This worked in my test app when I was creating and processing the stream in the same application. Now that I am connecting the client to a web service to get the stream, it fails. How do I get around this? The below cast for the memory stream also fails.

MemoryStream streamBinary = (MemoryStream)myIccWebService.getEftTranDataBinarySerialized(startTime, endTime);
sicInfoDataSetNotTyped = binaryDeSerialize(streamBinary);

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
A better/similar/Another description. This question was found on another website and was not answered.

I am using an XMLSerializer to create an xml object of a
class in a web application. I am then using an XMLWriter
to place this into a memory stream. No problems so far.

I have a web service that is to receive a
System.IO.MemoryStream object and then deserialize it to
retrieve the require information.

When I try to make the call to the WebMethod from my web
application the IDE is showing that it is not expecting a
System.IO.MemoryStream object, but is expecting an object
of type <WebService>.<Service>.MemoryStream i.e.
WebApplication.WebService.SystemMemoryStream.

If I just pass my memory stream object I get the compile
error: Cannot Convert from 'System.IO.MemoryStream'
to 'WebApplication.WebService.MemoryStream'.

Any ideas on why this isn't working? Or is there a better
way to do this???

Author

Commented:
The only thing that I see that even comes close to solving this is to convert the stream to a byte array. Got any other better ideas?
Technical Architect
Commented:
With web service you cannot serialize memory stream directly. It is not supported . You have to convert to bytes as you said using ToArray.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Author

Commented:
Is there any documentation on MSDN to support this?

Author

Commented:
Ok, now that I have created a byte array, I am now getting this error.

System.Runtime.Serialization.SerializationException: Unable to find assembly 'interchangeCalculationsWebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'.
   at System.Runtime.Serialization.Formatters.Binary.BinaryAssemblyInfo.GetAssembly()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.GetType(BinaryAssemblyInfo assemblyInfo, String name)
   at System.Runtime.Serialization.Formatters.Binary.ObjectMap..ctor(String objectName, String[] memberNames, BinaryTypeEnum[] binaryTypeEnumA, Object[] typeInformationA, Int32[] memberAssemIds, ObjectReader objectReader, Int32 objectId, BinaryAssemblyInfo assemblyInfo, SizedArray assemIdToAssemblyTable)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.ReadObjectWithMapTyped(BinaryObjectWithMapTyped record)
   at System.Runtime.Serialization.Formatters.Binary.__BinaryParser.Run()
   at System.Runtime.Serialization.Formatters.Binary.ObjectReader.Deserialize(HeaderHandler handler, __BinaryParser serParser, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream, HeaderHandler handler, Boolean fCheck, Boolean isCrossAppDomain, IMethodCallMessage methodCallMessage)
   at System.Runtime.Serialization.Formatters.Binary.BinaryFormatter.Deserialize(Stream serializationStream)
   at interchangeCalculations.interchangeCalculationsApplication.binaryDeSerialize(MemoryStream stream) in C:\Projects\ICC\version 1.2.1\source code\interchangeCalculations.root\interchangeCalculations\interchangeCalculations\interchangeCalculationsApplication.cs:line 1960
      
Aaron JabamaniTechnical Architect

Commented:
Sample of serialization and deserialization is given in vb please change it to C#.

But you error says that, it couldn't find assembly "interchangeCalculationsWebService".  Provide proper path or copy that to the required place.
Public Class Utils
    Public Shared Function Serialize(ByVal Obj As Object) As Byte()
        Dim bf As New BinaryFormatter
        Dim mem As New MemoryStream
        Try
            bf.Serialize(mem, Obj)
        Catch ex As Exception
            Throw ex
        End Try
        Return mem.ToArray
    End Function
 
    Public Shared Function Deserialize(ByVal bytes() As Byte) As Object
        Dim bf As New BinaryFormatter
        Dim mem As New MemoryStream(bytes)
        Dim obj As Object
        Try
            obj = bf.Deserialize(mem)
        Catch ex As Exception
            Throw ex
        End Try
        Return obj
    End Function
End Class

Open in new window

Author

Commented:
Thanks, but I solved the problem on my own. The problem was that I created a class to determine how to serialize a dataset. The class was in the web service but not in the client. Thus I created a DLL for the class and included it both in the web service and the client. Then the same assembly was found in both places, and now it works.

Thanks to everyone that participated.

Author

Commented:
Memory streams in a web service are not supported, but not alternative solution was provided.

Author

Commented:
apeter said to use a byte array, but did not provide to turn that dataset to a byte array.

Author

Commented:
apeter, you jogged my memory to point me to the correct solution, thanks for help and sorry to everyone else for the multiple postings.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial