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;
}
}
}
我需要一个大的缓冲区大小,因为我无法启用流式传输. (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 使用Asp.Net Identity 2在AspNetUserClaims中存储用户信息有
- asp.net-mvc – 在OnActionExecuting事件中更改模型
- ASP.NET,jQuery,脏窗体和window.onbeforeunload
- asp.net – 无效的回发或回调参数.为什么?
- asp.net – ASP:ItemTemplate中的DropDownList:为什么允许
- asp.net-web-api – 从ExceptionLogger引用操作参数
- asp.net-mvc – 从单个Web服务器迁移到多个Web服务器负载平
- 如何使用ASP.NET MVC实现站点而不使用Visual Studio?
- asp.net-mvc – Knockout JS发送到MVC 3
- 单元在ASP.NET中测试登录
- asp.net-mvc – 如何在asp.net mvc中的关系表/模
- asp.net-mvc-3 – 在MVC3中使用Html.LabelFor的表
- asp默认文档不能在IIS7上运行
- .net – 为什么事件处理程序只能在IHttpModule初
- asp.net-mvc – 在服务器上安装ASP.NET MVC 4
- asp.net-mvc – 使用OWIN的Google身份验证Oauth在
- asp.net – 如何将javascript文件的服务限制为仅
- 单元测试 – 使用MOQ对象进行ASP.NET MVC单元测试
- asp.net-mvc – Rotativa和Bootstrap网格样式
- 重命名ASP.NET_SessionId
