最大メッセージ サイズの増加

最終更新日: 2011年9月1日

適用対象: SharePoint Foundation 2010

SharePoint Online で使用可能

クライアント オブジェクト モデルを使用して大規模な要求を行う場合、不適切な要求としてエラーが表示される場合があります。この場合、サーバー オブジェクト モデルを使用して、SharePoint Foundation のクライアント オブジェクト モデルをサポートする Windows Communication Foundation (WCF) サービス (Client.svc) で許容される最大メッセージ サイズを増加できます。また、Distributed Authoring and Versioning (DAV) を使用して、PUT 要求を行うこともできます。

サーバー オブジェクト モデルでは、最大メッセージサイズを増加するための 2 つのプロパティが提供されます。SPWcfServiceSettings クラスの MaxReceivedMessageSize プロパティを設定するか、SPClientRequestServiceSettings クラスの MaxReceivedMessageSize プロパティを設定します。これらのプロパティには、現在の SPWebService オブジェクトで、SPWebService.ContentService.ClientRequestServiceSettings または SPWebService.ContentService.WcfServiceSettings["Client.svc"] を使用してアクセスできます。SPClientRequestServiceSettings オブジェクトのプロパティの値によって、SharePoint Foundation は、以下のように、SPWcfServiceSettings オブジェクトのプロパティを使用します (または使用しません)。

  • SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize がゼロより大きい場合、SharePoint Foundation はこの値をプロパティ設定として使用します。

  • SPWebService.ContentService.ClientRequestServiceSettings.MaxReceivedMessageSize がゼロに等しい場合、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 のマネージ クライアント オブジェクト モデルの使用

Client Object Model Resource Center (英語)