MVC, WCF ASP.NET 4.0 & JQUERY

可紊 提交于 2019-12-21 17:49:48

问题


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


回答1:


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>


来源:https://stackoverflow.com/questions/3883329/mvc-wcf-asp-net-4-0-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!