【问题标题】:How to use ServiceRoutes while defining maxReceivedMessageSize for non-custom bindings in WCF如何在 WCF 中为非自定义绑定定义 maxReceivedMessageSize 时使用 ServiceRoutes
【发布时间】:2011-10-03 17:41:13
【问题描述】:

编辑此内容以重新关注实际问题。我保留了消息底部的原始问题,但更改了标题和内容以反映实际发生的情况。

我需要为通过 ServiceRoute 机制添加到 MVC3 项目的 WCF 服务覆盖 maxReceivedMessageSize。在 web.config 中指定绑定不起作用。如何做到这一点。


最初的问题在这条线以下,但基于我看到的大量误报而具有误导性。

您好,我使用了一些示例将文件流上传服务添加到我的 MVC3 项目中。如果我使用默认绑定(即未在 web.config 中定义),只要我不超过 64k 默认大小,该服务就可以工作。当我尝试定义自己的绑定以增加大小时,我的跟踪中出现内容类型不匹配,响应中出现 HTTP415 Unsupported Media Type。我正在尝试通过 HTTP 通过 fiddler 调用它,并且没有使用 WCF 客户端。 这是跟踪中的错误:

Content Type image/jpeg was sent to a service expecting multipart/related;type="application/xop+xml".  The client and service bindings may be mismatched.

这里是 web.config 服务模型部分

 <system.serviceModel>
<behaviors>
  <endpointBehaviors>
    <behavior name="NewBehavior0" />
  </endpointBehaviors>
</behaviors>
<services>
  <service name="AvyProViewer.FileService">
    <endpoint address="UploadFile" binding="basicHttpBinding" bindingConfiguration=""
      contract="AvyProViewer.FileService"  />
  </service>
</services>

<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
<bindings>
  <basicHttpBinding>
    <binding name="NewBinding0" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647"
      messageEncoding="Mtom" transferMode="StreamedRequest">
      <readerQuotas maxDepth="2147483647" maxStringContentLength="2147483647"
        maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
    </binding>
  </basicHttpBinding>
</bindings>

这里是服务:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class FileService
{      
    [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "UploadFile")]       
    public string UploadFile(Stream fileStream)       
    {
        string path = HostingEnvironment.MapPath("~");  
        string fileName = Guid.NewGuid().ToString() + ".jpg";
        FileStream fileToupload = new FileStream(path + "\\FileUpload\\" + fileName, FileMode.Create);

        byte[] bytearray = new byte[10000];
        int bytesRead, totalBytesRead = 0;
        do
        {
            bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
            totalBytesRead += bytesRead;
        } while (bytesRead > 0);

        fileToupload.Write(bytearray, 0, bytearray.Length);
        fileToupload.Close();
        fileToupload.Dispose();
        return fileName;
    }
}

这是我在 MVC3 路由中公开它的地方:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.Add(new ServiceRoute("FileService", new WebServiceHostFactory(), typeof(FileService)));
        . . .
}

【问题讨论】:

    标签: wcf asp.net-mvc-3


    【解决方案1】:

    我认为问题在于您的绑定中 messageEncodingmtom 声明。尝试将 messageEncoding 更改为 Text

    【讨论】:

    • 我也试过了,虽然在跟踪错误中使用 text 而不是 mtom,但它给出了相同的错误。
    • 天啊!在您的 endpoint 元素中,bindingConfiguration 是一个空字符串。请添加绑定元素的名称。
    • 好消息,我稍后会检查它,因为我不在电脑旁。
    • 不幸的是,这似乎不是问题。还有其他想法吗?
    • 我开始认为答案是,如果您使用的是 ServiceRoute,它不会使用 web.config 中包含的绑定。如果是这样的话,我会发布一个不同的问题并在此处跟进。
    【解决方案2】:

    答案最终是三个不同堆栈溢出帖子的组合。没有一个人自己解决了这个问题,但每个人都提供了关于正在发生的事情的重要线索。

    1. 似乎如果添加 ServiceRoute,web.config 绑定信息将被忽略。这篇 SO 帖子让我了解了这个函数似乎没有记录的行为:Unable to set maxReceivedMessageSize through web.config

    2. 然后我使用这篇文章来确定如何以编程方式覆盖绑定的 maxreceivedmesssagesize:Specifying a WCF binding when using ServiceRoute

    3. 不幸的是,代码表单 #2 不能开箱即用(不确定 ServiceRoute 的绑定行为是否已更改或有何不同)。事实证明,如果您指定一个 ServiceRoute ,它会自动创建为一个 CustomBinding ,它不能转换为 #2 中使用的 WebHTTPBinding 类型。所以这篇文章:How to set the MaxReceivedMessageSize programatically when using a WCF Client? 帮助我确定了如何更改 #2 中的代码以将此功能添加到自定义绑定。

    【讨论】:

      猜你喜欢
      • 2019-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多