WCF NetTCPBinding vs HttpBinding difference in data sent on wire - wcf

Say I have a service exposing two end points, 1st is a NetTCPBinding the second is any flavour of HttpBinding. They both implement exactly the same service contract.
What is the difference in what is sent on the wire?
Using netTcp is my message still serialised to XML ? Or some binary representation of my objects?
In terms of what receives the messages what is the difference? Will the http endpoint only understand http commands (get/post etc) where as the nettcp end point understands something different?
Why is nettcp more efficient (in this case I dont need interoperability) than http - where is the overhead?
I think that in all cases, before the message is put onto the wire it will be converted to binary so, also http sits on top of tcp in networking terms - so somewhere extra is needed for http communications.
Appreciate the question is a bit vague but hopefully someone will know what I am trying to ask :)

In WCF a particular binding does not necessarily imply a particular encoding. Various bindings can be configured to use various encodings. Net.TCP uses a binary encoding by default (MTOM I think), and HTTP uses a text/xml encoding by default.
With net.tcp your messages go sender -> net.tcp -> receiver. With HTTP they go from sender -> http -> tcp -> http -> receiver. There's an extra layer. The advantage of tcp is both of those: Both the extra layer and the default encoding.
HTTP with a binary encoding approaches net.tcp performance.
EDIT: Actually I think there may also be other optimizations in Net.TCP. It's a WCF-WCF communication scenario, so MS has control of both ends.

Related

Why WCF Serialization

I am struggling to understand answer for one question.
.net standard types are first converted to standard messages. This we call Serialization and will be done by one of the WCF serializer.
Those standard messages are converted to stream of bytes . Its called encoding and done by Encoders and which encoder will do that is decided by binding we choose.
My Question is why this serialization is kept in between, Why.net objects are converted to steams of bytes directly by WCF run-time engine and transferred across.
The only thing that connects a client and a server in WCF is the contract and the serialized XML format to exchange messages between the two parties.
WCF is an interoperable messaging service - it cannot rely on anything for the other party. WCF is NOT a ".NET object remoting" system or anything like that. WCF cannot rely on the other side of the communication being a .NET application, so therefore it cannot just exchange .NET objects.
WCF can talk to and serve data for anything - Ruby, Java - whatever. The smallest common denominator for this are serialized XML messages. Therefore, WCF will serialize your messages into a XML format (text or binary) and send them across the wire.

when to use duplex service?

