通过asp.net上的ajax消耗WCF服务?

时间:2015-09-02 19:12:50

标签: jquery asp.net ajax vb.net wcf

我有一个我创建的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项目的网络配置......

<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调用的url是什么?

1 个答案:

答案 0 :(得分:0)

我终于搞清楚了。我使用了以下资源的组合:

从这些资源发布片段是不合理的,但我会提供有希望不会失败的网络档案:

  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/en-US/97d700be-1310-4a90-bd15-8aeceb56d11e/aspnet-compatibility-mode-for-wcf-web-service

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

  7. 指向网络档案的链接: 链接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>