65.9K
CodeProject is changing. Read more.
Home

Generate Async ServiceContract automatically for your Silverlight projects

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (1 vote)

Nov 16, 2011

CPOL
viewsIcon

22525

Generate Async ServiceContract automatically for your Silverlight projects

Silverlight can only use Async ServiceContract to call a remote WCF service. It's tedious, error prone and boring to keep in sync the Async version with the synchronous one. You can easily generate it at build time with Genuilder. Create your class in a file (here IServiceClient.cs).
[ServiceContract]
public interface IServiceClient
{
	[OperationContract]
	void AddCustomerRange(Customer[] customers);
	[OperationContract]
	Customer[] List();
	[OperationContract]
	void RemoveCustomer(int id);
}
After installing genuilder as documented[^], create a new Genuilder project. In the Program.cs of the Genuilder project, install the AsyncServiceContractsExtension on your project:
static void Main(string[] args)
{
	foreach(var project in Projects.InSubDirectories("../../..").ExceptForThisAssembly())
	{
		var ex = new ExtensibilityFeature();
		ex.AddExtension(new AsyncServiceContractsExtension()
		{
			Files = new string[] { "IServiceClient.cs" }
		});
		project.InstallFeature(ex);
		project.Save();
	}
}
Run it. Reload your first project. Then every time you compile, IServiceClientAsync is generated automatically based on your ServiceContract IServiceClient.
[System.ServiceModel.ServiceContractAttribute(Name = "IServiceClient")]
public interface IServiceClientAsync
{		
	[System.ServiceModel.OperationContractAttribute(Name = "AddCustomerRange",AsyncPattern = true)]
	System.IAsyncResult BeginAddCustomerRange(Customer[] customers, System.AsyncCallback ac, object state);
	void EndAddCustomerRange(System.IAsyncResult ar);
		
	[System.ServiceModel.OperationContractAttribute(Name = "List",AsyncPattern = true)]
	System.IAsyncResult BeginList(System.AsyncCallback ac, object state);
	Customer[] EndList(System.IAsyncResult ar);
		
	[System.ServiceModel.OperationContractAttribute(Name = "RemoveCustomer",AsyncPattern = true)]
	System.IAsyncResult BeginRemoveCustomer(System.Int32 id, System.AsyncCallback ac, object state);
	void EndRemoveCustomer(System.IAsyncResult ar);		
}