Karch
3/23/2007 6:31:00 PM
I am trying to marshal some binary data in my code before passing it to a
SQL Service Broker queue. I found this code for marshaling the binary data,
but I would like to also do this in my client c# code before sending it to
the queue. The problem is that when it gets to the queue, the unmarshaling
fails. This is the code that I need to emulate in my C# client:
BEGIN
DECLARE @marshaledPayload VARBINARY(MAX);
DECLARE @payloadLength BIGINT;
DECLARE @userLength INT;
SELECT @payloadLength = LEN(@payload), @userLength = LEN(@user)*2; --
wchar_t
SELECT @marshaledPayload = CAST(@dateTime AS VARBINARY(MAX)) +
CAST(@payloadLength AS VARBINARY(MAX)) + @payload +
CAST(@userLength AS VARBINARY(MAX)) + CAST(@user AS VARBINARY(MAX));
RETURN (@marshaledPayload);
END
And this is what I am trying to do in C# (which doesn't work):
byte[] dateTime =
BitConverter.GetBytes(Convert.ToInt64(DateTime.Now.Ticks));
byte[] bytesPayload = Encoding.Unicode.GetBytes(payload);
byte[] bytesPayloadLength =
BitConverter.GetBytes(Convert.ToInt64(bytesPayload.Length));
byte[] bytesUser = Encoding.Unicode.GetBytes("SA");
byte[] bytesUserLength = BitConverter.GetBytes(bytesUser.Length);
int totalPayloadLength = dateTime.Length + bytesPayloadLength.Length +
bytesPayload.Length +
bytesUserLength.Length + bytesUser.Length;
byte[] totalPayload = new byte[totalPayloadLength];
System.Buffer.BlockCopy(dateTime, 0, totalPayload, 0, dateTime.Length);
System.Buffer.BlockCopy(bytesPayloadLength, 0, totalPayload,
dateTime.Length, bytesPayloadLength.Length);
System.Buffer.BlockCopy(bytesPayload, 0, totalPayload, dateTime.Length +
bytesPayloadLength.Length, bytesPayload.Length);
System.Buffer.BlockCopy(bytesUserLength, 0, totalPayload, dateTime.Length +
bytesPayloadLength.Length + bytesPayload.Length, bytesUserLength.Length);
System.Buffer.BlockCopy(bytesUser, 0, totalPayload, dateTime.Length +
bytesPayloadLength.Length + bytesPayload.Length + bytesUserLength.Length,
bytesUser.Length);