无法在 IIS 中正确托管 WCF 服务。错误404

Mr_*_*Mac 2 c# asp.net iis wcf

我需要一个通过 REST 提供 PDF 的 WCF 服务库。例如,我有一个像这样的网址:localhost:8732/service1/reports/ok 我收到一个 PDF 作为响应。它是本地文件系统中的固定文件。这是我当前的代码:

服务.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfJsonRestService
{

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    [ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
    public class Service1 : IService1
    {

        public Stream GetReport(string value)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
            FileStream f = new FileStream("C:\\invoice.pdf", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while ((count = f.Read(buffer, sum, length - sum)) > 0)
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }

    }
}
Run Code Online (Sandbox Code Playgroud)

IService.cs

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfJsonRestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        // TODO: Add your service operations here

        [OperationContract(Action = "*")]
        [WebInvoke(Method = "GET", //Este metodo si esta en POST, te dice que metodo no permitido
            UriTemplate = "reports/{value}")]
        Stream GetReport(string value);
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    // You can add XSD files into the project. After building the project, you can directly use the data types defined there, with the namespace "WcfJsonRestService.ContractType".
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

应用程序配置

<?xml version="1.0" encoding="utf-8" ?>

<configuration>
  <system.serviceModel>
    <services>
      <service name="WcfJsonRestService.Service1">
        <endpoint address="http://localhost:8732/service1"
              binding="webHttpBinding"
              contract="WcfJsonRestService.IService1"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>
  <startup>
    <supportedRuntime version="v4.1" sku=".NETFramework,Version=v4.1"/>
  </startup>
</configuration>
Run Code Online (Sandbox Code Playgroud)

请不要注意那个误导性的名称“WcfJsonRestService”,该名称稍后会更改为更合适的名称......

当我在 Visual Studio 2013 中运行此程序时,一切正常(除了来自 Microsoft WCF 服务主机的警告,未找到任何服务元数据)。当我访问http://localhost:8732/service1/somerandomstring时,浏览器将打开 pdf。(请注意,目前它是我的文件系统上的固定目录......)

问题是当我尝试发布或托管时。我遵循了几个有关在 IIS 中托管的教程,但没有成功。我应该如何做才能使其发挥作用?

操作系统:Windows 8.1 .NET Framework 4

Dar*_*eal 5

几天前我偶然发现了这个问题:这是由于缺少处理此类调用所需的处理程序映射。有多种方法可以解决该问题,例如手动执行ServiceModelReg.exe控制台命令。我在下面提出的解决方法比较复杂,但它的优点是可以在不更改 Web 服务器的默认行为的情况下解决特定问题,从而避免潜在的副作用。

  • 打开用于机器管理的服务器管理器界面,通常出现在任务栏开始菜单中。
  • 转到仪表板并选择添加角色或功能以打开向导。
  • 选择基于角色或基于功能的安装类型以及您想要使用的服务器,即您的本地/本地服务器。
  • 转到“功能”部分:在那里,展开.NET Framework 3.5 功能节点和/或.NET Framework 3.5 功能节点,具体取决于您安装的内容:如果两者都有,则应执行以下步骤两次(对于每个其中之一)。
  • 展开 WCF 服务部分(如果可用),然后选择HTTP 激活(请参见下面的屏幕截图)。
  • 继续操作直至完成向导,然后单击“安装”

在此输入图像描述

安装完成后,您应该能够运行 WCF 服务,而不会再次出现 404 错误。

有关此特定问题以及如何解决该问题的更多信息,您还可以阅读我博客上的这篇文章