Welcome Guest! To enable all features please Login or Register.

Notification

Icon
Error

How to enable WCF conversation sessions in Silverlight
xomega
#1 Posted : Saturday, October 6, 2012 6:04:50 PM(UTC)
xomega



WCF conversation sessions are not supported in Silverlight out of the box due to the fact that Silverlight supports only a very limited set of WCF bindings (mainly the BasicHttpBinding), which do not support sessions. Xomega Framework provides a work around for these Silverlight limitations, which you can also read about in this article.

To enable support for WCF sessions on such sessionless bindings as BasicHttpBinding using Xomega Framework, you need to first configure the service endpoint behavior on the server side as follows.
  1. Add a new behavior extension with the type Xomega.Framework.Services.HttpSessionInstanceBehavior. Use the full type name including the assembly name and the version number without any line breaks. Give it any name, e.g. HttpSessionInstanceBehavior.
  2. Define an endpoint behavior and give it a name, e.g. BasicInstanceBehavior. Add an element with the name of the extension defined above inside of that behavior tag.
  3. Set the behavior configuration on your endpoints that use BasicHttpBinding to the name of the endpoint behavior defined in the previous step.

The following example demonstrates these steps.
Code:
<system.serviceModel>
  <behaviors>
    <endpointBehaviors>
      <behavior name="BasicInstanceBehavior">
        <HttpSessionInstanceBehavior/>
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name="AdventureWorks.Services.EmployeeService">
      <endpoint address="" binding="basicHttpBinding" 
                contract="AdventureWorks.Services.IEmployeeService"
                behaviorConfiguration="BasicInstanceBehavior"/>
    </service>
  </services>
  <extensions>
    <behaviorExtensions>
      <add name="HttpSessionInstanceBehavior"
       type="Xomega.Framework.Services.HttpSessionInstanceBehavior, Xomega.Framework, Version=1.3.0.0, Culture=neutral, PublicKeyToken=null"/>
    </behaviorExtensions>
  </extensions>
</system.serviceModel>


In order to create and use a WCF communication session in Silverlight you need to do the following steps.
  1. Create a new client for your service that is generated from your service reference.
  2. Register a new session for the client's inner channel with the Xomega.Framework.ClientSessionManager class, e.g. ClientSessionManager.Register(cltEmployee.InnerChannel);
  3. Make a sequence of service calls on the client, which will be routed to the same service instance on the server side. Since Silverlight allows only asynchronous calls, each subsequent call has to be made inside of the callback delegate for the previous call.
  4. After the last call, you need to call the EndSessionAsync() method on your client to end the session and free up the service instance, and then close the client. As described above, the asynchronous nature of the Silverlight WCF communication would require these methods to be called inside an asynchronous callback of the last service call.

The following example demonstrates how Create, Update and Save Changes operations are called in sequence on the employee service within the same session to create or update an employer record.
Code:
private void btnSave_Click(object sender, RoutedEventArgs e)
{
    EmployeeServiceClient cltEmployee = new EmployeeServiceClient();

    // Register the session with the ClientSessionManager before any calls.
    ClientSessionManager.Register(cltEmployee.InnerChannel);

    cltEmployee.SaveChangesCompleted += delegate(object s, SaveChangesCompletedEventArgs args)
    {
        FaultException<ErrorList> fex = args.Error as FaultException<ErrorList>;
        if (fex != null && fex.Detail != null)
            MessageBox.Show(fex.Detail.ErrorsText, "Service Errors", MessageBoxButton.OK);
        else
        {
            // end the session initiated by the ClientSessionManager.Register
            cltEmployee.EndSessionAsync();
            cltEmployee.CloseAsync();
        }
    };

    cltEmployee.UpdateCompleted += delegate(object s, AsyncCompletedEventArgs args)
    {
        cltEmployee.SaveChangesAsync(true);
    };

    if (!isNew)
    {
        Employee_UpdateInput inUpdate = new Employee_UpdateInput();
        obj.ToDataContract(inUpdate);
        cltEmployee.UpdateAsync(inUpdate);
    }

    // for new objects create the object and store a temporary key
    cltEmployee.CreateCompleted += delegate(object s, CreateCompletedEventArgs args)
    {
        obj.FromDataContract(args.Result);
        Employee_UpdateInput inUpdate = new Employee_UpdateInput();
        obj.ToDataContract(inUpdate);
        cltEmployee.UpdateAsync(inUpdate);
    };
    if (isNew) cltEmployee.CreateAsync();
}
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.

Copyright © 2009-2022 Xomega.Net. All rights reserved.