3

I have a TCP based service I would like to expose through WCF. The service API is similar to the TcpListener class (actually is based on it), accepts TCP connections as clients and sends and receive raw Stream derived objects through the client class. I would like to create a WCF wrapper that allow to send and receive objects, by taking advantage of WCF serialization.

The basic contract only contains two operations, send and receive, and the formatting usually would be schemaless like JSON, but the client can specify a protocol on connection, so I would like to be ready to accept some of them.

It should work also with SSL. At the moment, when I have to work with SSL I have to wrap the TcpClient.GetStream() in a SslStream but the peformance is very poor. I would like to use my service without this hack, and use the WCF SSL by doing something like:

            <binding name="MyBinding">
               <security mode="Transport">
                 <transport clientCredentialType="Windows" />
                </security>
            </binding>   

These are my doubts so far:

  1. What do I need to create? A custom transport or custom binding?

  2. Is there anything special I should implement to allow WCF track/manage the lifetime of my service or client connections?

Cheers.

vtortola
  • 34,709
  • 29
  • 161
  • 263
  • 2
    Maybe you should split this question up.. – lockstock Mar 11 '14 at 06:36
  • Right, it was to wide. – vtortola Mar 11 '14 at 09:51
  • Have you looked at the NetTcpBinding provided? Technically that binding is only compatible with .NET clients and services, but maybe you could write a custom binding that uses it internally to support non-.NET clients. You can create a data contract class that has a Stream property, and modify the web configuration for the NetTcpBinding to enable streaming-request support. The NetTcpBinding also supports SSL like the snippet you have above. – ajawad987 Mar 17 '14 at 19:16
  • I will take a look at that, seems good idea. – vtortola Mar 18 '14 at 14:20

1 Answers1

1
  1. You will want to create a security setting on your bindings like in your example. This page is the MSDN article about security settings for WCF bindings. I believe that SSLStream wrapper should be the correct way to handle this - I am not sure why the performance is so bad. You can look at this SO question about slow SSLStream because of buffering incorrectly. Depending on how you are using the socket this answer talks about slow performance because of how you encode binary data. If you are sending many small messages this particular setup will be pretty slow. The second SO link suggests using something like google's proto-buf libraries.

  2. What do you mean by track/manage the lifetime? Do you mean timing out stale connections? I believe that is already handled by the TCPListener.

I know that I might not have answered your question but without more details it is hard to determine the exact reason for the slowness.

Community
  • 1
  • 1
Ben Echols
  • 479
  • 2
  • 8
  • The actual question is not the SSL slowness, but how to create a TCP binding or transport from scratch. – vtortola Mar 19 '14 at 17:06
  • Let me clear up what I mean - You can configure a WCF service to have transport security using the settings that you have pasted into the question. This will make the WCF service handle all the SSL stuff. You will need to use the linked docs on MSDN about WCF bindings and security settings to see exactly what you want. You can also use the SSLStream as you said you tried in your question. There can be many reasons for slowness in that case that I tried to explain in the answer. – Ben Echols Mar 19 '14 at 17:39