需要 ASP.NET (3.5) ScriptManager 的帮助来通过 AJAX 调用 WCF JSON 数据

发布于 2024-07-29 10:32:23 字数 1464 浏览 8 评论 0 原文

我正在尝试让 ASP.NET (Framework 3.5)、AJAX 和 JSON 正常工作。 我有两个问题。 首先是,当我按照 ASP.NET 的要求添加以下标记时:

[AspNetCompatibilityRequirements(RequirementsMode = 
      AspNetCompatibilityRequirementsMode.Allowed)]

我发现无法将其添加到接口声明之上,只能添加到类之上。 我希望这段代码成为一个接口。 有人可以告诉我我做错了什么吗? 错误如下:

属性 AspNetCompatibilityRequirements 不是 对此声明类型有效。 这是 仅对“类”声明有效。

    [ServiceContract(Namespace = "API.Trade")]
    [AspNetCompatibilityRequirements(RequirementsMode = 
        AspNetCompatibilityRequirementsMode.Allowed)]
    public interface ITradeService
    {
        [OperationContract(Name = "GetAllCategories")]
        string GetCategories(string itemtype, string keywordstring);

        [OperationContract(Name = "GetCategoryByNodeLevel")]
        string GetCategories(int NodeLevel); 

        [OperationContract]
        int GetTrades(string KeywordString, string TradeType);
    }

第二个问题是,在 ASPX ScriptManager 标记中:

 <asp:ScriptManager ID="ScriptManager1" runat="server">
 <Services>
 <asp:ServiceReference Path="?" />
 </Services>
 </asp:ScriptManager>

我注意到 Path= 属性应该指向 .SVC 文件。 到目前为止,我已经 成功地使用 WCF 类库来完成我的需要。 班级图书馆 有我编译的 Trade.cs、TradeService.cs 和 ITradeService.cs 文件,然后 在我的 Web 项目中引用为我的 Web 服务。

那么,“Path=”应该指向什么? 或者说,我需要补充什么?

我正在边学习边学习,感谢您的耐心。 提前致谢。

I am trying to get ASP.NET (Framework 3.5), AJAX, and JSON to work. I have two questions along those lines. This first is, when I add the below tag as required by ASP.NET:

[AspNetCompatibilityRequirements(RequirementsMode = 
      AspNetCompatibilityRequirementsMode.Allowed)]

I find that I cannot add it above an interface declaration, only a class. I want this code to be an interface. Can somebody tell me what I am doing wrong? The error is as follows:

Attribute
AspNetCompatibilityRequirements is not
valid on this declaration type. It is
only valid on 'class' declarations.

    [ServiceContract(Namespace = "API.Trade")]
    [AspNetCompatibilityRequirements(RequirementsMode = 
        AspNetCompatibilityRequirementsMode.Allowed)]
    public interface ITradeService
    {
        [OperationContract(Name = "GetAllCategories")]
        string GetCategories(string itemtype, string keywordstring);

        [OperationContract(Name = "GetCategoryByNodeLevel")]
        string GetCategories(int NodeLevel); 

        [OperationContract]
        int GetTrades(string KeywordString, string TradeType);
    }

THE SECOND question is, in the ASPX ScriptManager tag:

 <asp:ScriptManager ID="ScriptManager1" runat="server">
 <Services>
 <asp:ServiceReference Path="?" />
 </Services>
 </asp:ScriptManager>

I notice that the Path= attribute should be pointing to a .SVC file. So far, I have
successfully been using a WCF Class Library to accomplish what I need. The Class Library
has the Trade.cs, TradeService.cs, and ITradeService.cs files which I compile and then
reference as my Web Service in my Web project.

So, what should "Path=" be pointing to? Or, what do I need to add?

I am learning as I go and I appreciate your patience. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

╰◇生如夏花灿烂 2024-08-05 10:32:23

关于第一个问题。 该属性被定义为仅适用于类,因此您不能在其他任何东西上声明它。

该路径应指向您的服务正在侦听的端点(例如/services/myserivce)。

With respect to the first question. The attribute is defined to be only applicable to a class, so you can't declare it on anything else.

The path should point to the endpoint where your service is listening (e.g. /services/myserivce).

趁年轻赶紧闹 2024-08-05 10:32:23

