Call synchronous WCF convention methods asynchronously on Silverlight

We consume wcf services in a silverlight application by creating proxies using ChanellFacotry.

The Operation and Data contracts fall under the Silverlight Crough assembly, which consists of shared files from the server document Data and Operation libriary. (omg I hope you understand what I'm saying).

Thus, the server and client use the same operations and data contracts.

Silverlight wcf client lib has a limitation on the impossibility of invoking wcf methods at the same time, as you know, therefore, the collaboration contract file must disclose asyn versions of each operation.

Writing an async WCF service will make sense if they do not contain locking operations, but since we use EF, asynchrony is achieved by delegating the locking operation to the thread pool. This is what WCF does for synchronization anyway. And this fact makes me want to tear my eyes (# @%! ^%! @%).

Our project consultant has the right not to allow the creation of dynamic proxies on the client for calling synchronous contract management methods (if you are interested in Google Evvan Bobrov Servelat Pieces). Thus, we must write senseles functions for asynchronous methods on the server, without any performance increase (blocking calls, as you recall).

Is it possible to call the synchronous wcf web service method from silverlight using a data contract?

- , , ?

, . , - t4, ?

, , async- :

    /// <summary>
    /// Subscribes to users of the specified organization.
    /// </summary>
    /// <param name="organizationId">The organization id.</param>
    public void Unsubscribe(int organizationId)
    {
        var clientId = this.OperationContext.GetClientId();
        if (string.IsNullOrEmpty(clientId))
        {
            return;
        }

        this.InternalUnsubscribe(organizationId, clientId);
    }

    /// <summary>
    /// Begins an asynchronous operation to Unsubscribe.
    /// </summary>
    /// <param name="organizationId">The organization id.</param>
    /// <param name="callback">The callback.</param>
    /// <param name="passThroughData">The pass through data.</param>
    /// <returns>
    /// An implementation of <see cref="IAsyncResult"/> that provides access to the state or result of the operation.
    /// </returns>
    public IAsyncResult BeginUnsubscribe(int organizationId, AsyncCallback callback, object passThroughData)
    {
        var clientId = this.OperationContext.GetClientId();
        if (string.IsNullOrEmpty(clientId))
        {
            return null;
        }

        var asyncResult = new VoidAsyncResult(callback, passThroughData);
        Task.Factory.StartNew(() =>
            {
                try
                {
                    this.InternalUnsubscribe(organizationId, clientId);
                    asyncResult.SetAsCompleted(false);
                }
                catch (Exception ex)
                {
                    asyncResult.SetAsCompleted(ex, false);
                }
            });
        return asyncResult;
    }

    /// <summary>
    /// Ends an existing asynchronous operation to Unsubscribe.
    /// </summary>
    /// <param name="result">The <see cref="IAsyncResult"/> provided by the BeginUnsubscribe operation.</param>
    public void EndUnsubscribe(IAsyncResult result)
    {
        var response = result as VoidAsyncResult;
        if (response != null)
        {
            response.EndInvoke();
        }
    }
+5
1

WCF Async. ServiceContract , Async.

[ServiceContract]
public interface IMyService
{
    [OperationContract]
    int Foo(string input);
}

//tell wcf that this contract applies to IMyService
[ServiceContract(Name = "IMyService")]
public interface IMyServiceAsync
{
    //setting AsyncPattern = true allows WCF to map the async methods to the sync ones.
    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginFoo(string input, AsyncCallback callback, object asyncState);

    int EndFoo(IAsyncResult result};
}

// you only need to implement the sync contract
public class MyService : IMyService
{
    public int Foo(string input)
    {
        return input.Length;
    }
}

IMyServiceAsync ChannelFactory, .

+3

All Articles