WCF 3.5の問題(パラメータがストリームの場合はWebHTTPBindingの代わりに割り当てられたCustomBinding)

StackOverflow https://stackoverflow.com//questions/21031159

質問

私のサービス契約では、パラメータとして 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実装からインスタンス化されている)を実行すると、webhttpbindingがcustombindingではなく、Not 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のMaxReceivedMessagesSizeの設定やバインディングを保証する方法については、WebHTTPBindingに設定されます。

p.S。私はこれをプログラム的に行う必要があるので、.configの設定は私には使用されません。

役に立ちましたか?

解決

大丈夫これで長く掘り下げた後、これは私が思い付いたものです:

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;
      }
    }
  }
}
.

それは間違いなく改善することができますが機能します。ケースをタイプ別に分離するのではなく、これを処理するより一般的な方法があるかどうかを確認する必要があります。現時点では、その他の方法が表示されていませんが(バインディングの種類はサービス自体のメソッドに基づいているため、Streamパラメータを1つのサービスメソッドに追加すると、WebHTTPBindingからCustomBindingに送信するだけです。)

  • 誰かがこのトピックに関する他の洞察を持っているならば、投稿してください。
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top