gpt4 book ai didi

c# - 使用 WCF Restful 服务的 PDF

转载 作者:太空宇宙 更新时间:2023-11-03 11:34:09 25 4
gpt4 key购买 nike

任务:当用户单击链接时能够呈现 PDF(这是一个 WCF 服务{示例:http://localhost:6186/MyReportServices.svc/reports/012})。该服务从 SSRS 服务器获取报告并返回流。这是 中的一段代码。

[ServiceBehavior(AddressFilterMode = AddressFilterMode.Any)]
public class AssistReportServices : IAssistReportServices
{
public Stream GetReport(int value)
{
//skipped some lines of code.
try
{
result = rs.Render(format, devInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);
execInfo = rs.GetExecutionInfo();
}
catch (SoapException err)
{
throw;
}
MemoryStream ms = new MemoryStream();
ms.Write(result, 0, result.Length);
ms.Position = 0;
//WebOperationContext.Current.OutgoingResponse.ContentType = ConfigurationManager.AppSettings["ResponseType"];
WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
return ms;
}
}

我的操作合约如下所示:

[OperationContract(Action = "*")]
[WebInvoke(Method = "POST",
UriTemplate = "reports/{value}")]
Stream GetReport(int value);

到目前为止一切顺利。但问题是...当我单击上面的链接时,我在标题为 Adob​​e Reader 和消息的对话框中收到以下错误:

File does not begin with '%PDF-'.

错误图片:http://i.imgur.com/A4J68.png

我可以将内存流保存到一个文件中并手动打开 pdf,它可以正常打开,没有任何问题。

谢谢。

最佳答案

我发现我哪里出错了。我曾使用 Web.Config 文件来配置我的 WCF 服务。当我使用以下代码切换到基于代码的配置时,该服务工作正常并指出了我的错误。

static void Main(string[] args)
{

ServiceHost serviceHost = new ServiceHost(typeof(MyReportingServices.MyReportServices), ServiceEndpointUri);
WebHttpBinding binding = new WebHttpBinding();
ServiceEndpoint sep = serviceHost.AddServiceEndpoint(typeof(MyReportingServices.IMyReportServices), binding, string.Empty);
sep.Behaviors.Add(new WebHttpBehavior());
serviceHost.Open();
Console.WriteLine("Service is running @ " + ServiceEndpointUri);
Console.WriteLine();
Console.WriteLine("Press enter to shutdown service.");
Console.ReadLine();
serviceHost.Close();
}

错误是:

Operation 'GetReport' in contract 'IMyReportServices' has a path variable named 'value' which does not have type 'string'. Variables for UriTemplate path segments must have type 'string'.

我通过将操作参数的数据类型更改为字符串来解决这个问题。

感谢 Mrchief,我使用他的输入添加了以下代码行,以帮助我显示保存/取消对话框:-

WebOperationContext.Current.OutgoingResponse.ContentType = "application/pdf";
WebOperationContext.Current.OutgoingResponse.Headers.Add("Content-disposition", "inline; filename=apurvReport.pdf");

关于c# - 使用 WCF Restful 服务的 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6819639/

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