【发布时间】:2015-12-24 09:14:17
【问题描述】:
我需要一个通过 REST 提供 PDF 的 WCF 服务库。
例如我有一个这样的网址:localhost:8732/service1/reports/ok
我收到一份 PDF 作为回复。它是本地文件系统中的固定文件。
这是我当前的代码:
Service.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; }
}
}
}
App.config
<?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。 (请注意,它目前是我文件系统上的一个固定目录...)
问题是当我尝试发布或主机时。我遵循了几个关于在 IIS 中托管的教程,但没有成功。 我应该怎么做才能使它工作?
操作系统:Windows 8.1 .NET 框架 4
【问题讨论】:
-
当您在 IIS 上发布时,您到底遇到了什么错误/问题?
-
我收到 404 错误
-
请求的 URL 地址:localhost:8732/service1/reports/ok Ruta de acceso física:C:\inetpub\wwwroot\PDFprueba\service1\reports\ok
-
好的..我会尝试在我的设备上做..然后更新你
-
你能看到
inetmgr发布的网站吗?