我有要求我想通過REST提供PDF的WCF服務庫。 例如我有這樣的網址:localhost:8732/service1/reports/ok
而且我得到一個PDF作爲迴應。它是本地文件系統中的一個固定文件。 這是我當前的代碼:無法在IIS中正確託管WCF服務。錯誤404
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地址:\t的http://本地主機:8732 /服務1 /報告/ OK 魯塔德accesofísica:\t C:\的Inetpub \ wwwroot的\ PDFprueba \服務1 \ \報告確定 –