繁体   English   中英

在WCF服务中接收大字符串

[英]Receiving a large string in WCF service

以下是我的服务合同

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

该字符串将始终超过3000个字符,因此我无法在网址中传递该字符串。

我正在尝试使用WebClient类上传。

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

当我尝试使用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.

我假设是因为期望它是XML,所以我将字符串包装成XML

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

但后来我得到了错误

无法使用DataContractSerializer反序列化具有根名称'string'和根名称空间''(对于操作'SavePrint'和合同('IPrintAPI',' http: //tempuri.org/')的XML主体)。 确保将与XML对应的类型添加到服务的已知类型集合中。

我不知道我应该如何将字符串添加到已知类型集合。

包装"<string></string>"不是编码xml的正确方法。 并且不确定为什么要使用xml编码。...如果字符串不是xml。

看到

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

不管....尝试

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);
}

附加:

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

附加:

现在尝试增加您的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