在我的服务合同中,我公开了一个将Stream作为参数的方法:
[OperationContract]
[WebInvoke(UriTemplate = "/Upload?fileName={fileName}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
Stream Upload(String fileName, Stream fileData);
当我这样做时,我的自定义 WebServiceHost 实现(从自定义 WebServiceHostFactory 实现实例化)将端点绑定视为 CustomBinding 而不是 WebHttpBinding:
public class myWebServiceHost : WebServiceHost {
.
.
.
protected override void OnOpening() {
base.OnOpening();
if (base.Description != null) {
foreach (ServiceEndpoint endpoint in base.Description.Endpoints) {
WebHttpBinding binding = endpoint.Binding as WebHttpBinding;
if (binding != null) { //Will always be null given the operation contract above
Int32 MB = 1048576 * Convert.ToInt32(ConfigurationManager.AppSettings["requestSizeMax"]);
binding.MaxReceivedMessageSize = MB;
binding.TransferMode = TransferMode.Streamed;
}
}
}
}
}
这是一个问题,因为我需要上传大于 64KB 的文件......关于如何为 CustomBinding 设置 MaxReceivedMessageSize 或如何确保我的 Binding 设置为 WebHttpBinding 的任何想法。
PS 我需要以编程方式执行此操作,因此 .config 设置对我没有任何用处。