WCF WebServiceHostFactory MaxReceivedMessageSize配置
我在我的VS2008解决方案中有一个名为“Palladium”的RESTful WCF Web服务作为项目.它通过名为“Palladium.svc”的页面使用WebServiceHostFactory实现托管在ASP.Net 3.5 Web应用程序中.
我的服务工作方式类似于here解释的方式,服务可以接收POST以及URITemplate中定义的其他参数. 该服务运作良好,我可以收到发布的信息并使用它.
由于该服务是通过WebServiceHostFactory实现托管的,因此该服务已为工厂设置了默认绑定.我试图通过在web.config文件中提供绑定设置和端点来覆盖这些绑定.但是,当我这样做时,我收到一条错误消息:
LogStartingDetails是我在RESTful服务中调用的方法. 显然,LogStartingDetails方法不需要具有类型为Stream的单个参数,因为当工厂为我创建绑定时,服务响应良好(或者更重要的是,它不需要具有单个参数当工厂为我工作时). 在经过大量研究并打了几个砖墙后,我决定创建自己的类,该类继承自WebServiceHostFactory并覆盖一些实现,以便在绑定上指定MaxReceivedMessageSize属性. 以下是我的服务代码示例.如果有人能帮助我弄清楚我在这里做错了什么,我将不胜感激. Palladium.svc(托管在ASP.Net Web应用程序中) <%@ ServiceHost Language="C#" Debug="true" Service="CDS.PalladiumService.Palladium" Factory="CDS.PalladiumService.MyWebServiceHostFactory" %> MyWebServiceHostFactory.cs(在CDS.PalladiumService项目中) using System; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Channels; using System.ServiceModel.Web; namespace CDS.PalladiumService { public class MyServiceHost : WebServiceHost { public MyServiceHost() { } public MyServiceHost(object singletonInstance,params Uri[] baseAddresses) : base(singletonInstance,baseAddresses) { } public MyServiceHost(Type serviceType,params Uri[] baseAddresses) : base(serviceType,baseAddresses) { } protected override void OnOpening() { base.OnOpening(); if (base.Description != null) { foreach (var endpoint in base.Description.Endpoints) { var transport = endpoint.Binding.CreateBindingElements().Find<TransportBindingElement>(); if (transport != null) { transport.MaxReceivedMessageSize = 5242880; transport.MaxBufferPoolSize = 5242880; } } } } } class MyWebServiceHostFactory : WebServiceHostFactory { protected override ServiceHost CreateServiceHost(Type serviceType,Uri[] baseAddresses) { return new MyServiceHost(serviceType,baseAddresses); } } } IPalladium.cs(在CDS.PalladiumService项目中) using System.IO; using System.ServiceModel; using System.ServiceModel.Web; namespace CDS.PalladiumService { // NOTE: If you change the interface name "IPalladium" here,you must also update the reference to "IPalladium" in Web.config. [ServiceContract] public interface IPalladium { [OperationContract] [WebInvoke(Method = "POST",UriTemplate = UriTemplate.LogStartingDetails)] void LogStartingDetails(string truckId,string palladiumId,Stream postData); } } Palladium.cs(在CDS.PalladiumService项目中) using System.IO; using System.ServiceModel.Activation; namespace CDS.PalladiumService { // NOTE: If you change the class name "Palladium" here,you must also update the reference to "Palladium" in Web.config. [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class Palladium : IPalladium { public void LogStartingDetails(string truckId,Stream postData) { string contents = string.Empty; using (var reader = new StreamReader(postData)) { contents = reader.ReadToEnd(); } StreamWriter sw1 = File.AppendText(@"C:log.txt"); sw1.WriteLine(contents); sw1.WriteLine(""); sw1.Close(); return; } } } URITemplate.cs(在CDS.PalladiumService项目中) namespace CDS.PalladiumService { public static class UriTemplate { public const string LogStartingDetails = "/log-starting/{truckId}/{palladiumId}"; } } 解决方法
我设法通过在我的WebServiceHost的OnOpening方法中的端点绑定上设置以下3个设置来实现此功能:
protected override void OnOpening() { base.OnOpening(); foreach (var endpoint in Description.Endpoints) { var binding = endpoint.Binding as WebHttpBinding; if (binding != null) { const int fiveMegaBytes = 5242880; binding.MaxReceivedMessageSize = fiveMegaBytes; binding.MaxBufferSize = fiveMegaBytes; binding.MaxBufferPoolSize = fiveMegaBytes; } } } 我需要一个大的缓冲区大小,因为我无法启用流式传输. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- dotnetcore3.1 WPF 中使用依赖注入
- asp.net – 用于检索大量二进制图像的.ashx处理程序的性能
- asp.net-mvc – 使ASP.MVC2/VS2010应用程序在IIS 7.5中工作
- asp.net-core – 实体框架核心以编程方式应用迁移
- nuget-package – MvcScaffolding是否通过命令行与VS 2013
- asp.net – 使用Visual Studio 2015检测gulp中的发布/调试
- asp.net – 在asp:Button中包装文本
- asp.net – 处理缓存和浏览器后退按钮的最佳方法是什么?
- asp.net-mvc-3 – 如何在MVC3中键入时过滤占用
- asp.net-mvc – 如何使用razor语法转换为对象
- asp.net-mvc-4 – 是否可以将MVC Razor视图保存到
- asp.net-mvc – 在ASP.NET Core 1.1中配置基本身
- ASP.NET MVC中使用Bundle打包压缩js和css的方法
- asp.net-mvc – 为什么我们使用ViewModels?
- asp.net – app_offline.htm,停止IIS站点和停止应
- asp.net – 如何防止开放重定向攻击?
- asp.net – 实体框架“等待操作超时”就简单的De
- asp.net – AngularJs 2与ASP.NET 4.5.1
- asp.net-mvc – ASP.NET MVC Beta授权属性发送给
- asp.net – 如何设置MVC应用程序的默认页面?