我已经在涉及LINQ Concepts的应用程序中创建了WCF服务,但是我无法理解如何将该服务调用到我的网页上。谁能说出如何调用我在应用程序中创建的服务以及详细步骤。样本编码,以便我可以进一步进行?提前致谢。
最佳答案
我整理了一个示例,展示了如何调用WCF RESTful服务,该服务使用jQuery的$.ajax
函数从基本网页的LINQ返回结果。无论您是从LINQ返回对象还是从LINQ返回SQL,基本概念都应该是相同的-我使用的是基本的LINQ到对象只是为了说明这一点。
这是WCF服务( 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;
}
这是一个使用LINQ的简单服务,它仅返回
List<string>
中以传入字母开头的那些元素。因此,如果传入“P”,则将返回“Peach”和“Plum”作为IEnumerable<string>
。由于使用了
WebGet
属性,因此将该服务设置为从GET请求返回数据。这表明只能通过HTTP GET请求调用此方法。设置service方法以返回JSON格式的数据(比XML更流线化,但扩展性不强)。之所以这样设置,是因为ResponseFormat=WebMessageFormat.Json
属性的WebGet
设置。当我从应用程序(或浏览器!)调用此方法并为过滤器参数传递字母“P”时,我对客户端的响应是这样(通过Fiddler):
["Peach","Plum"]
这是返回的简单的JSON标记数组。然后,我可以使用此返回的数据执行任何操作。
还要注意,我不必将
IEnumberable<string>
转换为JSON格式-WCF的内置JSON序列化程序为我完成了此操作。编辑:您可以将我的简单LINQ-to-objects替换为LINQ-to-SQL查询,它将与其他所有功能保持不变:
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;
}
我的
DbTest1DataContext
是我的LINQ-to-SQL上下文,它指向具有Attendees表和Name列的数据库。此答案中的所有其他内容均保持不变(web.config,HTML,jQuery等)。这是我用于服务的web.config( 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>
除了端点行为(我已设置为使用
webHttp
)外,这几乎是标准的现成配置。这是我的客户代码( 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>
这是一个非常简单的页面,它接受来自文本框(id =源)的输入,单击按钮(id = sendit)后,它将调用我的WCF RESTful服务。我为此使用jQuery,但是您可以看到我正在
$.ajax
调用(data: { q: $("#source").val() }
)的data属性中传递过滤器值,并将其设置为GET请求(type: "GET"
),因为这就是我的方式已经配置了服务。我希望获取的数据类型是JSON(dataType: "json"
)。如果我的服务调用成功,我将调用我的成功处理程序,然后将返回的字符串添加到<ul>
元素中。希望这为您从LINQ查询中检索一组值并将其发送到基于Web的客户端提供了一个良好的起点。如果还有其他问题,请告诉我,我将相应地更新答案。
希望这可以帮助!
关于linq - WCF服务与asp.net中的LINQ to SQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3575025/