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

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

问题描述

背景

我在过去创建ASMX Web服务,并已经能够从网络浏览器访问服务和Ajax GET使用地址约定请求:​​
MyService.asmx /参数的MyMethod = XXX

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

现状

我能够消耗使用 WcfTestClient 随VS2008的服务。我还可以通过添加另一个项目的服务引用或使用 svcutil.exe的命令行来生成代理和配置文件创建自己的WCF客户端。

问题

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

我试图

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

我已经遵循了本其他堆栈溢出问题的信息:

REST / SOAP端点WCF服务

不过,我要么得到一个空白页或HTTP 404错误以下这些步骤后。没有什么特别之处code。我只是以一个字符串作为参数,并返回你好XXX。这是一个基本的Hello WCF世界证明了概念式的东西。



更新 - 以下是相关code

  [的ServiceContract]
公共接口IMyService
{
    [WebGet(UriTemplate =的MyMethod / MyParam = {} MyParam)]
    [OperationContract的]
    字符串的MyMethod(字符串MyParam);
}

的Web.Config - system.serviceModel栏目

 &LT; system.serviceModel&GT;
    &LT;&行为GT;
    &LT; serviceBehaviors&GT;
    &LT;行为NAME =MyServiceBehavior&GT;
    &LT; serviceMetadata httpGetEnabled =真/&GT;
    &LT; serviceDebug includeExceptionDetailInFaults =真/&GT;
    &LT; /行为&GT;
    &LT; / serviceBehaviors&GT;
    &LT; /行为&GT;
    &LT;服务和GT;
      &LT;服务behaviorConfiguration =MyServiceBehaviorNAME =则将MyService&GT;
    &LT;端点地址=
                       绑定=的wsHttpBinding合同=IMyService/&GT;
    &LT;端点地址=MyService.svc
                       绑定=basicHttpBinding的合同=IMyService/&GT;
    &LT;端点地址=MEX
                       绑定=mexHttpBinding合同=IMetadataExchange接口/&GT;
      &LT; /服务&GT;
    &LT; /服务&GT;
&LT; /system.serviceModel>


解决方案

看你的的web.config serviceModel节中,我可以看到,你需要添加一个<一个HREF =htt​​p://msdn.microsoft.com/en-us/library/bb412176.aspx>的WebHttpBinding 以及包括的 webHttpGet

您经营合同是正确的。这是你的system.serviceModel配置部分应该是什么样子,为了让您能够使用来自一个GET HTTP请求的服务。

 &LT; system.serviceModel&GT;
    &LT;&行为GT;
    &LT; serviceBehaviors&GT;
    &LT;行为NAME =MyServiceBehavior&GT;
    &LT; serviceMetadata httpGetEnabled =真/&GT;
    &LT; serviceDebug includeExceptionDetailInFaults =真/&GT;
    &LT; /行为&GT;
    &LT; / serviceBehaviors&GT;
      &LT; endpointBehaviors&GT;
    &LT;行为NAME =WebBehavior&GT;
    &LT; webHttp /&GT;
    &LT; /行为&GT;
      &LT; / endpointBehaviors&GT;
    &LT; /行为&GT;
    &LT;服务和GT;
      &LT;服务behaviorConfiguration =MyServiceBehaviorNAME =则将MyService&GT;
    &LT;端点地址=WS绑定=的wsHttpBinding合同=IMyService/&GT;
    &LT;端点地址=behaviorConfiguration =WebBehavior
    绑定=的WebHttpBinding
    合同=IMyService&GT;
    &LT; /端点&GT;
    &LT;端点地址=MEX绑定=mexHttpBinding合同=IMetadataExchange接口/&GT;
      &LT; /服务&GT;
    &LT; /服务&GT;
&LT; /system.serviceModel>

请务必到不同的地址分配给您的wsHttpBinding终点,否则你会得到一个错误,说你有两个端点监听同一URI。

另一种选择是留在的wsHttpBinding地址空白,但是分配不同的地址给的WebHttpBinding服务。然而,这将改变你的GET地址为好。

例如,如果您指定的地址为ASMX,你会打电话与地址您服务 MyService.svc / ASMX /的MyMethod?MyParam = XXXX

Background
I have created ASMX web services in the past and have been able to access the service from the web browser and Ajax GET requests using the address convention:
MyService.asmx/MyMethod?Param=xxx

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.

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.

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.

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.

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

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 Section

<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>

解决方案

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

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>

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.

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.

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天全站免登陆