none
WCF项目和网站应用程序分离后无法请求 RRS feed

  • 问题

  • 我现在在做面向服务的解决方案,故我把WCF项目作为一个单独的Service项目,WEB应用程序通过xx.svc去调用服务,但是页面jQuery总是请求失败。
    WEB程序命名空间是jQueryCallWcf.Web,WCF Service命名空间是:jQueryCallWcf.Service,在WEB应用程序中添加了AjaxService3.svc(单独文件,没有cs后置代码),指定Service="jQueryCallWcf.Service.AjaxService2",jQueryCallWcf.Service.AjaxService2是WCF的实现。在页面ajax请求中指定url为AjaxService3.svc/GetObject,ajax报415错误。
    请高手指教错误之处。

    WCF项目:
    IAjaxService2 契约:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel;
    using System.ServiceModel.Web;
    using System.ServiceModel.Activation;
    
    namespace jQueryCallWcf.Service {
        [ServiceContract(Namespace = "")]
        public interface IAjaxService2 {
            [OperationContract]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
            string[] GetNames(string a);
    
            [OperationContract]
            [WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json)]
            object GetObject();
        }
    }

    AjaxService2 实现:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.ServiceModel.Activation;
    using Newtonsoft.Json;
    
    namespace jQueryCallWcf.Service {
    
        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
        public class AjaxService2 : IAjaxService2 {
    
            public string[] GetNames(string a) {
                return new string[] { "John", "Willim", "Sonny", "Allen" };
            }
    
            public object GetObject() {
                var a = new {
                    Name = "John",
                    Age = 23,
                    Address = "shanghai",
                    Born = new DateTime(1980, 10, 20)
                };
                string str = JsonConvert.SerializeObject(a);
                return str;
            }
        }
    }
    


    WEB应用程序:
    请求页面:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="jQueryPage3.aspx.cs" Inherits="jQueryCallWcf.Web.jQueryPage3" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
        <title></title>
        <script src="scripts/jquery-1.8.0.min.js" type="text/javascript"></script>
        <script src="scripts/jquery.json-2.2.min.js" type="text/javascript"></script>
        <script type="text/javascript">
            $(function () {
                $.ajax({
                    type: 'POST',
                    url: 'AjaxService3.svc/GetObject',
                    data: '{}',
                    dataType: 'json',
                    contentType: 'application/json; charset=utf-8',
                    processdata: true,
                    success: function (msg) {
                        alert(msg.d);
                    },
                    error: function (result) {
                        alert(result.status);
                    }
                });
            });
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
        
        </div>
        </form>
    </body>
    </html>
    

    web.config

    <?xml version="1.0"?>
    <configuration>
        <system.serviceModel>
            <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
                multipleSiteBindingsEnabled="true" />
            <behaviors>
                <endpointBehaviors>
                    <behavior name="AjaxServiceBehavior">
                        <webHttp />
                    </behavior>
                    <behavior name="jQueryCallWcf.Web.AjaxServiceAspNetAjaxBehavior">
                        <enableWebScript />
                    </behavior>
                    <behavior name="jQueryCallWcf.Web.AjaxService2AspNetAjaxBehavior">
                        <enableWebScript />
                    </behavior>
                    <behavior name="jQueryCallWcf.Web.AjaxService3AspNetAjaxBehavior">
                        <enableWebScript />
                    </behavior>
                </endpointBehaviors>
                <serviceBehaviors>
                    <behavior name="AjaxServiceBehavior">
                        <serviceMetadata httpGetEnabled="true" />
                        <serviceDebug includeExceptionDetailInFaults="true" />
                    </behavior>
                </serviceBehaviors>
            </behaviors>
            <services>
                <service behaviorConfiguration="AjaxServiceBehavior" name="jQueryCallWcf.Web.AjaxService3">
                    <endpoint address="" behaviorConfiguration="jQueryCallWcf.Web.AjaxService3AspNetAjaxBehavior"
                        binding="webHttpBinding" contract="jQueryCallWcf.Service.IAjaxService2,jQueryCallWcf.Service, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
                </service>
            </services>
        </system.serviceModel>
        <system.web>
            <compilation debug="true"/>
        </system.web>
    </configuration>

    WEB应用程序中的svc,WEB项目添加了Service项目的引用

    <%@ ServiceHost 
        Language="C#" Debug="true" 
        Service="jQueryCallWcf.Service.AjaxService2,
                jQueryCallWcf.Service, 
                Version=1.0.0.0, 
                Culture=neutral, 
                PublicKeyToken=null" %>

    但是请求就是不成功。

    请教:
    1.为什么请求会失败,该如何解决?
    2.面向服务的解决方案是不是WCF应该作为单独的WEB项目,比如应用程序是http://localhost:8000,WCF服务http://localhost:8001/AjaxService2.svc。WEB和Service之间没有引用关系?那该怎么做?
    我的示例在:http://dl.vmall.com/c0mw0cveqr,见jQueryPage3.aspx页面。



    Sonny.Lin

    2013年1月9日 3:22

答案