当 运行 在 iis express 下时,WCF 服务运行完美,但当 运行 在本地 IIS 下及其虚拟目录下时,相同的服务将无法工作

WCF Service runs perfectly when running under iis express but same service wont work when running under Local IIS with its virtual directory

我在练习WCF,卡在了这个情况。 我的 Wcf 服务在 IISExpress 下完美运行(VS2013 的默认设置)。 但是当我在本地 IIS 下使用它时,我在 WCF 测试客户端中遇到以下异常。

Error: Cannot obtain Metadata from http://localhost/WcfService1/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address. For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error URI: http://localhost/WcfService1/Service1.svc Metadata contains a reference that cannot be resolved: 'http://localhost/WcfService1/Service1.svc'. The remote server returned an unexpected response: (405) Method Not Allowed. The remote server returned an error: (405) Method Not Allowed.HTTP GET Error URI: http://localhost/WcfService1/Service1.svc There was an error downloading 'http://localhost/WcfService1/Service1.svc'. The request failed with HTTP status 404: Not Found.

我的网络配置具有用于元数据交换的端点,并且行为具有 httpGetEnabled true。

参见web.config:

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

  <system.diagnostics>
    <sources>
      <source name="System.ServiceModel.MessageLogging" switchValue="Warning,ActivityTracing">
        <listeners>
          <add type="System.Diagnostics.DefaultTraceListener" name="Default">
            <filter type="" />
          </add>
          <add name="ServiceModelMessageLoggingListener">
            <filter type="" />
          </add>
        </listeners>
      </source>
    </sources>
    <sharedListeners>
      <add initializeData="f:\tfs\wcfservice1\wcfservice1\web_messages.svclog"
        type="System.Diagnostics.XmlWriterTraceListener, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
        name="ServiceModelMessageLoggingListener" traceOutputOptions="Timestamp">
        <filter type="" />
      </add>
    </sharedListeners>
    <trace autoflush="true" />
  </system.diagnostics>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5"/>
  </system.web>
  <system.serviceModel>
    <diagnostics>
      <messageLogging logEntireMessage="true" logMalformedMessages="true"
        logMessagesAtTransportLevel="true" />
    </diagnostics>
    <services>
      <service behaviorConfiguration="behave" name="WcfService1.Service1">
        <endpoint address="WcfService1.Service1" binding="basicHttpBinding" name="basicHttp"
          contract="WcfService1.IService1" />
        <endpoint binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost/WcfService1/" />
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="behave">
          <serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
          <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"/>
    <!--
        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.
      -->
    <directoryBrowse enabled="true"/>
  </system.webServer>

</configuration>

我检查了其他 WCF 问题。但找不到答案。 提前致谢。

当我使用本地 IIS 时,我可以点击 url http://localhost/wcfservice1/ 它显示了该目录中的所有文件。 但是当我点击 Service1.svc 时,它显示 IIS 错误页面。

HTTP Error 404.3 - Not Found The page you are requesting cannot be served because of the extension configuration. If the page is a script, add a handler. If the file should be downloaded, add a MIME map.

在 IIS 中添加 WCF 激活后。现在我遇到了不同的错误。 我听不懂。

您需要在 IIS 管理器中为您的站点启用 WCF,请参阅:https://blogs.msdn.microsoft.com/blambert/2009/02/13/how-to-enable-iis7asp-net-and-wcf-http-activation/

为 IIS 启用 WCF 后,您应该能够运行一切都没有问题。

我解决了问题。 我已经打开 .Net framework 4.6 高级服务 > WCF 服务 > HTTP 激活。 现在 WCF 服务运行良好。

感谢 gmiley.. 你之前的回答让我明白了。