在发送JSON的AJAX Web应用程序中帮助WCF服务

时间:2009-11-23 02:09:49

标签: ajax wcf json

我正在尝试设置一个ASPX页面,以通过ScriptManager调用WCF服务。出于某种原因,当我调用servcie的“SpinStitch.Services.Chat.IChatter.CreateChatSession”方法时,我收到此错误“服务器方法'CreateChatSession'失败了。”我在服务器上的CreateChatSession方法上设置了一个断点,但它永远不会那么远。我不确定我的设置是否错误。我已经查看了100个如何设置它的样本,它们看起来大致相同。 Jsut noe<%= ChatSessionObject%>  属性来自后面的ASPX代码,并且正在创建一个JSON序列化对象。有什么想法吗?

Interface:
using System.ServiceModel;
using System.ServiceModel.Web;
namespace SpinStitch.Services.Chat
{
    [ServiceContract(Namespace = "SpinStitch.Services.Chat")]
    public interface IChatter
    {

        [OperationContract]
        bool CreateChatSession(Domain.Objects.ChatSession chat);

    }

}

Service:
using System.ServiceModel.Activation;
using SpinStitch.Domain.Adapters;

namespace SpinStitch.Services.Chat
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class Chatter : IChatter
    {

        public bool CreateChatSession(Domain.Objects.ChatSession chat)
        {
            return ChatSessionAdapter.CreateChatSession(chat) > 0;
        }
    }

}

Web.Config of hosted service:
<system.serviceModel>

    <behaviors>
      <endpointBehaviors>
        <behavior name="ChatBehaviorConfig">
          <enableWebScript/>         
        </behavior>

      </endpointBehaviors>
            <serviceBehaviors>
                        <behavior name="ChatBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>

        </behavior>

            </serviceBehaviors>
        </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true">
    </serviceHostingEnvironment>

    <services>

      <service behaviorConfiguration="ChatBehavior" name="SpinStitch.Services.Chat.Chatter">
        <endpoint address="http://servicesdev.spinstitch.com/Chat.svc" binding="webHttpBinding" behaviorConfiguration="ChatBehaviorConfig"  bindingConfiguration="" name="Chat" contract="SpinStitch.Services.Chat.IChatter" />
      </service>
        </services>
    </system.serviceModel>

Web Config of Ajax App:
<system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="Chat">
          <textMessageEncoding maxReadPoolSize="64" maxWritePoolSize="16"
            messageVersion="Soap12" writeEncoding="utf-8">
            <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          </textMessageEncoding>
        </binding>
      </customBinding>
          </bindings>
    <client>
           <endpoint binding="customBinding" bindingConfiguration="Chat"
        contract="ServicesDevChat.IChatter" name="Chat" />
    </client>
  </system.serviceModel>


ASPX:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="AnonymousUser.aspx.vb" Inherits="TestArea_ChatTest_AnonymousUser" %>

<!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 runat="server">
    <title></title>
</head>
<script language="javascript" type="text/javascript">
function pageLoad() {
    var chatSession = <%=ChatSessionObject %>
    SpinStitch.Services.Chat.IChatter.CreateChatSession(chatSession, OnRetrieveLoadData, failed)
}

function OnRetrieveLoadData(recordInserted) {
    alert(recordInserted)
}

function failed(sender, e) {
    alert(sender._message);
}
</script>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        <Services>
            <asp:ServiceReference Path="http://servicesdev.spinstitch.com/chat.svc" />
        </Services>

        </asp:ScriptManager>



    </div>
    </form>
</body>
</html>

2 个答案:

答案 0 :(得分:0)

看起来ChatSession不是DataContract / DataMember对象。相反,它似乎是某种纯粹的脚本对象。是对的吗?如果是这样,那可能是操作失败的原因。

答案 1 :(得分:0)

此操作失败,因为该服务在单独的域中运行。在.Net 3.5下,您无法跨域。感谢大家的期待!