首页 > 解决方案 > 如何在 IIS 上托管的 wcf 服务中使用 http 请求访问任何方法?

问题描述

我有一个 WCF 服务应用程序。我想通过 Web 浏览器上的 http 请求访问此服务。

当我在 IISExpress 上运行 WCF 服务时,它可以工作,但是当我在 IIS 上发布服务时,返回 404 错误。我该如何解决这个问题?

IIS Express 屏幕截图

IIS 屏幕截图

IService.cs

namespace Protek.WebService
{
    [ServiceContract]
    public interface IService
    {
        [WebGet(UriTemplate = "HelloWorld")]
        [OperationContract]
        string HelloWorld();
    }
}

服务.svc

<%@ ServiceHost Language="C#" Debug="true" Service="Protek.WebService.Service" CodeBehind="Service.svc.cs" %>

服务.svc.cs

 namespace Protek.WebService
 {
    public class Service : IService
    {
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
 }

网页配置

  <system.serviceModel>

    <behaviors>

      <serviceBehaviors>
        <behavior name="DefaultBehavior">
          <serviceMetadata httpGetEnabled="true"    />
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

      <endpointBehaviors>
        <behavior name="WebBehavior">
          <webHttp />
        </behavior>
      </endpointBehaviors>

    </behaviors>
    <services>

      <service behaviorConfiguration="DefaultBehavior" name="Protek.WebService.Service">
        <endpoint address="ws" binding="wsHttpBinding" contract="Protek.WebService.IService"/>
        <endpoint address="" binding="webHttpBinding" contract="Protek.WebService.IService" behaviorConfiguration="WebBehavior"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:5858/Service" />
          </baseAddresses>
        </host>
      </service>

    </services>

  </system.serviceModel>

标签: .nethttpwcfiis

解决方案


首先,stackoverflow 是基于英文的技术问答论坛,请在描述和截图中使用英文。
如果要发布 WCF REST 服务,请参考下面的回复。
如何使用 WCF 服务?
而且,当我们在IIS中托管WCF服务时,基地址是由IIS提供的,不需要在你的配置文件中添加基地址。
https://social.msdn.microsoft.com/Forums/vstudio/en-US/d42fc165-052d-476a-9580-1240b3d0293d/specify-endpoint-in-wcf-webconfig?forum=wcf
如果有,请 随时告诉我有什么我可以帮忙的。


推荐阅读