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
几天前我偶然发现了这个问题:这是由于缺少处理此类调用所需的处理程序映射。有多种方法可以解决该问题,例如手动执行ServiceModelReg.exe
控制台命令。我在下面提出的解决方法比较复杂,但它的优点是可以在不更改 Web 服务器的默认行为的情况下解决特定问题,从而避免潜在的副作用。
安装完成后,您应该能够运行 WCF 服务,而不会再次出现 404 错误。
有关此特定问题以及如何解决该问题的更多信息,您还可以阅读我博客上的这篇文章。
归档时间: |
|
查看次数: |
5965 次 |
最近记录: |