使用 GET 请求使 WCF Web 服务工作 [英] Making a WCF Web Service work with GET requests

查看:36
本文介绍了使用 GET 请求使 WCF Web 服务工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景
我过去创建了 ASMX Web 服务,并且能够使用地址约定从 Web 浏览器和 Ajax GET 请求访问该服务:
MyService.asmx/MyMethod?Param=xxx

我刚刚开始使用 WCF,并在我的 ASP.NET 项目中创建了一个新的 Web 服务.它会创建一个扩展名为 .svc 的文件,例如 MyService.svc.

I just got started using WCF and created a new web service in my ASP.NET project. It creates a file with the .svc extension such as MyService.svc.

现状
我可以使用 VS2008 附带的 WcfTestClient 使用该服务.我还可以通过在另一个项目中添加服务引用或使用 svcutil.exe 命令行生成代理和配置文件来创建自己的 WCF 客户端.

Current Situation
I am able to consume the service using the WcfTestClient that comes with VS2008. I am also able to create my own WCF Client by either adding a service reference in another project or using the svcutil.exe commandline to generate the proxy and config file.

问题
当我尝试使用 MyService.svc/MyMethod?MyParam=xxx 从浏览器使用该服务时,我得到一个没有任何错误的空白页面.

The Problem
When I try to use the service from a browser using MyService.svc/MyMethod?MyParam=xxx, I get a blank page without any errors.

我的尝试
我已经在 web.config 中添加了一个 basicHttpBinding 并使它<行为配置中的 href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.servicemetadatabehavior.httpgetenabled.aspx" rel="noreferrer">HttpGetEnabled.我还在我的操作合同中添加了 [WebGet(UriTemplate = "MyMethod?MyParam={MyParam}")] 属性.

What I have tried
I have already added a basicHttpBinding to the web.config and made it HttpGetEnabled in the behavior configuration. I also added the [WebGet(UriTemplate = "MyMethod?MyParam={MyParam}")] attribute to my operation contract.

我已经关注了另一个堆栈溢出问题中的信息:
WCF 服务的 REST/SOAP 端点

I have already followed the information in this other stack overflow question:
REST / SOAP EndPoints for a WCF Service

但是,在执行这些步骤后,我要么得到一个空白页,要么得到一个 HTTP 404 错误.代码没有什么特别之处.我只是将一个字符串作为参数并返回Hello xxx".这是一个基本的Hello WCF World"概念验证类型的东西.


更新 - 这是相关代码

However, I either get a blank page or an HTTP 404 Error after following those steps. There's nothing special about the code. I am just taking in a string as a parameter and returning "Hello xxx". This is a basic "Hello WCF World" proof-of-concept type thing.


UPDATE - Here's the relevant code

[ServiceContract]
public interface IMyService
{
    [WebGet(UriTemplate = "MyMethod/MyParam={MyParam}")]
    [OperationContract]
    string MyMethod(string MyParam);
}

Web.Config - system.serviceModel 部分

<system.serviceModel>     
    <behaviors>
        <serviceBehaviors>          
            <behavior name="MyServiceBehavior">
              <serviceMetadata httpGetEnabled="true"  />
              <serviceDebug includeExceptionDetailInFaults="true"/>
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="MyServiceBehavior" name="MyService">
        <endpoint address="" 
                       binding="wsHttpBinding" contract="IMyService" />
        <endpoint address="MyService.svc" 
                       binding="basicHttpBinding"  contract="IMyService" />
        <endpoint address="mex" 
                       binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>    
</system.serviceModel>

推荐答案

查看你的 web.config serviceModel 部分,我可以看到你需要添加一个 webHttpBinding 并关联一个包含 webHttpGet.

Looking at your web.config serviceModel section, I can see that you need to add a webHttpBinding and associate an endPointBehavior that includes webHttpGet.

您的运营合同是正确的.以下是 system.serviceModel 配置部分的外观,以便您能够通过 GET HTTP 请求使用服务.

Your operation contract is correct. Here's how your system.serviceModel config section should look in order for you to be able to consume the service from a GET HTTP request.

<system.serviceModel>     
    <behaviors>
        <serviceBehaviors>
            <behavior name="MyServiceBehavior">
                <serviceMetadata httpGetEnabled="true"    />
                <serviceDebug includeExceptionDetailInFaults="true"/>          
            </behavior>
        </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>    
    <services>      
      <service behaviorConfiguration="MyServiceBehavior" name="MyService">
        <endpoint address="ws" binding="wsHttpBinding" contract="IMyService"/>
        <endpoint address="" behaviorConfiguration="WebBehavior"
                  binding="webHttpBinding"
                  contract="IMyService">
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>    
</system.serviceModel>

请务必为您的 wsHttpBinding 端点分配不同的地址,否则您将收到一条错误消息,指出您有两个端点正在侦听同一个 URI.

Be sure to assign a different address to your wsHttpBinding endpoint, otherwise you will get an error saying that you have two endpoints listening on the same URI.

另一种选择是将 wsHttpBinding 中的地址留空,但为 webHttpBinding 服务分配不同的地址.但是,这也会改变您的 GET 地址.

Another option is to leave the address blank in the wsHttpBinding, but assign a different address to the webHttpBinding service. However, that will change your GET address as well.

例如,如果您将地址分配为asmx",您将使用地址MyService.svc/asmx/MyMethod?MyParam=xxxx"调用您的服务.

For example, if you assign the address as "asmx", you would call your service with the address "MyService.svc/asmx/MyMethod?MyParam=xxxx".

这篇关于使用 GET 请求使 WCF Web 服务工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