I've spent the past few days getting frustrated with WCF, so I've decided to post for help on here because.. well.. I don't have a clue where to start!.. any help would be appreciated!
我花了这么多天对WCF感到沮丧,所以我决定在这里发帖求助,因为......好吧......我不知道从哪里开始!..任何帮助都将不胜感激!
Firstly: When creating a WCF Service in .Net 4.0, which template should I use if I want to be able to create a service that will accept data from an AJAX POST using JQuery? (I'd like to be able to have a Global.asax if possible).
首先:在.Net 4.0中创建WCF服务时,如果我希望能够使用JQuery创建一个接受来自AJAX POST的数据的服务,我应该使用哪个模板? (如果可能,我希望能够拥有Global.asax)。
Secondly: My service works fine in the WCF Test Client, however when I manage to get it to accept GET requests, the Test Client stops showing the service methods. POST methods just seem to refuse to work outright.
其次:我的服务在WCF测试客户端中工作正常,但是当我设法让它接受GET请求时,测试客户端停止显示服务方法。 POST方法似乎完全拒绝工作。
I'd like to develop a WCF service that will run on an IIS server that I can hook into from any one of my applications via a JQuery Ajax call.
我想开发一个WCF服务,它将在IIS服务器上运行,我可以通过JQuery Ajax调用从任何一个应用程序挂钩。
If anyone has a tutorial that point's me in the right direction, that would be greatly appreciated as I havn't been able to find anything on WCF using .Net 4, that works.
如果有人有一个教程指向我正确的方向,那将非常感激,因为我无法使用.Net 4在WCF上找到任何东西,这是有效的。
Cheers
干杯
1 个解决方案
#1
6
The first thing that you should consider is the same origin policy restriction. If you are not able to comply with it and your web service is not hosted on the same domain as the consuming AJAX script you may stop reading my answer here and rethink your architecture.
您应该考虑的第一件事是相同的原产地政策限制。如果您无法遵守它并且您的Web服务未与消费AJAX脚本托管在同一个域中,您可以在此处停止阅读我的答案并重新考虑您的体系结构。
If you are still reading you could start by defining the service contract and implementation as usual:
如果您仍在阅读,您可以开始像往常一样定义服务合同和实施:
[ServiceContract]
public interface IFoo
{
[OperationContract]
string GetData(int value);
}
public class FooService : IFoo
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Then you add a fooservice.svc
file which will expose the service in IIS:
然后添加一个fooservice.svc文件,该文件将在IIS中公开该服务:
<%@ ServiceHost
Language="C#"
Debug="true"
Service="SomeNs.FooService"
CodeBehind="FooService.svc.cs"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>
The last line Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
is extremely important as this is what will allow you to use JSON.
最后一行Factory =“System.ServiceModel.Activation.WebScriptServiceHostFactory”非常重要,因为这将允许您使用JSON。
The last part is web.config:
最后一部分是web.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
And finally an HTML page sending AJAX request to consume the service:
最后一个HTML页面发送AJAX请求来使用该服务:
<!DOCTYPE html>
<html>
<head>
<title>WCF Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.json.org/json2.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
// Notice the URL here: Need to be hosted on the same domain
url: '/fooservice.svc/getdata',
type: 'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ value: 7 }),
success: function (result) {
alert(result.d);
}
});
});
</script>
</head>
<body>
</body>
</html>
#1
6
The first thing that you should consider is the same origin policy restriction. If you are not able to comply with it and your web service is not hosted on the same domain as the consuming AJAX script you may stop reading my answer here and rethink your architecture.
您应该考虑的第一件事是相同的原产地政策限制。如果您无法遵守它并且您的Web服务未与消费AJAX脚本托管在同一个域中,您可以在此处停止阅读我的答案并重新考虑您的体系结构。
If you are still reading you could start by defining the service contract and implementation as usual:
如果您仍在阅读,您可以开始像往常一样定义服务合同和实施:
[ServiceContract]
public interface IFoo
{
[OperationContract]
string GetData(int value);
}
public class FooService : IFoo
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
Then you add a fooservice.svc
file which will expose the service in IIS:
然后添加一个fooservice.svc文件,该文件将在IIS中公开该服务:
<%@ ServiceHost
Language="C#"
Debug="true"
Service="SomeNs.FooService"
CodeBehind="FooService.svc.cs"
Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>
The last line Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
is extremely important as this is what will allow you to use JSON.
最后一行Factory =“System.ServiceModel.Activation.WebScriptServiceHostFactory”非常重要,因为这将允许您使用JSON。
The last part is web.config:
最后一部分是web.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
And finally an HTML page sending AJAX request to consume the service:
最后一个HTML页面发送AJAX请求来使用该服务:
<!DOCTYPE html>
<html>
<head>
<title>WCF Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="http://www.json.org/json2.js"></script>
<script type="text/javascript">
$(function () {
$.ajax({
// Notice the URL here: Need to be hosted on the same domain
url: '/fooservice.svc/getdata',
type: 'post',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify({ value: 7 }),
success: function (result) {
alert(result.d);
}
});
});
</script>
</head>
<body>
</body>
</html>