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

我刚刚开始使用WCF,并在我的ASP中创建了一个新的web服务.NET项目.它创建了一个带有.svc扩展,比如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

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


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>

推荐答案

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

你的运营合同是正确的.以下是你的系统是如何工作的.serviceModel配置部分应该查看,以便您能够从GET HTTP请求中使用服务.

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

另一个选项是在wsHttpBinding中将地址留空,但为webHttpBinding服务分配不同的地址.然而,这也会改变你的GET地址.

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

Asp.net相关问答推荐

JWT 和 Google 在 Razor 页面中进行身份验证和授权

单个方法(即扩展方法)之间的调用不明确

在 Web.Config 的 Location Path 元素中指定多个目录

jQuery隐藏字段

asp.net、url 重写模块和 web.config

VS2013发布Web部署任务失败文件正在使用中

MvcBuildViews true 与 ASP.NET MVC 2 中的实体框架

返回 IHttpActionResult vs IEnumerable vs IQueryable

Web Api 参数始终为空

Razor 主机工厂错误

Eval()、XPath() 和 Bind() 等数据绑定方法只能在数据绑定控件的上下文中使用

在asp.net mvc 3中实现FilterAttribute,IActionFilter和从ActionFilterAttribute继承有什么区别?

协调 ASP.NET 脚本包和源映射

主机与 DnsSafeHost

使用 ConfigurationManager.RefreshSection 重新加载配置而不重新启动应用程序

Ashx 文件中的 HttpContext.Current.Session 为空

~/ 等价于 javascript

如何解决我的 ASP.Net MVC 应用程序中的 iisreset 后发生的 AntiForgeryToken 异常?

如何加密 web.config 中的一项

Sys.WebForms.PageRequestManagerServerErrorException:处理服务器上的请求时发生未知错误.