|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
Note: This is an unedited contribution. If this article is inappropriate,
needs attention or copies someone else's work without reference then please
Report This Article
IntroductionIn this article, a web service with http binding is created, exposing a method that queries a database table using Linq. The method is defined in the service contract of the WCF service. Each resulting record from the Linq query is populated in an object that is defined in a data contract of the WCF service. The collection of objects are then returned by the service method as a generic IEnumerable collection. Defining the WCF serviceWe first create the WCF service in Visual Studio 2008 and version 3.5 of .Net, using the wsHttp binding. The service is defined in the web.config file, as follows: <services> <service name="SomeService" behaviorConfiguration="ServiceBehavior"> <endpoint address="" binding="wsHttpBinding" contract="ISomeService"> ... Note how the address attribute of the endpoint is left blank for now, letting IIS use a default address. This can be changed to a fixed address at deployment time. We then define the service contract in the ISomeService.cs file. The service contract is where all the service methods are defined: [ServiceContract]
public interface ISomeService
{
[OperationContract]
string GetData(int value);
[OperationContract]
IEnumerable<EmpRecord> GetEmpData(string empLastName);
}
We have 2 methods defined, the first one is just a simple method that returns a string based on an int passed in as an argument. The second method, which is the one that is of real interest to us, accepts a last name of an employee and returns a collection of [DataContract]
public class EmpRecord
{
int id = 0;
string fName = "";
string lName = "";
string state = "";
[DataMember]
public int Id
{
get { return id; }
set { id = value; }
}
[DataMember]
public string First
{
get { return fName; }
set { fName = value; }
}
[DataMember]
public string Last
{
get { return lName; }
set { lName = value; }
}
[DataMember]
public string State
{
get { return state; }
set { state = value; }
}
}
The In the SomeService.cs file of the WCF project, we implement the service interface and methods that we defined above, as follows: public class SomeService : ISomeService { public string GetData(int value) { return string.Format("You entered: {0}", value); } public IEnumerable<EmpRecord> GetEmpData(string empLastName) { LinqDataClassesDataContext ctxt = new LinqDataClassesDataContext(); return from r in ctxt.Employees where r.lName.Contains(empLastName) select new EmpRecord { Id = r.ID, First = r.fName, Last = r.lName, State = r.state}; } } A Linq expression is used in the body of the GetEmpData method to query the Employee table, returning all rows that have their lName fields containing the last name string passed in as an argument. The Employee table is made up of the following fields: ID, fName, lName, City, and State. All matching rows are returned by the method as an IEnumerable collection. The Consuming the WCF serviceTo consume the WCF service, we create an ASP.Net web application project, and add a service reference to it in the Solution Explorer. We then create an instance of the service and call the appropriate methods, such as: protected void Page_Load(object sender, EventArgs e) { SomeSvc.SomeServiceClient someSrvc = new SomeSvc.SomeServiceClient(); var coll = someSrvc.GetEmpData("Evans"); GridView2.DataSource = coll; GridView2.DataBind(); } SomeSvc is the name of the service reference that I added to my ASP.Net web app project, which gives us access to the client proxy class. I then call the GetEmpData method, passing an employee last name, and bind the returned collection of matching Alternatively, we can just iterate throught the collection of bool bFound = false; foreach (SamSvc.EmpRecord empl in coll) { if (empl.State.IndexOf("FL") >= 0) { bFound = true; break; } } Points of InterestThe simplicity of defining our own objects as data contracts in a WCF service, and making use of easy to use Linq expressions to encapsulate returned database records in those objects. Similarly, service contracts make it fairly straight forward to define our own methods in the WCF service, accepting and returning our own defined objects. HistoryInitial post.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||