该路径应该是 HTTP 服务的服务端点:http://host.example.com/tradeservice.svc/method

您只能将该属性应用于接口(类)的实现,而不能应用于接口本身。

The path ought to be the service endpoint for an HTTP service: http://host.example.com/tradeservice.svc/method.

You can only apply the attribute to an implementation of the interface (a class) not the interface itself.

所谓喜欢 2024-08-05 10:32:23

至于你的第二个答案。 我相信该路径需要指向实际的 Web 服务端点(.svc 或旧的端点。我忘记了,抱歉)。

您可以做的是创建一个 WebService、TradeService.svc,并实现 ITradeService 接口。 作为服务上的私有变量,创建 TradeService 的实例,并将其用作代理......就像这样

private TradeService _proxy;

public string MyMethod(){
    _proxy.MyMethod();
}

有意义。

As for your second answer. I believe the path needs to point to a actual webservice endpoint (.svc or the old one.. I forget off the top my head, sorry).

What you can do is create a WebService, TradeService.svc, and implement the ITradeService interface. As a private variable on the service create a instance of TradeService, and use it as a proxy... like so

private TradeService _proxy;

public string MyMethod(){
    _proxy.MyMethod();
}

make sence.

时光瘦了 2024-08-05 10:32:23
  • AspNetCompatibilityRequirementsAttribute.RequirementsMode 不是必需属性,可用于以编程方式设置托管模式或者您可以执行相同的操作
    在 webconfig 中使用 serviceHostingEnvironment 标记:
    <system.serviceModel> 
    <serviceHostingEnvironment ..>
    </serviceHostingEnvironment>
    </system.serviceModel>

参考:http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.aspnetcompatibilityrequirementsattribute.requirementsmode(v=vs.110).aspx

  • 您应该添加服务主机(.svc) 在您的项目中手动添加
    TradeService.svc。 您也可以通过添加新项目>来执行相同的操作 世界碳纤维
    服务,但这将覆盖您的服务 cs 文件。 一旦你有
    添加您的 svc 文件后,您可以添加以下行:
 
     <%@ ServiceHost Language="C#" Debug="true" Service="TradeService" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

请注意,WebScriptServiceHostFactory 会自动将 ASP.NET Ajax 端点添加到您的服务中。 最后将您的“路径”属性指向这个新创建的“TradeService.svc”。

您可以通过访问 http://localhost.me/TradeService.svc 来测试您的服务,

并且 Ajax 支持可以是在 http://localhost.me/TradeService.svc/jsdebug 进行测试

参考:http://berniecook.wordpress.com/ 2012/01/08/consuming-a-wcf-service-with-jquery-or-scriptmanager/

源代码:https://docs.google.com/open?id=0Bz2usIBCcor0NDQxZDc2ODYtNTUzMi00OTRlLTlhOGMtMGI4Y2RhNGIzYWNj

  • AspNetCompatibilityRequirementsAttribute.RequirementsMode is not a required attribute and can be used for setting the hosting mode programmatically Or you can do the same
    in webconfig by using serviceHostingEnvironment tag:
    <system.serviceModel> 
    <serviceHostingEnvironment ..>
    </serviceHostingEnvironment>
    </system.serviceModel>

Reference : http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.aspnetcompatibilityrequirementsattribute.requirementsmode(v=vs.110).aspx

  • You should add a service host(.svc) in your project by manually adding
    TradeService.svc. You can also do same by adding new item > WCF
    Service but that will override your service cs files. Once you have
    your svc file added then you can add following line:
 
     <%@ ServiceHost Language="C#" Debug="true" Service="TradeService" Factory="System.ServiceModel.Activation.WebScriptServiceHostFactory" %>

Note that WebScriptServiceHostFactory will automatically add ASP.NET Ajax endpoint to your service. Finally point your "path" attribute to this newly created "TradeService.svc".

You can test your service by visiting http://localhost.me/TradeService.svc

And Ajax support can be tested at http://localhost.me/TradeService.svc/jsdebug

Reference : http://berniecook.wordpress.com/2012/01/08/consuming-a-wcf-service-with-jquery-or-scriptmanager/

SourceCode : https://docs.google.com/open?id=0Bz2usIBCcor0NDQxZDc2ODYtNTUzMi00OTRlLTlhOGMtMGI4Y2RhNGIzYWNj

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文