I wanted to make two of 3.5 framework additions to work together. I decided to make a WCF Web Service and LINQ. I had a few pitfalls with the LINQ. So, I created it in SQL first, then I re-did it in using LINQ.
Creating a WCF web service is fairly simple. Open the newest and greatest version of Visual Studio to date; VS 2008. File, New, Web Site and highlight WCF Service. Choose how you want it to work, meaning file system or point to a web; I choose file system and uploaded to my web site. After you create the default WCF Service you should open IService.cs and the Service.cs; both located in the App_Code folder. Starting with the Interface you will notice two contracts already there;
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
At this point I added a new contract.
[OperationContract]
string AllEmployees();
That was pretty much it for the Interface.
Next, I created my data classes. This is the very heart of LINQ. I added a new item to my project, LINQ to SQL Classes. This added a blank page where I simply dragged the tables in I wanted to reference. Pretty simple!!!
Now it was time for the backend. I opened Service.cs to add a few name spaces;
using System.Configuration;
using System.Data.Linq;
The configuration name space allowed me to point to a database. The System.Data.Linq name space allowed me to use Table<>.
Next I made a public method named AllEmployees() and added the following code;
public string AllEmployees()
{
DataClassesDataContext db = new DataClassesDataContext(ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString"].ConnectionString);
Table<Employee> Emps = db.GetTable<Employee>();
var query =
from e in Emps
select e;
string result = "<Employees>";
foreach (var e in query)
{
result += "<Employee><EmployeeID>" + e.EmployeeID + "</EmployeeID><LoginName>" + e.LoginName + "</LoginName>" +
"<FullName>" + e.FullName + "</FullName><Department>" + e.Department + "</Department></Employee>";
}
result += "</Employees>";
return result;
}
When working in SQL most of us create our connection object first. Then we create our command object, then we do some binding and move on to either a adapter object or a reader object. In LINQ the connection object and the command object are summed up in DataContext object – which references the dbml file created. In my case the default name DataClasses (DataClassesDataContext.dbml). In any event this one object took the place of the SQL Connect and command objects. The next line creates a table enumeration object; basically a pointer to the table. Then instead of a standard SQL statement LINQ statements use the new var type that basically holds our data. The foreach look simply reads through the var type and in my case builds xml to be returned in the service.
The next thing I need to learn is sending that var type directly to a DataSet!!!
This pretty much sums up the WCF service – So I uploaded it to my site…
Consuming the service was just as simple
I created a windows form; I added the service reference I just built on my web site. I added a datagrid control and a button to the form. In my button click event I added the following code;
private void button2_Click(object sender, EventArgs e)
{
ServiceReference1.ServiceClient sc = new WindowsFormsApplication1.ServiceReference1.ServiceClient();
WindowsFormsApplication1.ServiceReference1.IService Proxy = sc.ChannelFactory.CreateChannel();
string list = Proxy.AllEmployees();
StringReader sr = new StringReader(list);
DS.ReadXml(sr, XmlReadMode.InferSchema);
DS.AcceptChanges();
try
{
DS.Tables[0].Clear();
}
catch { }
this.dataGridView1.DataSource = DS.Tables[0];
}
The first thing I did was create a object that references the WCF service. Then I created an object that references the WCF Interface. Next I call the AllEmployees() contract on the Interface, which calls the public string method AllEmployees(). Then convert the string back to XML and point the data source of the datagrid…
What I learned was WCF is just as simple as a regular web service. WCF is all about the contract and thus the Interface. LINQ is just plain simple!
If anyone knows how to send LINQ var to a DataSet – please let me know!