【问题标题】:Help with WCF servcie in AJAX web applicaiton sending JSON帮助 AJAX Web 应用程序中的 WCF 服务发送 JSON
【发布时间】:2009-11-23 02:09:49
【问题描述】:

我正在尝试设置一个 ASPX 页面以通过 ScriptManager 调用 WCF 服务。出于某种原因,当我调用服务的“SpinStitch.Services.Chat.IChatter.CreateChatSession”方法时,我收到此错误“服务器方法'CreateChatSession'失败。'我在服务器中的 CreateChatSession 方法上设置了一个断点,但它永远不会走那么远。我不确定我设置错了什么。我查看了 100 个如何设置它的示例,它们看起来都差不多。 Jsut noe 属性来自背后的 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>

【问题讨论】:

    标签: ajax wcf json


    【解决方案1】:

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

    【讨论】:

    • 实际上,它失败了,因为该服务在单独的域中运行,这在 3.5 中是不可以的。现在,在 4.0 中,您可以跨域。我会把这个作为答案发布。
    【解决方案2】:

    因为服务在单独的域中运行而失败。在 .Net 3.5 下,您不能跨域。感谢大家观看!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-09-26
      • 1970-01-01
      • 1970-01-01
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 2011-11-29
      相关资源
      最近更新 更多