c# – 没有ASP.NET AJAX的JQuery / WCF:

当WCF配置得很好,jQuery正在构建其请求/理解响应时,我正在努力获得那个“魔法”的时刻.

我有一个服务:

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

这是由人Rick Strahl推荐的,以避免定义Web.config中的行为.

我的WCF服务接口位于另一个程序集中:

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);    
    }
}

具体的服务类是:

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 / DataMember属性装饰.

我试图使用Rick Strahl的博客http://www.west-wind.com/weblog/posts/324917.aspx(“全脂”版)上概述的方法调用我的WCF服务.调试器跳转到WCF服务中(所以我的jQuery / JSON被理解),并获取IClient并返回它.但是,当我回复响应时,我会收到各种无用的错误.我回来的错误并不意味着什么.

我正在使用POST.

我正确使用界面而不是具体对象吗?因为它进入WCF服务,它似乎是失败的结果的编码.

有人有什么想法吗?

解决方法

乍一看,您的代码有三个问题:

1:在操作合同中仅暴露基本类型时,应使用ServiceKnownTypeAttribute指定已知类型:

[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,因为后者与WebScriptServiceHostFactory不兼容.

3:使用Method =“GET”的IMHO对于称为GetClientJson的方法比Method =“POST”更加RESTful

使用WCF服务时可以给您的另一个建议是使用与Visual Studio捆绑在一起的SvcTraceViewer.exe.这是一个很好的调试工具.您需要的是将以下部分添加到您的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打开此文件,您将看到很多有用的信息.例如可以说:

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.

当然,你不应该在进入生产之前不要忘记这个部分,也可能会得到一些很大的文件.

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...