【问题标题】:How to call simple WCF service using jQuery or simple js如何使用 jQuery 或简单 js 调用简单的 WCF 服务
【发布时间】:2010-07-19 10:11:05
【问题描述】:

我有一个非常简单的 hello world WCF 服务,如下所示。当我通过添加 Web 服务引用通过 asp.net 项目调用它时,它工作得非常好。但是当我使用 jQuery 或标准 js ajax 调用(使用XMLHttpRequest)调用它时,它会回调成功函数但返回 null 数据。

当我尝试使用这个地址通过 Firefox 浏览器访问它时:http://localhost:8282/Test/TestService.svc/HelloWorld

它返回一个错误,代码为“a:ActionNotSupported”,错误详情为

由于 EndpointDispatcher 的 ContractFilter 不匹配,接收方无法处理带有 Action '' 的消息。这可能是因为合约不匹配(发送方和接收方之间的操作不匹配)或发送方和接收方之间的绑定/安全不匹配。检查发送方和接收方是否具有相同的合同和相同的绑定(包括安全要求,例如消息、传输、无)。

如果我将绑定更改为wsHttpBinding,那么即使在 Firefox 中它也不会返回任何内容。

代码如下:

文件“Test/ITestService.svc”:

[ServiceContract(Namespace = "http://localhost:8282/")]
public interface ITestService
{

    [OperationContract]
    string HelloWorld();
}

文件“Test/TestService.svc”:

public class TestService : ITestService
{
    public string HelloWorld()
    {
        return "This is echo from server. Hello World";
    }
}

文件“web.config”

<system.serviceModel>

    <services>
    <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior"
        <endpoint address="HelloWorld" binding="webHttpBinding" contract="radMLRPC.Test.ITestService">
            <identity>
            <dns value="localhost"/>
            </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
    <serviceBehaviors>
        <behavior name="radMLRPC.Test.TestServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
        </serviceBehaviors>
    </behaviors>
</system.serviceModel>

【问题讨论】:

    标签: javascript jquery wcf


    【解决方案1】:

    上面的代码服务只允许soap请求,所以为了允许Get http请求,我们必须修改代码如下:

    在界面中:

        [WebGet(UriTemplate="helloworld")]
        [OperationContract]
        string HelloWorld();
    

    在网络配置中:

    • 添加行为配置:

      <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService" behaviorConfiguration="webBehav">
      
    • 然后在行为中添加以下标签:

      行为>

    “请从上面删除多余的空格。没有多余空格的标签不显示”


    查看一些资源:

    使用 WCF 介绍 RESTful 服务 http://msdn.microsoft.com/en-us/magazine/dd315413.aspx

    Endpoint.TV 截屏视频:

    Endpoint.TV 通常对 WCF 和 WCF REST 内容有很好的覆盖。 http://channel9.msdn.com/shows/Endpoint/

    【讨论】:

      【解决方案2】:

      Consuming ASP.net WebServices, WCF Services and static Page methods from JavaScript (sans MS AJAX)

      对于 webHttp 和客户端脚本,mex 绑定没有用。暂时放下它。

      这个身份可能会让你有些悲伤,暂时放下它。

      您将 HelloWorld 作为您的地址,为了在您的服务上调用 HelloWorld 方法,您需要调用 http://localhost:8282/Test/TestService.svc/HelloWorld/HelloWorld。放下它。

      <services>
          <service name="radMLRPC.Test.TestService" behaviorConfiguration="radMLRPC.Test.TestServiceBehavior"
          <endpoint address="" binding="webHttpBinding" contract="radMLRPC.Test.ITestService"/>
        </service>
      </services>
      <behaviors>
          <serviceBehaviors>
          <behavior name="radMLRPC.Test.TestServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
          </behavior>
        </serviceBehaviors>
      </behaviors>
      

      现在可能不会让你一路走好,但会给我们一个更好的起点。我愿意帮你解决这个问题。

      将我们现在所拥有的与链接文章中显示的工作示例进行比较

      <system.serviceModel>
        <behaviors>
          <endpointBehaviors>
            <behavior name="AjaxEndpointBehavior">
              <enableWebScript />
            </behavior>
          </endpointBehaviors>
          <serviceBehaviors>
            <behavior name="ClientScriptServices.Service1Behavior">
              <serviceMetadata httpGetEnabled="true" />
              <serviceDebug includeExceptionDetailInFaults="true" />
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="ClientScriptServices.Service1Behavior" name="ClientScriptServices.Service1">
            <endpoint behaviorConfiguration="AjaxEndpointBehavior" binding="webHttpBinding" contract="ClientScriptServices.Service1" />
          </service>
        </services>
      </system.serviceModel> 
      

      看看我们是否可以使您的配置具有类似的形状,并且我们可以涵盖调整输入和输出格式的一些更精细的点。

      【讨论】:

      • 你好像搞错了。该链接为执行您想做的事情提供了明确的指导。您显示的配置有一些问题,也许我们可以解决这个问题。查看编辑后的答案
      • 如果没有 ,我无法解决我的问题,因为我没有使用 dotnet scriptmanager 作为客户端,而是使用它的简单 html 应用程序,没有任何服务器技术,为此我需要我的服务将其方法公开给 http网。如果我弄错了,请告诉我
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-23
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      相关资源
      最近更新 更多