增加消息的最大大小

上次修改时间: 2011年9月1日

适用范围: SharePoint Foundation 2010

在 SharePoint Online 中提供

当您通过客户端对象模型发出大型请求时,您可能会因不正确的请求而收到一个错误。如果出现此情况,您可以使用服务器对象模型来增加 Windows Communication Foundation (WCF) 服务 Client.svc(支持 SharePoint Foundation 客户端对象模型)允许的最大消息大小。另一个选择是,使用分布式创作和版本控制 (DAV) 发出 PUT 请求。

该服务器对象模型提供两个用于增加最大消息大小的属性。可以在 SPWcfServiceSettings 类上设置 MaxReceivedMessageSize 属性,也可以在 SPClientRequestServiceSettings 类上设置 MaxReceivedMessageSize 属性。可以按下列方式之一通过当前 SPWebService 对象访问这些属性:SPWebService.ContentService.ClientRequestServiceSettings 或 SPWebService.ContentService.WcfServiceSettings["Client.svc"]。根据 SPClientRequestServiceSettings 对象的属性值,SharePoint Foundation 将决定是否使用 SPWcfServiceSettings 对象的属性,如下所示:

  • 如果 SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize 大于 0,则 SharePoint Foundation 将此值用作属性设置。

  • 如果 SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize 等于 0,则 SharePoint Foundation 将默认值用作设置。

  • 如果 SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize 等于 -1,则 SharePoint Foundation 将 SPWcfServiceSettings 值(如果已定义该值)用作设置;但如果未定义该值,则 SharePoint Foundation 将 64KB 用作设置。

增加 WCF MaxReceivedMessageSize

以下代码段说明了如何使用 SPClientRequestServiceSettings 对象的 MaxReceivedMessageSize 属性来修改消息大小设置。

Public Shared Sub IncreaseMaxReceivedMessageSize()
    Dim contentService As SPWebService = SPWebService.ContentService
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = 10485760  ' 10MB
    contentService.Update()
End Sub
public static void IncreaseMaxReceivedMessageSize()
{        
    SPWebService contentService = SPWebService.ContentService;
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = 10485760;  // 10MB
    contentService.Update();
}

下一个代码段说明如何使用 SPWcfServiceSettings 对象的 MaxReceivedMessageSize 属性来修改设置。此示例必须先在 SPClientRequestServiceSettings 上将此属性设置为 -1。

Public Shared Sub IncreaseMaxReceivedMessageSize()
    Dim contentService As SPWebService = SPWebService.ContentService
    
    ' Must set this to -1, else, the MaxReceivedMessageSize value for
    ' SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1
    
    ' SPWcfServiceSettings has other Properties that you can set.
    Dim csomWcfSettings As New SPWcfServiceSettings()
    csomWcfSettings.MaxReceivedMessageSize = 10485760  ' 10MB
    contentService.WcfServiceSettings("client.svc") = csomWcfSettings
    
    contentService.Update()
End Sub
public static void IncreaseMaxReceivedMessageSize ()
{
    SPWebService contentService = SPWebService.ContentService;

    /* Must set this to -1, else, the MaxReceivedMessageSize value for
    SPWebService.ContentService.WcfServiceSettings["client.svc"] will not be used.*/
    contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = -1;

    // SPWcfServiceSettings has other Properties that you can set.
    SPWcfServiceSettings csomWcfSettings = new SPWcfServiceSettings();
    csomWcfSettings.MaxReceivedMessageSize = 10485760; // 10MB
    contentService.WcfServiceSettings["client.svc"] = csomWcfSettings;

    contentService.Update();
}

使用 DAV 发出请求

下面的示例说明如何使用 DAV 发出请求。

WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp)

Dim request As HttpWebRequest = DirectCast(WebRequestCreator.ClientHttp.Create(New Uri("https://Server/MyFile.txt")), HttpWebRequest)
request.Method = "PUT"

' Make an asynchronous call for the request stream. The callback method will be called on a background thread. 
Dim asyncResult As IAsyncResult = request.BeginGetRequestStream(New AsyncCallback(RequestStreamCallback), request)

Private Sub RequestStreamCallback(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = TryCast(ar.AsyncState, HttpWebRequest)
    Dim requestStream As Stream = request.EndGetRequestStream(ar)
    Dim streamWriter As New StreamWriter(requestStream)
    
    ' Write your file here.
    streamWriter.Write("Hello World!")
    
    ' Close the stream.
    streamWriter.Close()
    
    ' Make an asynchronous call for the response. The callback method will be called on a background thread. 
    
    request.BeginGetResponse(New AsyncCallback(ResponseCallback), request)
End Sub

Private Sub ResponseCallback(ByVal ar As IAsyncResult)
    Dim request As HttpWebRequest = TryCast(ar.AsyncState, HttpWebRequest)
    Dim response As WebResponse = Nothing
    Try
        response = request.EndGetResponse(ar)
    Catch generatedExceptionName As WebException
    Catch generatedExceptionName As SecurityException
        
        ' You may need to analyze the response to see if it succeeded.
    End Try
End Sub
WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp);

HttpWebRequest request = (HttpWebRequest)WebRequestCreator.ClientHttp.Create(new Uri("https://Server/MyFile.txt"));
request.Method = "PUT";

/* Make an asynchronous call for the request stream. The callback method will be called on a background thread. */ 
IAsyncResult asyncResult = request.BeginGetRequestStream(new AsyncCallback(RequestStreamCallback), request);

private void RequestStreamCallback(IAsyncResult ar)
{
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;            
    Stream requestStream = request.EndGetRequestStream(ar);
    StreamWriter streamWriter = new StreamWriter(requestStream);

    // Write your file here.
    streamWriter.Write("Hello World!");            

    // Close the stream.
    streamWriter.Close();

    /* Make an asynchronous call for the response. The callback method will be called on a background thread. */
    request.BeginGetResponse(new AsyncCallback(ResponseCallback), request);
}       

private void ResponseCallback(IAsyncResult ar)
{
    HttpWebRequest request = ar.AsyncState as HttpWebRequest;
    WebResponse response = null;
    try
    {
        response = request.EndGetResponse(ar);
    }
    catch (WebException)
    {}
    catch (SecurityException)
    {}

    // You may need to analyze the response to see if it succeeded.
}

在 Silverlight 应用程序中,您可能还需要设置客户端访问策略 XML 文件,如下面的示例中所示。可将此文件置于 %inetpub%\wwwroot\wss\VirtualDirectories\80 中。

<?xml version="1.0" encoding="utf-8"?>
<access-policy>
  <cross-___domain-access>
    <!--Enables Silverlight 3 all methods functionality-->
    <policy>
      <allow-from http-methods="*">"
        <___domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
    <!--Enables Silverlight 2 clients to continue to work normally -->
    <policy>
      <allow-from >
        <___domain uri="*"/>
      </allow-from>
      <grant-to>
        <resource path="/" include-subpaths="true"/>
      </grant-to>
    </policy>
  </cross-___domain-access>
</access-policy>

有关 Silverlight 和 HTTP 通信的详细信息,请参阅 HTTP Communication and Security with Silverlight

请参阅

概念

数据检索概述

客户端对象模型准则

常见编程任务

其他资源

使用 SharePoint Foundation 2010 托管客户端对象模型

客户端对象模型资源中心(该链接可能指向英文页面)