Parameters WCF 3.5问题(当参数为流时,分配CustomBinding而不是WebHttpBinding)

Parameters WCF 3.5问题(当参数为流时,分配CustomBinding而不是WebHttpBinding),parameters,stream,webhttpbinding,maxreceivedmessagesize,Parameters,Stream,Webhttpbinding,Maxreceivedmessagesize,在我的服务合同中,我公开了一个将流作为参数的方法: [OperationContract] [WebInvoke(UriTemplate = "/Upload?fileName={fileName}", Method = "POST", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] Stream Upload

在我的服务合同中,我公开了一个将流作为参数的方法:

[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的文件。。。关于如何设置自定义绑定的MaxReceivedMessageSize或如何确保绑定设置为WebHttpBinding的任何想法


另外,我需要以编程的方式完成这项工作,因此.config设置对我没有任何用处。

好的,在对这项工作进行了长时间的深入研究之后,我得出了以下结论:

protectedoverride void OnOpening(){
base.OnOpening();
if(base.Description!=null){
foreach(base.Description.Endpoints中的ServiceEndpoint端点){
Int32 MB=1048576*Convert.ToInt32(ConfigurationManager.AppSettings[“requestSizeMax]”);
开关(endpoint.Binding.GetType().ToString()){
案例“System.ServiceModel.Channel.CustomBinding”:
CustomBinding custom=(CustomBinding)endpoint.Binding;
custom.Elements.Find().MaxReceivedMessageSize=MB;
custom.Elements.Find().TransferMode=TransferMode.Streamed;
打破
案例“System.ServiceModel.WebHttpBinding”:
WebHttpBinding webhttp=(WebHttpBinding)endpoint.Binding;
webhttp.MaxReceivedMessageSize=MB;
webhttp.TransferMode=TransferMode.Streamed;
打破
}
}
}
}
它是有效的,尽管它肯定可以改进。我仍然需要看看是否有一种更通用的方法来处理这个问题,而不是按类型来区分案例。尽管目前我没有看到任何其他方法可以做到这一点(因为绑定类型基于服务本身中的方法,请记住,当我向其中一个服务方法添加流参数时,它只是从WebHttpBinding切换到CustomBinding)

  • 如果有人对这个话题有任何其他见解,请发表
protected override void OnOpening() {
  base.OnOpening();
  if (base.Description != null) {
    foreach (ServiceEndpoint endpoint in base.Description.Endpoints) {
      Int32 MB = 1048576 * Convert.ToInt32(ConfigurationManager.AppSettings["requestSizeMax"]);
      switch(endpoint.Binding.GetType().ToString()) {
        case "System.ServiceModel.Channels.CustomBinding":
          CustomBinding custom = (CustomBinding)endpoint.Binding;
          custom.Elements.Find<HttpTransportBindingElement>().MaxReceivedMessageSize = MB;
          custom.Elements.Find<HttpTransportBindingElement>().TransferMode = TransferMode.Streamed;
        break;
        case "System.ServiceModel.WebHttpBinding":
          WebHttpBinding webhttp = (WebHttpBinding)endpoint.Binding;
          webhttp.MaxReceivedMessageSize = MB;
          webhttp.TransferMode = TransferMode.Streamed;
        break;
      }
    }
  }
}