繁体   English   中英

通过asp.net上的Ajax使用WCF服务吗?

[英]Consume WCF service via ajax on asp.net?

我创建了一个WCF:

http://localhost:63542/GetStoresByLocation.svc

它将驻留在与使用它的页面不同的服务器上。

我需要通过ajax来使用它。 公开方法是

Public GetStores(<some params>) As String blahblah

合同因此被装饰:

<OperationContract> <WebInvoke(Method:="GET", BodyStyle:=WebMessageBodyStyle.Wrapped, ResponseFormat:=WebMessageFormat.Json)>

这是WCF项目的Web配置...

<system.serviceModel>
<services>
  <service name="BRCWS.StoreLocator.GetStoresByLocation" behaviorConfiguration="ServiceBehavior">
    <!--<endpoint address="" binding="webHttpBinding" contract="BRCWS.StoreLocator.IGetStoresByLocation" behaviorConfiguration="EndpBehavior"/>-->
    <endpoint address="mex" binding="mexHttpBinding" contract="BRCWS.StoreLocator.IGetStoresByLocation" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="true" />
    </behavior>      
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="EndpBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

我需要两件事:

  1. 我想在asp.net(ascx)控件/页面中通过ajax来使用它,这是我想念的吗?

  2. Ajax调用的网址是什么?

我终于确定了。 我使用了以下资源的组合:

从这些资源中发布代码片段是不合理的,但是我将提供希望不会消失的Web存档:

  1. 构建基本的WCF服务,该服务解析为您在运行服务项目时弹出的WCF测试仪中看到的地址。

  2. 按照此处的建议实施代码: http : //www.aspsnippets.com/Articles/Consuming-WCF-Rest-Service-using-jQuery-AJAX-in-ASPNet.aspx

  3. 运行您的服务,并从WCF测试器复制该服务的地址,添加您拥有的任何GET参数。

  4. 解决浏览器给您的错误。 如果您只是阅读它们,它们是非常自我解释的。

  5. 如果您仍然遇到问题,请转到此处: https : //social.msdn.microsoft.com/Forums/silverlight/zh-CN/97d700be-1310-4a90-bd15-8aeceb56d11e/aspnet-compatibility-mode-for-wcf-网络服务

  6. 您的服务将不会显示在WCF测试器中,因为您的web.config将不会为该服务指定地址。 因此,测试人员将找不到它。 我建议您在运行基本服务形式后就获取地址(这样就可以获取端口,等等)

链接到Web存档:链接1: https : //archive.is/VefFk链接2: https : //archive.is/XKMyH

代码段:

接口:

<ServiceContract()>
Public Interface IGetStoresByLocation
    <OperationContract>
    <System.ServiceModel.Web.WebInvoke(Method:="GET", ResponseFormat:=System.ServiceModel.Web.WebMessageFormat.Json)>
    Function GetStores(<blahblah>) as 

类:

Imports System.ServiceModel.Activation
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class GetStoresByLocation
    Implements IGetStoresByLocation
<WebInvoke(Method:="GET", ResponseFormat:=WebMessageFormat.Json)>
Public Function GetStores(<blahblah>) as string Implements IGetStoresByLocation.GetStores

服务网址:

http://localhost:63542/GetStoresByLocation.svc/GetStores?<params in get format>

网页配置:

<system.serviceModel>
<behaviors>
  <serviceBehaviors>
    <behavior name="ServiceBehavior">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="ServiceAspNetAjaxBehavior">
      <enableWebScript/>
    </behavior>
  </endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
  <service name="BRCWS.StoreLocator.GetStoresByLocation" behaviorConfiguration="ServiceBehavior">
    <endpoint address="" binding="webHttpBinding" contract="BRCWS.StoreLocator.IGetStoresByLocation" behaviorConfiguration="ServiceAspNetAjaxBehavior">
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
  </service>
</services>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM