JSON을 사용하여 WCF REST 서비스를 호출하는 오류. 길이 할당량 (8192)이 초과되었습니다
-
20-09-2019 - |
문제
.NET 4 RC를 사용하여 IIS에서 호스팅 된 WCF REST 서비스가 있습니다. 서비스에 대한 게시물 호출은 JSON을 사용하여 직렬화됩니다. Datamember (String) 중 하나의 크기가 8k보다 길어질 때까지 모든 것이 잘 작동합니다. 이 경우 MaxStringContentLength가 초과되었음을 나타내는 아래에 설명 된 오류를받습니다. 엔드 포인트의 MaxStringContentLength 속성이 증가했으며 구성 파일에서 올바르게 읽습니다.
웹 구성은 다음과 같습니다.
<services>
<service name="MyServiceServer" >
<endpoint address="http://localhost/MyService" kind="webHttpEndpoint" endpointConfiguration="serviceEndPoint" contract="IMyService">
</endpoint>
</service>
</services>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="serviceEndPoint" maxReceivedMessageSize="2048000" maxBufferSize="2048000" maxBufferPoolSize="0">
<readerQuotas maxStringContentLength="2048000" maxArrayLength="2048000" maxDepth ="65000"/>
<security mode="None">
<transport clientCredentialType="None"/>
</security>
</standardEndpoint>
</webHttpEndpoint>
</standardEndpoints>
ImyService 인터페이스는 다음과 같이 정의됩니다.
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/request", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
void MyMehod(<Class Type> obj);
}
완전한 오류 메시지는 다음과 같습니다.
“서버는 요청을 처리하는 오류가 발생했습니다. 예외 메시지는 '유형의 객체를 실시하는 오류가있었습니다. XML 데이터를 읽는 동안 최대 문자열 내용 길이 할당량 (8192)이 초과되었습니다. 이 할당량은 XML 리더를 생성 할 때 사용되는 xmlDictionaryReaderQuotas 객체에서 MaxStringContentLength 속성을 변경하여 증가 할 수 있습니다. ' 자세한 내용은 서버 로그를 참조하십시오. 예외 스택 추적은 : at System.Runtime.serialization.xmlobjectserializer.xmlobjectserializer.ReadObJecThandleExceptions (XMLREADERDELEGATOR Reader, boolean verifyObjectName, dataConTractresolver dataconTresolver)에서 system.runtimeation.json.json.json.smeriOloger.serializer.SoneRialozer.SoneRialozer.SoneRialozer.SoneRialOzer.SONEREALOIGER dispatcher.singlebodyparametermessageformatter.deserializeRequest (메시지 메시지, 객체 [] 매개 변수) system.servicemodel.dispatcher.demultiplexingdispatchmessageformatter.deserializeRequest (system.severicemodel.dispatter.dispatcher.uitempatcher.uitempatcher.uitempatcher. system.servicemodel.dispatcher.dispatchoperationruntime.deserializeinputs (messagerpc & rpc)에서 system.servicemodel.dispatcher.dispatchoperationRuntime.invokebegin (messagerpc)에서 systempather.servicodel.dispatcher.servicodemel.dispatcher. cemodel.dispatcher.immutabledispatchruntime.processmessage31 (messagerpc & rpc)의 system.servicemodel.dispatcher.messagerpc.process (boolean isoperationcontextset)”
해결책
이 작업은 엔드 포인트 주소로 완전한 절대 URL을 사용해야합니다. 교활하고 상대 경로를 사용하려고 시도하거나 .SVC를 생략하면 요청이 너무 커지면 이상한 독자 할당량 오류로 폭탄이됩니다.
나는 이것을 WCF의 버그로 제출할 것이다.
- 상대 URL은 허용되지 않아야합니다 (및 적절한 예외는 발생)
또는
- 독자 할당량은 상대 경로와도 작동해야합니다.
다른 팁
web.config에 삽입 :
<configuration>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="webHttpBindingConfig">
<readerQuotas maxStringContentLength="2048000" />
</binding>
</webHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
및 Endpoint BindingConfiguration = "webhttpbindingconfig"삽입 엔드 포인트에 삽입
비슷한 문제가 있었지만 .NET 3.5도있었습니다
서버 로그에 문제가 없었으므로 클라이언트에 문제가있었습니다.최대 값이 증가한 구성을 읽고 사용하지 않은 것 같습니다....
그래서 나는 다른 오버로드를 사용하여 webchannelfactory의 생성자에서 엔드 포인트의 이름을 명시 적으로 전달했습니다.
였다:
WebChannelFactory<IWKRestTest> factory = new WebChannelFactory<IWKRestTest>(new Uri(XXX));
factory.Credentials.UserName.UserName = K_USERNAME;
factory.Credentials.UserName.Password = K_PASSWORD;
IWKRestTest proxy = factory.CreateChannel();
이다:
WebChannelFactory<IWKRestTest> factory = new WebChannelFactory<IWKRestTest>("IWKRestTestService");
그리고 app.config에는 다음이 있습니다.
URI는 엔드 포인트 노드에 표시되지만 BindingConfiguration 등도 찾을 수 있으므로 이제 모든 새로운 증가 된 한계가 작동합니다.