Tuesday, March 27, 2012

Return empty IEnumerable<T> in C sharp

In order to return empty IEnumerable  in csharp you can use static Empty<T>() method in Enumerable class.

Suppose i have custom class called Foo  then,

Enumerable.Empty<Foo>();

Thursday, March 22, 2012

The workflow could not update the item, possibly because one or more columns for the item require a different type of information

I have SPD work flow which associated with a list that derived from content type. Using my .net application which was written using SharePoint Client Object Model i update .list item.But in some instances my workflow corrupted  by mentioning  above error.As well as my workflow has configured to initiate when list item creation only.

Finally i figure out reason for this strange behavior

There is one instance in my application in which i create list item and then updates same item to particular state.Due to these two step happen in instantly my workflow getting failed.

Solution

As solution what i have done is to make sure properly workflow initiated or not by querying the workflow instance id.Once i confirmed it then i execute rest of the code.other wise my operation is sleep till workflow initiated.From the beginning of my work flow i saved workflow instance id as list item.

As follows you can get the workflow instance id,

public string GetWorkflowURL(string styleNo, int versionId)
{
/*Getting the update list item */
ListItem listItem = GetUpdateItem(styleNo, versionId);
ListColumns listColumns = new ListColumns();

return listItem != null
? (listItem[listColumns.WorkFlow] != null
? ((FieldUrlValue) (listItem[listColumns.WorkFlow])).Url
: string.Empty)
: string.Empty;
}

This work around has worked to me.

Monday, March 19, 2012

What is Func<T> and Action<T> ?

These two are inbuilt delegate types which can use without defining a new delegate type with each and every parameter type and return type.

Func(Of TResult)

Which can hold method references  which have return types with input parameters or not.As well as there are bunch of overloads
example-:Here I need to add two integers and return the result.. 
Here my Adding method ,which expects the Func with two inputs and one out put.
public static void PerformAction(Func<int,int,int> method,int x,int y)
{
   int result=method.Invoke(x, y);
}
Here my is my controller class
static void Main(string[] args)
{
   Func<int, int, int> addingFunc = (x, y) => (x + y);
   PerformAction(addingFunc , 5, 10);
}

Action(Of T)


Which can hold method references which have no return types.There are bunch of overloads for the Action delegate,
here i use same example without retuning the result,instead of that i write it in to a just Console.
public static void PerformAction(Action<int,int> method,int x,int y)
{
   method.Invoke(x, y);
}
Here my is my controller class
static void Main(string[] args)
{
   Action<int, int> addingAction = (x, y) => Console.WriteLine(x + y);
   PerformAction(addingAction, 5, 10);
}      

Friday, March 16, 2012

Method Overloading error in WCF

In normal behavior of  WCF if you try to implement method overloading in WCF service contract it will throw the an InvaildOperationException once you invoke the service.

[OperationContract]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(EBasicDataType genericType, string descriptionPart);

[OperationContract]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(string styleNo, int versionId, EBasicDataType genericType, string descriptionPart);
In generally above statement will throw the InvaildOperationException.To overcome this problem you have to put OperationContractAttribute with Name Property.
[OperationContract(Name = "GetGenericData")]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(EBasicDataType genericType, string descriptionPart);

[OperationContract(Name = "GetAllgenericData")]
[FaultContract(typeof (ServiceException))]
BasicDataDTO[] RetrieveGenericData(string styleNo, int versionId, EBasicDataType genericType, string descriptionPart);
In Object Oriented aspect above code is correct ,WCF always generate proxy using the WSDL.The problem is WSDL does not support the concepts like inheritance and overloading.