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

标签 c# web-services wcf iis iis-express

我在练习 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.

我的 Web 配置具有用于元数据交换的端点,并且行为也具有 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 激活后。现在我遇到了不同的错误。 我无法理解。 enter image description here

最佳答案

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

一旦为 IIS 启用了 WCF,您应该能够毫无问题地运行所有内容。

关于c# - WCF 服务在 iis express 下运行时运行完美,但在本地 IIS 及其虚拟目录下运行时相同服务将无法工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37502895/

相关文章:

c# - 使用 "Run..."创建不同的 user.config

java - 在 Java 中发出带有 header 的 http get 请求

wcf - WCF和传统的ASP.NET Web有什么区别

visual-studio - 如何在 Visual Studio 2017 中更新 OData WCF?

c# - 转换为十进制并执行 OrderBy

c# - 空闲动画不会在 keyup 上触发

c# - 从 ASP.NET 批量打印 PDF

Java Web Service 进行 JPA-MySql 插入,但插入后不进行选择

ios - 是否可以在 Heroku 上为 Facebook 应用程序运行一个进程?

c# - 如何模拟 WCF 服务中的数据层?