MVC、WCF ASP.NET 4.0 和 WCF查询

发布于 2024-09-26 16:25:24 字数 427 浏览 0 评论 0原文

过去几天我对 WCF 感到沮丧,所以我决定在这里发帖寻求帮助,因为.. 好吧.. 我不知道从哪里开始!.. 任何帮助将不胜感激!

首先:在 .Net 4.0 中创建 WCF 服务时,如果我希望能够创建一个使用 JQuery 接受来自 AJAX POST 数据的服务,我应该使用哪个模板? (如果可能的话,我希望能够拥有 Global.asax)。

其次:我的服务在 WCF 测试客户端中工作正常,但是当我设法让它接受 GET 请求时,测试客户端停止显示服务方法。 POST 方法似乎无法完全发挥作用。

我想开发一个在 IIS 服务器上运行的 WCF 服务,我可以通过 JQuery Ajax 调用从我的任何一个应用程序中连接该服务。

如果有人有一个教程可以指出我正确的方向,我将不胜感激,因为我无法在使用 .Net 4 的 WCF 上找到任何有效的内容。

干杯

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!

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).

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.

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.

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.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

萌逼全场 2024-10-03 16:25:24

您应该考虑的第一件事是同源政策限制。如果您无法遵守它,并且您的 Web 服务未托管在与使用 AJAX 脚本相同的域中,您可能会停止阅读我的答案并重新考虑您的架构。

如果您仍在阅读,您可以像往常一样从定义服务契约和实现开始:

[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);
    }
}

然后添加一个 fooservice.svc 文件,该文件将在 IIS 中公开该服务:

<%@ ServiceHost 
    Language="C#" 
    Debug="true" 
    Service="SomeNs.FooService" 
    CodeBehind="FooService.svc.cs" 
    Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"
%>

最后一行 Factory=" System.ServiceModel.Activation.WebScriptServiceHostFactory" 非常重要,因为这将允许您使用 JSON。

最后一部分是 web.config:

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true"/>
                <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
         </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>

最后是一个发送 AJAX 请求以使用服务的 HTML 页面:

<!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>

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.

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:

<%@ 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.

The last part is 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:

<!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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文