简体   繁体   中英

WCF - calling back to client (duplex ?)

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

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

I have done bit of research to select best approach for duplex communication. i came up with following solutions.

WCF service with wsDualhttpbinding :
This approach is good if the server and client both has public IPs. Client and server use http to communicate so no firewall restriction. But when initiating the duplex communication client has to make separate connection to server so that client also need to have public IP.

WCF service with NetTcpbinding :
This approach uses the TCP protocol for duplex communication and this is way to go if you are using WCF. Only disadvantage is some firewall blocks TCP port. In this case you need to have firewall exception.

WCF NetHttpBinding with web socket :
this approach use http protocol so no firewall restriction. In this approach client initiate http connection and later upgrade to TCP connection for duplex communication. At the moment only win 8 or win 2012 operating system support WCF web socket. All server and clients are wind 8 or win 2013 this is the way to go.

Web socket using third party framework (signal R , Xsocket.net) :
This is not a WCF but still you can implement rest web service or windows service application. Use http protocol and later upgrade to TCP for duplex communication

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM