4

Is it possible to calculate the size of a (complexed) object (with dataContract) that I send over WCF? What I need is to calculate the size on both the request and the response objects and I need to do this in a winform application.

Could I maybe serlize the objects and then get the total size?

mavnn
  • 9,101
  • 4
  • 34
  • 52
Banshee
  • 15,376
  • 38
  • 128
  • 219

3 Answers3

2

You can manually serialise / deserialise the objects yourself. Here's a simple example of serialisation and obtaining the length.

[DataContract(Name = "Person", Namespace = "http://www.woo.com")]
    class Person
    {
        [DataMember()]
        public string Name;
        [DataMember()]
        public int Age;        
    }

calling code (in a console app)

        Person p = new Person();
        p.Name = "Sean Cocteau";
        p.Age = 99;

        DataContractSerializer ds = new DataContractSerializer(p.GetType());

        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            ds.WriteObject(ms, p);                
            // Spit out

            Console.WriteLine("Output: " + System.Text.Encoding.UTF8.GetString(ms.ToArray()));
            Console.WriteLine("Message length: " + ms.Length.ToString());
        }

        Console.ReadKey();

In respect to performing this automatically, say on each WCF call may involve you having to create your own Custom Binding that adds this to the message.

SeanCocteau
  • 1,838
  • 19
  • 25
  • This will not get you the size of the message because WCF wraps an the DataContract in an envelope before it goes over the wire. In some cases, the envelope can be bigger than the message. – Davin Tryon Feb 29 '12 at 14:46
  • I fully agreed, the above only calculates the size of the 'object' as originally requested :) – SeanCocteau Feb 29 '12 at 14:52
  • Yes, I did thought of this but as you said, it will not be the correct size. – Banshee Mar 01 '12 at 14:41
0

I think a message interceptor will help, you can add this endpoint behavior to the endpoint and handle httpwebrequest/response.

For more information, google WCF Message Interceptor, you'll find lots of useful things.

Hope this will help.

Simon. Li
  • 404
  • 2
  • 11
  • I do knwo about the message interceptor but I havent found a way to calculate the real size of the package that goes over the wire. – Banshee Mar 01 '12 at 14:40
  • Thus……It seems you need to hack into WCF framework? As far as I know, WCF doesn't provide any way to monitor the real network traffic. How about create your own binding element? And If you use ssl, the data will be encrypted... So, I suggest you try to find the solution out of WCF...Windows Hook? I heart that WCF (and all .net socket) use IOCP underlayer, you can try to do some hack here. – Simon. Li Mar 02 '12 at 01:52
0

You can use svctraceviewer to trace both the activity and the message and then you can see the actual message flowing over WCF. However it seems that this tool might not get you the total size.

This post might help.

Community
  • 1
  • 1
Davin Tryon
  • 66,517
  • 15
  • 143
  • 132
  • Yes I know about that but alot of other information is logged there and its hard to follow the flow. – Banshee Mar 01 '12 at 14:41
  • I just blogged about the config [here](http://www.davintryon.com/). Should be easy to see what is happening with the message. – Davin Tryon Mar 01 '12 at 14:55