Well, I know that in a duplex contract the service can send messages to the client, but I would like to know when that is really useful.
I have a common application that send request to the service to get data from the a database, insert data... etc. Also, I need to store files about 40MB in the database, so I need a good performance. For this reason, I would like to use the net.tcp binding with transfer mode streamed, but the problem is that a net.tcp duplex service can't use the streamed transfer mode.
So I think I have some options.
1.- study if I really need a duplex contract for this kind of application. Perhaps in a chat application, for example, it has more sense a duplex contract because the server perhaps need to notify to the client when a contact is connected... etc. But in a common client that access to a data base, is necessary a duple contract? what kind of operations would can need a duplex contract?
2.- Other option it's not to have a duplex contract, but implement a no duplex contract in the server and other single contract in the the client, so when a client connect to the service, the service receive the needed information to connect to the service of the client. But, is this a good way to avoid a duplex contract?
3.- Really for my application I need tcp instead of a duplex HTTP that allows a streamed transfer mode? What is the advantages of the tcp over the HTTP in terms of performance?
Thanks.
You need duplex if you want to implement callback pattern. Callback means that client does not know when some event happens in server.
If you do not know when event happens you have two options to implement:
Polling - send requests every X minutes to check if event happened. Server should either return event details (if it happened) or return flag saying that you need to continue calling. Server also can return recommended timeout in advanced scenarios.
Callback - client sends some form of description what server should do if event happened. This may be pointer to function in C, delegate in .NET or endpoint schema in WCF. Server remembers that info and makes call from their side when time came.
As you can see duplex/callback means that at some point server works as client (initiates communication) and this is a big game change.
WCF duplex communications may require special network configuration because in many cases network allows you to call external services (you work as client) but forbids external resources to call you (external service works as client). This is implemented for security purposes.
Returning to your questions:
You do not need duplex if you only need to download big amount of data. You may need it if you want to catch updates that happened in server and notify clients. Duplex should work for Chat because in chat there are many scenarios when client needs to be notified with changes introduced by others.
What you described is hand-made variant of duplex channel. You should use proved and tested duplex implementation made by MS If you want server to call your method. Otherwise your option is polling.
You are right, you need tcp + streamed transfer mode to deal with big amount of data. TCP uses binary serialization which is more compact comparing to text serialization + with TCP you do not need to send any HTTP headers or SOAP envelops. Disable security if you do not need it. It has a big performance impact.
Addressing each point:
1, 2. I think that for your scenario a duplex service is an overkill. As you say yourself a duplex service is usually handy when both the client and service need to keep notifying each other on a constant basis, what you're doing, getting lots of data in/out of a database doesn't seem to be a good case for using duplex communication. Regarding netTcpBinding not allowing Streaming with duplex, you can just return a byte array (byte[]) instead of a stream. 40 MB is a lot, but I don't think Streaming will necessarily have a significant performance gain over a duplex service which will return a byte array (up to you to test each setup and compare the results). So you have a few options here, don't stream and return a byte array (you can do this with your duplex service) or you can just forget about making your service duplex since there doesn't seem to be a strong case for you to make it duplex and just return a Stream:
[OperationContract]
Stream RetrieveFile(long _fileId);
[OperationContract]
long SaveFile(Stream _stream);
3. netTcpBinding has a considerable performance advantage over HTTP bindings, but it comes with a price, mostly because its TCP ports are sometimes blocked by internet firewalls, although you can use netTcpBinding over the internet, it's not recommended. Choosing a binding depends on what you're looking to do, if your clients are going to consume your service over the internet, then netTcpBinding is not a good idea (blocked TCP ports, firewalls etc.), but if your clients are consuming the service in the same network (LAN) then netTcpBinding is the most sensible choice. wsDualHttpBinding (doesn't support streaming :#) is a good choice if you want to stick to a duplex service (equivalent of PollingDuplexHttpBinding in Silverlight), or any other HTTP based bindings if you let go of the idea of a duplex service.
Some articles that may help you out, performance comparison of various WCF bindings:
http://blog.shutupandcode.net/?p=1085
http://tomasz.janczuk.org/2010/03/comparison-of-http-polling-duplex-and.html
And about Streaming large data with WCF over HTTP, according to the authors, both samples have been tested with up to 2GB of data:
http://garfoot.com/blog/2008/06/transferring-large-files-using-wcf/
http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
You shouldn't think that you must use netTcpBinding or you must use Streamed transfer for your service, netTcpBinding only becomes more performant than HTTP bindings after you enable throttling and configure some socket level properties. And streaming 40 MB will not have significant performance gains over buffered transfer. So you have a lot of options and a lot of trade-offs to make. There is no black and white and right or wrong, it's about how you customise your service to suit your needs best, most solutions will work. Your scenrio is a very common one and there are lots of stuff online about large data transfer in WCF, do more research ;)

WCF binding confusion re Message Format

Im getting confused with WCF, can someone clarify this for me please.
According to Michele Leroux's WCF book the following is true.
"Regardless of the message-encoding format, messages are represented on the wire either as SOAP 1.1 or SOAP 1.2" - I confirmed this by creating a simple net.tcp bound service and used the WCF test client to see the request and response XML.
According to http://www.codemeit.com/wcf/wcf-restful-pox-json-and-soap-coexist.html however, the following is true."webHttpBinding specifies that the service understands generic HTTP requests instead of SOAP requests. The REST service is built on top of generic HTTP request with GET HTTP verb."
So how can both these statements be true?
WCF now has a split personality. The vast majority of it talks SOAP (1.1 or 1.2) and messages end up structured as SOAP on the wire even if the encoder produces something other than XML
However, the WebHttpBinding is special. It uses the Json/POX encoder which strips all of the SOAP framing off the message just sending the message body, however that happens to be structured, down the wire. This means that it can be used to send any content type over HTTP

