JQuery的/ WCF没有ASP.NET AJAX: [英] JQuery/WCF without ASP.NET AJAX:

查看:141
本文介绍了JQuery的/ WCF没有ASP.NET AJAX:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是正确的挣扎让这神奇的时刻,WCF是很好的配置和jQuery是构建其请求/响应的理解很好。

I am proper struggling getting that "magic" moment when WCF is configured nicely and jQuery is structuring its requests/understanding responses nicely.

我有一个服务:

<%@ ServiceHost Language="C#" Debug="true" Service="xxx.yyy.WCF.Data.ClientBroker" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory"  %>

这是推荐人里克施特拉尔以避免Web.config中定义的行为。

This was recommended by the man Rick Strahl to avoid having to define the behaviours within Web.config.

我的WCF服务接口坐在另一个程序:

My interface for the WCF service sits in another assembly:

namespace xxx.yyy.WCF.Data
{       
    [ServiceContract(Namespace = "yyyWCF")] 
    public interface IClientBroker
    {
        [OperationContract]         
        [WebInvoke(Method="POST",BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)]
        IClient GetClientJson(int clientId);    
    }
}

具体服务类是:

The concrete service class is:

namespace xxx.yyy.WCF.Data
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    class ClientBroker : IClientBroker 
    {
        public IClient GetClientJson(int clientId)
        {
            IClient client=new Client();

            // gets and returns an IClient
            return client;
        }
    }
}

我的IClient是如此饰有DataContract /数据成员的实体框架类相应的属性。

My IClient is an Entity Framework class so is decorated with DataContract/DataMember attributes appropriately.

我想打电话给使用概述了里克施特拉尔的博客的方法中的的 http://www.west-wind.com/weblog/posts/324917.aspx (以下简称全脂版本)。调试器跳跃到WCF服务罚款(所以我的jQuery / JSON被理解),并得到IClient并返回。然而,当我返回响应,我得到了各种各样的无用的错误。我取回的错误不会太大意义。

I am trying to call my WCF service using the methods outlined on Rick Strahl's blog at http://www.west-wind.com/weblog/posts/324917.aspx (the "full fat" version). The debugger jumps into the WCF service fine (so my jQuery/JSON is being understood) and gets the IClient and returns it. However, when I return the response, I get various useless errors. The errors I am getting back don't mean much.

我使用POST。

我是否有权使用一个接口而不是一个具体的对象?因为它进入WCF服务,它似乎是,失败的结果的编码。

Am I right to be using an Interface instead of a concrete object? As it does get into the WCF service, it does seem to be the encoding of the result that is failing.

有没有人有什么想法?

推荐答案

乍看之下有三个问题,您的code:

At first glance there are three problems with your code:

1:你应该使用 ServiceKnownTypeAttribute 以在你的业务合同只露出基本类型时指定已知类型:

1: you should use the ServiceKnownTypeAttribute to specify known types when exposing only base types in your operation contracts:

[ServiceContract(Namespace = "yyyWCF")]     
public interface IClientBroker
{
    [OperationContract]
    [ServiceKnownType(typeof(Client))]
    [WebInvoke(
        Method="GET",
        BodyStyle=WebMessageBodyStyle.WrappedRequest,
        ResponseFormat=WebMessageFormat.Json)]
    IClient GetClientJson(int clientId);

}

2:您应该使用 WebMessageBodyStyle.WrappedRequest 而不是 WebMessageBodyStyle.Wrapped ,因为后者是不符合<兼容一个href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.webscriptservicehostfactory.aspx"相对=nofollow> WebScriptServiceHostFactory 。

2: You should use WebMessageBodyStyle.WrappedRequest instead of WebMessageBodyStyle.Wrapped because the latter is not compatible with WebScriptServiceHostFactory.

3:恕我直言使用方法=GET会更REST风格的一个名为GetClientJson比方法=POST的方法

3: IMHO using Method="GET" would be more RESTful for a method called GetClientJson than Method="POST"

另一种意见与WCF服务工作时,我可以给你是使用 SvcTraceViewer。 EXE 捆绑使用Visual Studio。这对于调试一个伟大的工具。所有你需要的是下面的部分添加到您的应用程序/ web.config中:

Another advice I could give you when working with WCF services is to use SvcTraceViewer.exe bundled with Visual Studio. It is a great tool for debugging purposes. All you need is to add the following section to your app/web.config:

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel"
              switchValue="Information, ActivityTracing"
              propagateActivity="true">
        <listeners>
          <add name="sdt"
              type="System.Diagnostics.XmlWriterTraceListener"
              initializeData= "WcfDetailTrace.e2e" />
        </listeners>
      </source>
    </sources>
  </system.diagnostics>

然后调用Web方法和WcfDetailTrace.e2e文件将在您的网站的根目录下生成。接下来打开此文件SvcTraceViewer.exe,你会看到很多有用的信息。例如,它可以说:

Then invoke the web method and WcfDetailTrace.e2e file will be generated in your web site root directory. Next open this file with SvcTraceViewer.exe and you will see lots of useful information. For example it could say:

无法序列类型的参数   MyNamespace.Client(操作   GetClientJson,合同   'IClientBroker'),因为它是不   精确类型'MyNamespace.IClient中   方法签名,而不是在   已知类型的集合。为了   序列化的参数,添加的类型   到已知类型集合的   使用操作   ServiceKnownTypeAttribute。

Cannot serialize parameter of type 'MyNamespace.Client' (for operation 'GetClientJson', contract 'IClientBroker') because it is not the exact type 'MyNamespace.IClient' in the method signature and is not in the known types collection. In order to serialize the parameter, add the type to the known types collection for the operation using ServiceKnownTypeAttribute.

当然,你不应该在投入生产之前忘了评论这一部分或者你可能最终得到一些pretty的大文件。

Of course you should not forget commenting this section before going into production or you might end up with some pretty big files.

这篇关于JQuery的/ WCF没有ASP.NET AJAX:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