公开 wcf 服务元数据

Exposing wcf service metadata

我正在学习有关设置 wcf 服务的教程。我完成了它所说的更改,直到它第一次说要启动服务。在我的 Web 浏览器中导航到端点时,我收到 404 或 403.14 错误。最初该服务显示文件夹结构,但我删除了启用该功能的 web.config 文件中的属性。我确定问题与该文件有关,但我不确定还需要更改什么。 Link 给指南:http://www.c-sharpcorner.com/UploadFile/dhananjaycoder/four-steps-to-create-first-wcf-service-for-beginners/

Web.config 文件:

<?xml version="1.0"?>
<configuration>

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5.2"/>
    <httpRuntime targetFramework="4.5.2"/>
    <httpModules>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"/>
    </httpModules>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="WcfCalculator.Calculator" behaviorConfiguration="Metadata">
        <endpoint address="" contract="WcfCalculator.ICalculator" binding="basicHttpBinding"/>
        <endpoint name="mex" address="mex" contract="IMetadataExchange" binding="mexHttpBinding"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="Metadata">
          <serviceMetadata httpGetEnabled="true"/>
        </behavior>
        <behavior>
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
        <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>    
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true">
      <remove name="ApplicationInsightsWebTracking"/>
      <add name="ApplicationInsightsWebTracking" type="Microsoft.ApplicationInsights.Web.ApplicationInsightsHttpModule, Microsoft.AI.Web"
        preCondition="managedHandler"/>
    </modules>
    <!--
        To browse web app root directory during debugging, set the value below to true.
        Set to false before deployment to avoid disclosing web app folder information.
      -->
    <validation validateIntegratedModeConfiguration="false"/>
  </system.webServer>

</configuration>

服务合同:

using System.ServiceModel;

namespace WcfCalculator
{
    [ServiceContract]
    public interface ICalculator
    {
        [OperationContract]
        double AddNumbers(double number1, double number2);
        [OperationContract]
        double SubstractNumbers(double number1, double number2);
        [OperationContract]
        double MultiplyNumbers(double number1, double number2);
        [OperationContract]
        double DivisionNumbers(double number1, double number2);
    }
}

我认为403.14可能与您的文件夹权限有关。您的应用程序池 运行 是什么帐户,.NET Framework 是什么版本?确保您的应用程序池以 Framework 4.0 为目标。 403.14 - 很可能您正在尝试浏览根目录,而目录浏览在 IIS 中被禁用,或者您的应用程序池没有足够的权限。然后,尝试删除 protocolMapping。

VS2015中可以设置.svc文件为启动文件。点击 "F5" 将在 WCF 测试客户端 (WcfTestClient.exe) 中打开该文档。或者,您可以右键单击 .svc 文件并 select "View in Browser".