简体   繁体   中英

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.

Im trying to upload with the WebClient class.

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. 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

<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. Ensure that the type corresponding to the XML is added to the known types collection of the service.

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. And not sure why you're encoding with xml....if the string isn't xml.

See

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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