Monday, December 20, 2010

WCF OperationContractAttribute with example

The OperationContractAttribute, also defined in the System.ServiceModel namespace, can be applied only to methods. It should declare to the method which belongs to a Service contract. Operation contract can be declared with below named parameters that provide control over the service description and message formats.

Name Specifies a different name for the operation instead of using the default, which is the method name.Action Controls the action header for messages to this operation.

ReplyAction Controls the action header for response messages from this operation.

IsOneWay Indicates whether the operation is one-way and has no reply. When operations are one-way, ReplyAction is not supported.

ProtectionLevel Enables the Service contract to specify constraints on how messages to
all operations in the contract are protected on the wire, that is, whether they are signed and encrypted.

IsInitiating Indicates whether invoking the operation initiates a new session between the caller and the service.

IsTerminating Indicates whether invoking the operation terminates an existing session between the caller and the service

Here is Sample WCF Class for reference.

// C#
[ServiceContract(Namespace = "")]
public class CrudContract
{
    [OperationContract(IsOneWay = true, Action = "urn:crud:insert")]
    void ProcessInsertMessage(Message message);
    [OperationContract(IsOneWay = true, Action = "urn:crud:update")]
    void ProcessUpdateMessage(Message message);
    [OperationContract(IsOneWay = true, Action = "urn:crud:delete")]
    void ProcessDeleteMessage(Message message);
    // The catch-all operation:
    [OperationContract(IsOneWay = true, Action = "*")]
    void ProcessUnrecognizedMessage(Message message);
}

Hope this is useful Party smile

Wednesday, December 15, 2010

“CleanRiaClientFilesTask" task failed unexpectedly

Recently few of our developers and me got this error while building my Silverlight application. I got this error while checking in the code and merge my code changes.

Error Details.

Error    38    The "CleanRiaClientFilesTask" task failed unexpectedly.
System.ArgumentException: Illegal characters in path.

To resolve this we need to do two simple steps.

Step 1: Go to this location in your system or your Framework install directory, for me its C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files. Clean all the temporary files under this location and leave the root folder empty.

Step 2: Go to your project location and Delete all the files in \obj folder. Basically all the files are stored in the \obj folder created by ria services.

Now clean the project and try building again. It worked for me. Hope this will help you. Good luck Hot smile

Thursday, December 09, 2010

Iterating through Generic List in C#

Recently I need to loop through a Generic list and bind the result to a Combobox. Here is how you can do this in C#

List<CCDSDateResult> list = new List<CCDSDateResult>();
for (int i = 0; i < e.Result.Count; i++)
{
    if (e.Result.ToList()[i].ccds_date.ToString().Length > 0)
    {
        // Write your logic..to add item in the list
        list.Add(new CCDSDateResult(DateTime.Parse(e.Result.ToList()[i].ccds_date.ToString())));
    }
}
// adding a empty record to the list
list.Insert(0, new CCDSDateResult());
this.OrderComboBox.ItemsSource = list.ToList();

Hope this is useful. Thumbs up

Exporting to Excel from Telerik Gridview in Silverlight

Here is the sample code for exporting Telerik Gridview in silverlight.

private void btnExportOrders_Click(object sender, RoutedEventArgs e)
{
   // Checking whether grid has rows
    if (OrderGrid.Items.Count > 0)
    {
          string extension = "xls";
          string selectedItem = "Excel";
          ExportFormat format = ExportFormat.ExcelML;
          SaveFileDialog dialog = new SaveFileDialog();
          dialog.DefaultExt = extension;
          dialog.Filter = String.Format("{1} files (*.{0})|*.{0}|All files (*.*)|*.*", extension, selectedItem);
          dialog.FilterIndex = 1;
          if (dialog.ShowDialog() == true)
          {
              using (Stream stream = dialog.OpenFile())
              {
                GridViewExportOptions options = new GridViewExportOptions();
                options.Format = format;
                options.ShowColumnHeaders = true;
                options.Encoding = Encoding.UTF8;
                options.ShowColumnHeaders = true;
               // name of the grid which you want export      
                OrderGrid.Export(stream, options);
               }
           }
     }
     else
      {
         // User friendly Message
          MessageBox.Show("There are no records to export.", "Export Warning!", MessageBoxButton.OK);
      }
 }

This export code will also support different formats like CSV,Text and HTML. All you need to change is the enum type for ExportFormat to above formats and the extension. Its easy and simple. Good luck Winking smile

Silverlight: MessageBox with Title, OK, and Cancel Buttons

A message box is a dialog box that is used to display an alert or a message. A message box can have a title and multiple options such as Yes/No or  OK/Cancel. A message box can also have some input control to take input from a user.

The MessageBox class in WPF represents a modal message box dialog, which is defined in the System.Windows namespace. The Show static method of the MessageBox is the only method that is used to display a message box. The Show method returns a MessageBoxResult enumeration that has values OK, Cancel, Yes, and No. We can use MessageBoxResult to find out what button was clicked on a MessageBox and take an appropriate action.

The following code snippet creates a MessageBox with a message, a title, and two OK and Cancel buttons.

 MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this Deposit?",
                                    "Delete", MessageBoxButton.OKCancel);
      if (result == MessageBoxResult.OK)
      {
         // do if your result is OK
      } 
      else
      {
         // do if result is cancel
      }

Hope this help Smile