This project has moved. For the latest updates, please go here.

PhotoAtomic Logo color

SyncWcf overview

SyncWcf allows you to consume async WCF service easilly. it allows you to share the service interface in a portable library and use the synchronous style interface to perform async operations. It doesn't requires any magical code generation.

SyncWcf is a library that allows Silverlight to call WCF service with ease.
Usually you have two option:

  1. rely on the generated code in visual studio for service client ("add service reference and so on...")
  2. handwrite an Async version of the Sync interface you have used at the server side.


both of this have downsides:

  • generated code is usually a mess to maintain, and you (or your development team) have to remember to update the service reference each time the service interface change
  • on the other side if you handwrite the async version of the service interface you could potentially spend lot of time and again you have to remember to update it, plus you have to manage the invokation of the async pattern


there are scenarios where these points are a big problems, for examples in teams where there are developers with few experience with .NET, or where the development time is very critical or, again, your client side application must be very tight coupled to the server side, and you have to be absolutely sure that they share the exact same interface (even with additional attributes you have applied on it!)

this where SyncWcf came into place
by using the new Portable assebly you could:

  • share exactly the same interface and data types between server and client.
  • invoke the service using the synchrounous interface (no more Begin<Operation> End<Operation> but just <Operation>), and the asynchronicity is maintained!
  • by using lambdas your code could look like as it is synchrnous but in fact it executes asyncrhonously.

Code samples

Below how the code changes if you use SyncWcf

assume we have the following service interface on the server side

    [ServiceContract]
    public interface ITestService
    {
        [OperationContract]
        int Operation(int value);
    }
  • Client side service interface

without SyncWcf (you have to write it by hand)

    [ServiceContract]
    public interface ITestService
    {
        [OperationContract(AsyncPattern = true)]
        IAsyncResult BeginOperation(int value, AsyncCallback callback, object state);
        
        int EndOperation(IAsyncResult result);
    }


with SyncWcf (nothing to write and maintain)

     //simply import the portable assembly that contains the service interface definition
  • Client side service call

without SyncWcf (using the Async version of the Interface you have written by hand)

    ITestService testService = new ChannelFactory<ITestService>("*").CreateChannel();
    // [...]
    AsyncCallback asyncCallBack = delegate(IAsyncResult result)
    {
        int value = ((int)result.AsyncState).EndOperation(result);
        this.Dispatcher.BeginInvoke(delegate
        {
            // [...] Do something with the result
        });
    };
    testService.BeginOperation(1, asyncCallBack, testService);


with SyncWcf (using the Sync version of the Interface in the portable library)

    var channel = new AsyncChannelFactory<ITestService>().CreateChannel();

    channel.ExecuteAsync(
        ws => ws.Operation(1),
        result => 
        {
             // [...] Do something with the result
        });
  • Client side service configurationfile (ServiceReferences.ClientConfig)

without SyncWcf

    <client>
        <endpoint 
            address="../TestService.svc" 
            binding="customBinding"
            bindingConfiguration="CustomBinding_ITestService" 
            contract="PhotoAtomic.Communication.Wcf.Silverlight.Interface.Test.ITestService"
            name="CustomBinding_ITestService" 
        />
    </client>


with SyncWcf (just ad ".Async" at the namespace that identifies where the service interface is)

    <client>
        <endpoint 
            address="../TestService.svc" 
            binding="customBinding"
            bindingConfiguration="CustomBinding_ITestService" 
            contract="PhotoAtomic.Communication.Wcf.Silverlight.Interface.Test.Async.ITestService"
            name="CustomBinding_ITestService" 
        />
    </client>


As you can see by using SyncWcf the code looks a way better and it is a lot simpler. moreover, by default the result callback is dispatched con the calling thread so there is no synch problem with the UI in Silverlight.

Last but not least: all the call are strongly typed, with a large use of type auto inferred: total intellisense support.

Current Version

Current version is used in a real project, it resembles the WCF’s ChannelFactory – Channel pattern.
Version Version 2.0.1.0 could be considered the first production grade release. Anyway please let me know any feedback you have, areas that requires improvements and ideas, I’ll be glad to know your point of view to make the library evolve in the right direction

Version 3.0.0.0 is a Silverlight 5 revamp of the project in Visual Studio 2015. Five year later, it adds support for native Task and for generic services. The former is extensively tested, while the latter is derived from a community proposed patch, but it hasn't tested it carefully.

License

The project is free for use

About the author

Mosè Bottacini

Mosè Bottacini

PhotoAtomic Research Lab.
Technical Leader

Facebook: http://www.facebook.com/mose.bottacini

Twitter: @PhotoAtomicLab

Last edited Jun 21, 2016 at 2:55 PM by Duke360, version 29