WCF duplex channel, de-coupling the request and the response

I'm contemplating a project where I'll be required to make use of what is variously called the "asynchronous" mode, or the "duplex" mode, or the "callback" mode of SOAP webservice. In this mode, the caller of the service provides in the SOAP header a "reply-to" address, and the service, instead of returning the output of the call in the HTTP response, creates a separate HTTP connection to this "reply-to" address and posts the response message to it. This is normally achieved in WCF using a CompositeDuplexBinding, like so:
<binding name="async_http_text">
<compositeDuplex clientBaseAddress="http://192.168.10.123:1234/async" />
<oneWay />
<textMessageEncoding messageVersion="Soap12WSAddressing10" />
<httpTransport useDefaultWebProxy="false" />
</binding>
This results in not one, but two HTTP connections per call: one from the client to the service, and then one from the service back to the client. From the point of view of the service implementation, nothing changes, you have a method that implements the interface method, and you take in the request and return the response. Fantastic, this is what I need, almost.
In my situation, the request and response can be separated by anything from minutes to days. I need a way to decouple the request and the response, and "store" the state (message, response URI, whatever) until I have enough information to respond at a later time (or even never, under certain circumstances).
I'm not terribly excited about having my methods essentially "paused" for up to days at a time, along with the required silly timeout values (if they're even accepted as valid), but I don't know how to go about putting a system like this together.
In order to be completely clear, I'm implementing a set of standards provided by a standards body, so I do not have flexibility to change SOAP message semantics or alter protocol implementations. This sort of interaction is exactly what was intended when the ReplyTo header was implemented in WS-Addressing.
How would you do it? Perhaps Workflow Foundation enables this sort of thing?
In such case don't use HTTP duplex communication as defined in WCF. It will simply not work because it is dependent on some other prerequisities - session, service instance living on the server, etc. It all adds a lot of problems with timeouts.
What you need is bi-directional communication based on fact that service exposes one way service and client exposes one way service as well (services can be two-way to support some kind of delivery notification). You will pass client's address in the first request as well as some correlation Id to differ multiple requests passed from the same client. You will call client service when the request is completed. Yes, you will have to manage all the stuff by yourselves.
If you are in intranet environment and your clients will be Windows based you can even think about changing your transport protocol to MSMQ because it has built-in support for all these requirements.
Edit:
I checked your updated question and you would call your communication pattern as Soap Messaging. I have never did it with WCF but it should be possible. You need to expose service on both sides of the communication - you can build your service to exactly follow needed contracts. When your service receives call you can use OperationContext.Current.IncommingMessageHeaders to access WS-Addressing information. You can store this information and use them later if you need them. The problem is that these information will not contain what you need. You have to fill them first on the client. This is generally possible by using OperationContextScope and filling OperationContext.Current.OutgoingMessageHeaders. What I'm affraid is that WCF can be "to clever" and override your changes to outgoing WS-Addressing information. I will probably try it myself during weekend.
It turns out the Windows Workflow Foundation (v4) does indeed facilitate this sort of message exchange.
Because WF allows you to decouple the request and response, and do basically whatever you want in the middle, including persist the workflow, idle it, and go outside and cut the grass, you get this capability "for free". Information can be found at these URLs:
Durable Duplex (MSDN)
Workflow 4 Services and duplex communications

Is there a way to intercept the raw data that's being sent over a WCF endpoint?

Is there a way to intercept the raw data that's being sent over a TCP WCF endpoint? I have implemented IClientMessageInspector but I am not sure if that's what's actually being sent over the wire.
My goal is to measure the performance of different serializers. I know there is some information out there but I would like to take a closer look at how they behave in my app.
Enable Message Logging in your configuration. To see the raw messag you want to log at Transport level.
You probably want to look into the built-in tracing capabilities of WCF. I don't have a link handy, but search for WCF tracing.