I have Created the WCF Service in my application which involves LINQ Concepts but i cant understand how to call t开发者_开发知识库hat service to my web pages.Can anyone say me how to call the service that i have created to my application with the detailed steps along with some sample codings So that i can proceed further? Thanks In Advance.
Here's a sample I put together to show how to call a WCF RESTful service that returns the result from LINQ from a basic web page using jQuery's $.ajax
function. Whether you're returning from a LINQ to objects or LINQ to SQL, the basic concept should be the same -- I'm using a basic LINQ-to-object just to illustrate the point.
Here's the WCF Service (Service2.svc.cs):
[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode=AspNetCompatibilityRequirementsMode.Allowed)]
public class Service2
{
public static List<string> myValues = new List<string>()
{
"Apple", "Orange", "Apricot", "Banana", "Grape", "Peach", "Plum"
};
[OperationContract]
[WebGet(UriTemplate="Filter?q={filter}",
ResponseFormat=WebMessageFormat.Json)]
public IEnumerable<string> FilterList(string filter)
{
var items = from v in myValues
where v.StartsWith(filter)
select v;
return items;
}
This is a simple service that uses LINQ to return only those elements in my List<string>
that start with the letter passed in. So if you pass in 'P', you get "Peach" and "Plum" returned as an IEnumerable<string>
.
This service is set up to return data from a GET request because of the use of the WebGet
attribute. This indicates that this method can only be called via an HTTP GET request. The service method is set up to return data in JSON format (which is more stream-lined than XML, but not as extensible). It's set up that way because of the ResponseFormat=WebMessageFormat.Json
setting for the WebGet
attribute.
When I invoke this method from my application (or a browser!) and pass in the letter "P" for my filter parameter, my response to the client is this (via Fiddler):
["Peach","Plum"]
This is a simple JSON-notated array that's returned. I can then use this returned data to do whatever.
Also notice that I didn't have to convert the IEnumberable<string>
to JSON format -- WCF's built-in JSON serializer did that for me.
EDIT: You can replace my simple LINQ-to-objects with a LINQ-to-SQL query and it will work with everything else unchanged:
public IEnumerable<string> FilterList(string filter)
{
DbTest1DataContext context = new DbTest1DataContext();
var names = from n in context.Attendees
where n.Name.StartsWith(filter)
select n.Name;
return names;
}
My DbTest1DataContext
is my LINQ-to-SQL context pointing to a database that has an Attendees table with a column of Name. Everything else in this answer remains unchanged (web.config, HTML, jQuery, etc.).
Here's my web.config for the service (web.config):
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WcfRestService1.Service2AspNetAjaxBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
<services>
<service name="WcfRestService1.Service2">
<endpoint address="" behaviorConfiguration="WcfRestService1.Service2AspNetAjaxBehavior"
binding="webHttpBinding" contract="WcfRestService1.Service2" />
</service>
</services>
</system.serviceModel>
This is pretty much standard, out-of-the-box configuration except for the endpoint behavior, which I've set to use webHttp
.
Here's my client code (TestService.htm):
<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#sendit").click(function (e) {
e.preventDefault();
$.ajax({
url: "Service2.svc/Filter",
data: { q: $("#source").val() },
type: "GET",
dataType: "json",
success: function (data) {
var theResults = $("#results");
theResults.empty();
$.each(data, function (i, item) {
theResults.append("<li>" + item + "</li>");
});
},
error: function (x, t, m) { alert(t + " :: " + m); }
});
});
});
</script>
</head>
<body>
<div>
Starts with: <input id="source" type="text" />
<button id="sendit">Send It</button>
</div>
<div>
<ul id="results">
</ul>
</div>
</body>
</html>
This is a very simple page that takes the input from a text box (id = source) and, after you click on the button (id = sendit), it invokes my WCF RESTful service. I'm using jQuery for this, but you can see that I'm passing my filter value in the data property of the $.ajax
call ( data: { q: $("#source").val() }
), I'm setting this up as a GET request (type: "GET"
), because that's how I've configured the service. The data type that I expect to get back is JSON (dataType: "json"
). And if my service call is successful, I invoke my success handler, which then just adds the returned strings to a <ul>
element.
Hopefully this gives you a good starting point for retrieving a set of values from a LINQ query and sending it to a web-based client. Please let me know if there are more questions and I'll update my answer accordingly.
Hope this helps!
精彩评论