views:

250

answers:

1

Hi,

I have a problem with what solution to choose.. I have a server running having a Service running that can receive orders from a website. To this server several client (remote computers) are connected somehow.

I would really like to use WCF for all comunication, but not sure it's possible.

I dont wanna configure all client firewall settings in their routers, so the clients would have to connect to the server.

But when an order is recieved on the server, it should be transferred to a specific client.

One solution could be to have the Client connect using a duplex binding, but it will have to somehow keep the connection alive in order to be able to received data from server... Is this a good way to do this ??

Normally the connection times out and probably for a good reason...

Anyone have insight to this problem.

Thx alot for any advise :-)

Best Regards Søren Müller

+3  A: 

This is exactly what duplex bindings were designed for. The two best choices you have are NetTcpBinding or PollingDuplexBinding.

The former uses a TCP protocol which may not be suitable for your clients if they aren't on your network. However, it does allow two-way communication over a client-initiated socket. So the client doesn't need to be able to accept incoming connections. I recently used this on a project and it works very well. It's also very responsive. When client applications close, the session on the server immediately ends.

The second option, PollingDuplexBinding is included in the Silverlight SDK. It uses a client-initiated "long" HTTP request. The request waits for messages that need to go out to the client and when they arrive, the client request returns. The client then initiates a new HTTP request back to the server. In other words, the client always has a pending HTTP request. This works well over firewalls and should be used when you're dealing with internet clients. However, I've found this to be not as responsive as NetTcpBinding. I may have been doing something wrong but it seemed like attempts to send callbacks to abandoned client sessions took a while to "time out".


Here's an example of the configuration file from my recent project that used NetTcpBinding for duplex communication. Note that other than some tweaks to service throttling I am pretty much using the defaults for this binding. But there's all kinds of things you can tweak such as receiveTimeout, inactivityTimeout, etc.

<configuration>
    <system.serviceModel>

        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

        <behaviors>
            <serviceBehaviors>
                <behavior name="">
                    <serviceMetadata httpGetEnabled="true" />
                    <serviceDebug includeExceptionDetailInFaults="true" />
                    <serviceThrottling maxConcurrentCalls="65535" 
                                       maxConcurrentSessions="65535" 
                                       maxConcurrentInstances="65535" />
                </behavior>
            </serviceBehaviors>
        </behaviors>

        <bindings>
            <netTcpBinding>
                <binding maxConnections="65535">
                    <security mode="None" />
                </binding>
            </netTcpBinding>
        </bindings>

        <services>
            <service name="BroadcastService">
                <endpoint address="" binding="netTcpBinding" contract="BroadcastService" />
            </service>
        </services>

    </system.serviceModel>
</configuration>

[ServiceContract( CallbackContract = typeof( IBroadcastCallback ) )]
[ServiceBehavior( ConcurrencyMode = ConcurrencyMode.Multiple )]
public class BroadcastService : IDisposable
{

    [OperationContract(IsInitiating=true)]
    public long Subscribe( Guid clientID )
    {
        // clients call this to initiate the session
    }

    [OperationContract(IsOneWay = true)]
    public void Publish( BroadcastMessage message )
    {
        // client calls this to broadcast a message to
        // all other subscribed clients via callback
    }

}

[ServiceContract( Name = "BroadcastCallback" )]
public interface IBroadcastCallback
{

    [OperationContract( IsOneWay = true, AsyncPattern = true )]
    IAsyncResult BeginBroadcast(BroadcastMessage Message, AsyncCallback callback, object state);

    void EndBroadcast( IAsyncResult asyncResult );

}   // interface
Josh Einstein
Ahh sounds nice...It is over internet and as it seems I think TCPBinding sounds best.But in order to have the server send data to client Async, the connection needs to be kept alive by the client..Are there any setting on the binding to do the trick or do you need to send a heartbeat "message" to server in order to keep it alive..?Do do have any App.config example on how to set up a good TCPBinding for this??Thx..:-)
MüllerDK
There are various timeout settings on the NetTcpBinding that control how long it will remain open without any activity, but other than that there's nothing special you need to do to keep it alive. I used mostly defaults. I'll update my answer to include the relevant config from my recent project.
Josh Einstein