我不完全确定如何问这个,所以我做了一个SSCCE
我有这个简单的 WCF 服务
[ServiceContract]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class EmailService
{
[WebInvoke(UriTemplate = "/SendEmail", Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Xml)]
public bool SendEmail(EmailData data)
{
try
{
byte[] fileBinaryContents = Convert.FromBase64String(data.Enc64FileContents);
File.WriteAllBytes(data.FileName, fileBinaryContents);
return true;
}
catch (Exception)
{
return false;
}
}
}
[DataContract(Namespace = "http://somenamespace/")]
public class EmailData
{
[DataMember]
public string FileName { get; set; }
[DataMember]
public string EmailAddress { get; set; }
[DataMember]
public string Enc64FileContents { get; set; }
}
我正在尝试让 Winforms 客户端调用 webservice 方法;这就是我所拥有的
string URI = " http://localhost:59961/EmailService/SendEmail";
string fileContents = Convert.ToBase64String(File.ReadAllBytes("test.txt"));
EmailData emailData = new EmailData
{
EmailAddress = "foo@bar.com",
Enc64FileContents = fileContents,
FileName = "test.txt"
};
XNamespace ns = "http://somenamespace/";
XElement emailDataElement = new XElement(ns + "EmailData");
emailDataElement.Add(new XElement(ns + "FileName", emailData.FileName));
emailDataElement.Add(new XElement(ns + "Enc64FileContents", emailData.Enc64FileContents));
emailDataElement.Add(new XElement(ns + "EmailAddress", emailData.EmailAddress));
var xml = emailDataElement.ToString(SaveOptions.DisableFormatting);
using (WebClient wc = new WebClient())
{
wc.Headers[HttpRequestHeader.ContentType] = "application/xml; charset=utf-8";
string response = wc.UploadString(URI, "POST", xml);
}
现在在服务端,一些属性为空,如下面的屏幕截图所示。
为什么它FileName
具有正确的价值而其他人没有?