简体   繁体   English

在WCF服务中接收大字符串

[英]Receiving a large string in WCF service

Below is my service contract 以下是我的服务合同

[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "/SavePrint?seq={seq}", BodyStyle = WebMessageBodyStyle.Bare)]
bool SavePrint(int seq, string print_lines);

The string will always be over 3000 characters so i cannot pass it in the Url. 该字符串将始终超过3000个字符,因此我无法在网址中传递该字符串。

Im trying to upload with the WebClient class. 我正在尝试使用WebClient类上传。

WebClient client = new WebClient();
client.Headers["Content-type"] = "application/xml";
client.UploadString(baseURLTxtBox.Text + "/SavePrint?seq=1", LongStringHere);

When i try and pass it using the UploadString method of the WebClient class the service fails with the error There was an error checking start element of object of type System.String. The data at the root level is invalid. Line 1, position 1. 当我尝试使用WebClient类的UploadString方法传递它时,该服务失败, There was an error checking start element of object of type System.String. The data at the root level is invalid. Line 1, position 1. There was an error checking start element of object of type System.String. The data at the root level is invalid. Line 1, position 1.

I assumed because it is expected it as XML so i wrapped my string in XML 我假设是因为期望它是XML,所以我将字符串包装成XML

<string xmlns="">LongStringHere</string>

but then i get the error 但后来我得到了错误

Unable to deserialize XML body with root name 'string' and root namespace '' (for operation 'SavePrint' and contract ('IPrintAPI', ' http://tempuri.org/ ')) using DataContractSerializer. 无法使用DataContractSerializer反序列化具有根名称'string'和根名称空间''(对于操作'SavePrint'和合同('IPrintAPI',' http: //tempuri.org/')的XML主体)。 Ensure that the type corresponding to the XML is added to the known types collection of the service. 确保将与XML对应的类型添加到服务的已知类型集合中。

I dont know how i am supposed to add a string to the known types collection. 我不知道我应该如何将字符串添加到已知类型集合。

Wrapping "<string></string>" is not the proper way to encode xml. 包装"<string></string>"不是编码xml的正确方法。 And not sure why you're encoding with xml....if the string isn't xml. 并且不确定为什么要使用xml编码。...如果字符串不是xml。

See 看到

http://www.w3.org/TR/html401/interact/forms.html#form-content-type http://www.w3.org/TR/html401/interact/forms.html#form-content-type

Regardless....try 不管....尝试

string url = baseURLTxtBox.Text;

using(WebClient client = new WebClient())
{
    System.Collections.Specialized.NameValueCollection nvc = new System.Collections.Specialized.NameValueCollection();
    nvc.Add("ParameterOne", "!@#$%^&*() Anything Any Characters Here");
    nvc.Add("ParameterTwo", LongString);
    byte[] responsebytes = client.UploadValues(url, "POST", nvc);
    string responsebody = Encoding.UTF8.GetString(responsebytes);
}

APPEND: 附加:

 [OperationContract]
    [WebInvoke(Method = "POST", UriTemplate = "Mod?ParameterOne={ParameterOne}&ParameterTwo={ParameterTwo}")]
    bool SavePrint(string ParameterOne, string ParameterTwo);

APPEND: 附加:

Now try increasing your maxReceivedMessageSize. 现在尝试增加您的maxReceivedMessageSize。

<endpoint
          address  = "http://somethinghere"
    binding  = "basicHttpBinding" bindingConfiguration="BasicHttpEndpointBinding"
    contract = "MyIService" >
</endpoint>

  <basicHttpBinding>
<binding name="BasicHttpEndpointBinding"
   maxBufferSize="9000000"
   maxReceivedMessageSize="9000000"                      >
  <security mode = "None">
  </security>
</binding>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM