簡體   English   中英

如何在IIS托管的WCF服務中使用HTTP請求訪問任何方法?

[英]How to access any method using http request in wcf serice which is hosted on IIS?

我有一個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();
    }
}

Service.svc

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

Service.svc.cs

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

Web.config文件

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

首先,stackoverflow是基於英語的技術性問答論壇,請在描述和屏幕截圖中使用英語。
如果要發布WCF休息服務,請參考以下答復。
如何使用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
請隨時告訴我是否有什么我可以幫助的。

我通過將此行添加到服務Web.config文件中解決了該問題。 我的http錯誤是404.3

<system.webServer>
    <handlers>
       <add
          name="svc-Integrated"
          path="*.svc"
          verb="*"
          type="System.ServiceModel.Activation.HttpHandler, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
     </handlers>
</system.webServer>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM