C# 没有 ASP.NET AJAX 的 JQuery/WCF:
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/655400/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JQuery/WCF without ASP.NET AJAX:
提问by Program.X
I am proper struggling to get that "magic" moment when WCF is configured nicely and jQuery is structuring its requests/understanding responses nicely.
当 WCF 配置得很好并且 jQuery 很好地构建其请求/理解响应时,我正在努力获得那个“神奇”的时刻。
I have a service:
我有一个服务:
<%@ ServiceHost Language="C#" Debug="true" Service="xxx.yyy.WCF.Data.ClientBroker" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>
This was recommended by the man Rick Strahlto avoid having to define the behaviors within Web.config.
这是由Rick Strahl推荐的,以避免必须在 Web.config 中定义行为。
My interface for the WCF service sits in another assembly:
我的 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);
}
}
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;
}
}
}
My IClient is an Entity Framework class so it is decorated with DataContract/DataMember attributes appropriately.
我的 IClient 是一个实体框架类,因此它适当地使用 DataContract/DataMember 属性进行修饰。
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.
我正在尝试使用 Rick Strahl 博客http://www.west-wind.com/weblog/posts/324917.aspx(“全脂”版本)中概述的方法调用我的 WCF 服务。调试器很好地跳转到 WCF 服务(所以我的 jQuery/JSON 被理解)并获取 IClient 并返回它。但是,当我返回响应时,会收到各种无用的错误。我回来的错误没有多大意义。
I am using POST.
我正在使用 POST。
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.
我使用接口而不是具体对象是否正确?当它进入 WCF 服务时,它似乎确实是失败的结果编码。
Does anyone have any ideas?
有没有人有任何想法?
采纳答案by Darin Dimitrov
At first glance there are three problems with your code:
乍一看,您的代码存在三个问题:
1: you should use the ServiceKnownTypeAttributeto specify known types when exposing only base types in your operation contracts:
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: You should use WebMessageBodyStyle.WrappedRequest
instead of WebMessageBodyStyle.Wrapped
because the latter is not compatible with WebScriptServiceHostFactory.
2:您应该使用WebMessageBodyStyle.WrappedRequest
而不是WebMessageBodyStyle.Wrapped
因为后者与WebScriptServiceHostFactory不兼容。
3: IMHO using Method="GET" would be more RESTful for a method called GetClientJson than Method="POST"
3:恕我直言,对于名为 GetClientJson 的方法,使用 Method="GET" 比 Method="POST" 更具有 RESTful
Another advice I could give you when working with WCF services is to use SvcTraceViewer.exebundled 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:
在使用 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>
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:
然后调用 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.
无法序列化“MyNamespace.Client”类型的参数(对于操作“GetClientJson”,合同“IClientBroker”),因为它不是方法签名中的确切类型“MyNamespace.IClient”,并且不在已知类型集合中。为了序列化参数,使用 ServiceKnownTypeAttribute 将类型添加到操作的已知类型集合中。
Of course you should not forget commenting this section before going into production or you might end up with some pretty big files.
当然,您不应该忘记在投入生产之前评论此部分,否则您最终可能会得到一些非常大的文件。
回答by Mike_G
回答by poeticGeek
Related to the question, a while ago I posted an article on my blog showing all the steps needed to get a WCF service working together with jQuery code on the client side:
与这个问题相关,不久前我在我的博客上发表了一篇文章,展示了让 WCF 服务与客户端的 jQuery 代码协同工作所需的所有步骤:
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/
http://yoavniran.wordpress.com/2009/08/02/creating-a-webservice-proxy-with-jquery/