使用Html / Javascript消费WCF服务

时间:2014-02-13 16:02:15

标签: javascript vb.net wcf web-services

FILE WebService.svc.vb

Public Class WebService
    Implements IWebService
    Public Function HelloThere(ByVal name As String) As String Implements IWebService.HelloThere
        Return "Hello there " & name
    End Function
End Class

FILE IWebService.vb

Imports System
Imports System.ServiceModel

<ServiceContract()>
Public Interface IWebService

    <OperationContract()>
    Function InsertReport(ByVal name As String) As String
End Interface

FILE Web.config

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, set the value below to false before deployment -->
          <serviceMetadata httpGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="WebService">
        <endpoint address="WebService" binding="basicHttpBinding" contract="IWebService" />
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />

      </service>
    </services>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

FILE WebService.svc

<%@ ServiceHost Language="VB" Debug="true" Service="WebService.WebService" CodeBehind="WebService.svc.vb" %>

我的问题是:这项服务是在IIS 7中远程托管的(.5我认为),我想在Web应用程序中使用它。这个Web应用程序使用jquery,只是一个标准的HTML5文档。我已经看到很多例子,人们用一些javascript或AJAX等来调用WCF服务......我正在寻找一些能够做到这一点的代码以及额外需要的web.config(和/或对我服务的一般更改) )修改以允许此类消费。

1 个答案:

答案 0 :(得分:1)

如果您的服务正常,那么您无需在服务器端进行任何更改。您的Web服务功能独立于调用客户端。您可以使用SoapUI等工具测试您的服务。

下一个HTML代码段应该可以作为您服务的客户端正常运行,并使帖子网址适合您的服务。如您所见,它发布了json数据,

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
    <script type="text/javascript">
      $(document).ready(function () {
        $("a#CallService").click(function (e) {
          e.preventDefault();

          $.ajax({
            type: 'POST',
            data: '{"name": "' + $("input#name").val() + '"}',
            url: 'http://targetURL/Hello',
            contentType: 'application/json; charset=utf-8',
            dataType: 'json',
            success:
              function (data, textStatus, XMLHttpRequest) {
                alert(data.d);
              },
            error:
              function (XMLHttpRequest, textStatus, errorThrown) {
                alert(textStatus);
              }
          });
        });
      });    
    </script>
</head>
<body>
  <input id="button" /><a id="CallService" href="#">Test</a>  
</body>
</html>

希望我帮忙!