gpt4 book ai didi

c# - 无法在 IIS 中正确承载 WCF 服务。错误 404

转载 作者:行者123 更新时间:2023-12-02 17:31:22 34 4
gpt4 key购买 nike

我有一个要求,我想要一个通过 REST 提供 PDF 的 WCF 服务库。例如我有一个这样的 url: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);
}

}
}

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

应用程序配置

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

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

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

问题是当我尝试 publish 或 host 时。我遵循了几个关于在 IIS 中托管的教程,但没有成功。我应该怎么做才能让它发挥作用?

操作系统:Windows 8.1.NET 框架 4

最佳答案

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

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

enter image description here

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

有关此特定问题及其解决方法的更多信息,您还可以 read this post在我的博客上。

关于c# - 无法在 IIS 中正确承载 WCF 服务。错误 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32800794/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com