Click here to Skip to main content
15,881,898 members
Articles / Programming Languages / C#
Tip/Trick

Generate Async ServiceContract automatically for your Silverlight projects

Rate me:
Please Sign up or sign in to vote.
1.00/5 (1 vote)
20 Nov 2011CPOL 22K   1   2
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).

C#
[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);		
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

Comments and Discussions

 
GeneralReason for my vote of 1 Contracts shouldn't change so freque... Pin
ArchAngel12312-Dec-11 10:41
ArchAngel12312-Dec-11 10:41 
GeneralRe: Yes, it should not change a lot when you are in a SOA design... Pin
Nicolas Dorier12-Dec-11 21:56
professionalNicolas Dorier12-Dec-11 21:56 
Yes, it should not change a lot when you are in a SOA design. That is, service first.
In RIA application, the definition of service is user interface driven so the service contract change as fast as the requirements change.
One way is to use a big datacontract (what I did before) so you don't have to change your service contract, but that's not really easy to use and write.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.