Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WCF MessageHeader

Status
Not open for further replies.

waynegs

Programmer
Feb 14, 2006
17
US
I am trying to send a file via a WCF service. I have a MessageContract that should allow me to enter the filename into the message header but it is not.

Here is the contract:
[MessageContract()]
public class FileTransferRequest
{
[MessageHeader(MustUnderstand=true)]
public System.String fName;

[MessageBodyMember(Order = 1)]
public System.IO.Stream Data;

}

Here is the implementation:
[OperationContract(IsOneWay = true)]
void UploadStream(FileTransferRequest request);

The function is:
public void UploadStream(FileTransferRequest request)
{
...
}


The reference in the proxy class on the client should be:

void UploadStream(String fName, Stream Data)

But shows up as:

void UploadStream(byte[] Data)

Any ideas? I am very new to web services and wcf.
 
One of the principles of service orientation is that you share schema, not class. What this means is that you trasmit everything in data structures that don't carry any sort of behavior with them. For instace, an List<string> tracks a collection of strings, but also has behaviors that let you add, remove, sort, etc. If you strip away all the behavior what you have left is basically an array of strings, which is what a List<string> gets converted to in service form. In this way, you get the schema of the List<string> but not the class.

The reason for this is interoperability. With service orientation, the whole point is loosely coupled system that don't rely on any specific underlying technology (.NET) so you can trasmit data in a form that clients will all understand, be they Java, Coldfusion, .NET, whatever.

Anyway, getting to the point, what is probably happening is that this concept of "Stream" is gettings its class stripped away in favor of a serializable representation of data. This makes sense, because WCF is a messaging framework where a request or response is sent in one big chunk.

"Streams" can't be submitted in a big chunk because the Stream class implies an open connection where you can seek through, read, and write N-bytes at a time, where only N-bytes are loaded into memory and the write capabilities save directly to, for example, a file which exists on the service host.

Therefore, to get to the answer, the best thing to do is not to try to figure out how to trasmit a "Stream" over a WCF message, but to change the way you think about services and program to their nature.

If you still want a Stream on the service's client, then you can always read the byte array into a MemoryStream.

MCP, MCTS - .NET Framework 2.0 Web Applications
 
Thanks for the explanation. I have already figured out that I can turn my filestream into a byte array and upload via the service. The thing I can't do is set a value to the fName MessageHeader so the service knows what to save the upload as.
 
Oh, my appologies.

Yes, you're defining the file name as a header (MessageHeader) which is why it doesn't show up as a parameter (you'd need to add the header separately).

Remove the MessageHeaderAttribute.





MCP, MCTS - .NET Framework 2.0 Web Applications
 
Thanks for the advice.

I figured out the problem, user error. :( I had setup the service link as a web reference and not a service reference.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top