Creating a Service with Windows Communication Foundation and LINQ to SQL, Page 3
Finally, add an Imports statement for your service, create an instance of the service client object, and invoke the service. Listing 4 demonstrates.
Listing 4: Add a reference to the service, create the client wrapper object, and call the method—GetCustomer—defined by the service contract.
Imports CallService.CustomerServiceReference Module Module1 Sub Main( Dim service As CustomerServiceClient = _ New CustomerServiceClient Console.WriteLine(service.GetCustomer("ALFKI").CustomerName) Console.ReadLine() End Sub End Module
I didn't count the number of lines for the whole sample, but it's not very many. What is interetsing is that the example goes from a console client, through a service, to a SQL Server database with very modest effort. The upshot is you get to focus on the problem space and the enhancements to the .NET framework do a lot of the heavy lifting for you.
|Updates are supported by sending the Customer back to the service, attaching it to the DataContext, and calling SaveChanges. For roundtrip examples, stay tuned for future articles or pick up a copy of my book LINQ Unleashed for C#. The book is self-explanatory, so VB programmers should be able to decipher the C# code.|
In this article, you touched up generics, extension methods, interfaces, services, LINQ to SQL, SQL Server, and distributed programming but the technical effort on the programmer's part is relatively modest.
Ten years ago, to do the same thing might have taken a week of effort. In .NET 3.5, more of the plumbing is handled by the framework and that's a very good thing.
About the Author
Paul Kimmel is the VB Today columnist for www.codeguru.com and has written several books on object-oriented programming and .NET. Check out his upcoming book LINQ Unleashed for C#; preorder your copy today at Amazon.com. Paul Kimmel is an Application Architect for EDS. You may contact him for technology questions at firstname.lastname@example.org.
Copyright © 2008 by Paul T. Kimmel. All Rights Reserved.