能否为WCF使用的XmlWriter配置默认设置?
在序列化数据时,是否有任何方法可以使用DataContractSerializer配置WCF服务使用的默认XmlWriter 使用DataContractSerializer的现成WCF服务正在丢失新行(\r\n) [编辑:很抱歉造成混淆。开箱即用的WCF不会丢失新行。] 我能够让XmlWriter将新行编码为能否为WCF使用的XmlWriter配置默认设置?,wcf,xmlwriter,Wcf,Xmlwriter,在序列化数据时,是否有任何方法可以使用DataContractSerializer配置WCF服务使用的默认XmlWriter 使用DataContractSerializer的现成WCF服务正在丢失新行(\r\n) [编辑:很抱歉造成混淆。开箱即用的WCF不会丢失新行。] 我能够让XmlWriter将新行编码为 使用XmlWriterSettings(NewLineHandling.Entitize),但我想让WCF在序列化对象时以同样的方式运行 public string Seriali
代码>使用XmlWriterSettings(NewLineHandling.Entitize),但我想让WCF在序列化对象时以同样的方式运行
public string Serialize<T>(T object)
{
var serializer = new DataContractSerializer(typeof(T));
using (var stringWriter = new StringWriter())
{
var settings = new XmlWriterSettings { NewLineHandling = NewLineHandling.Entitize };
using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
{
serializer.WriteObject(xmlWriter, object);
string xml = stringWriter.ToString();
return xml;
}
}
}
公共字符串序列化(T对象)
{
var serializer=新的DataContractSerializer(typeof(T));
使用(var stringWriter=new stringWriter())
{
var settings=newxmlwritersettings{NewLineHandling=NewLineHandling.Entitize};
使用(var xmlWriter=xmlWriter.Create(stringWriter,设置))
{
serializer.WriteObject(xmlWriter,object);
string xml=stringWriter.ToString();
返回xml;
}
}
}
如果要使用不同的XmlWriter
,则需要使用自定义消息编码器。上的示例演示了如何编写一个
但我从未见过WCF丢失\r\n
字符-它正确地将\r
实体化为&XD代码>,至少在我检查的所有时间。当我运行下面的代码时,它显示正确返回的字符:
public class StackOverflow_12205872
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
string str = proxy.Echo("Hello\r\nworld");
Console.WriteLine(str);
Console.WriteLine(str[5] == '\r' && str[6] == '\n');
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
公共类StackOverflow_12205872
{
[服务合同]
公共接口测试
{
[经营合同]
字符串回显(字符串文本);
}
公共类服务:ITest
{
公共字符串回显(字符串文本)
{
返回文本;
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
AddServiceEndpoint(typeof(ITest),new BasicHttpBinding(),“”);
host.Open();
Console.WriteLine(“主机已打开”);
ChannelFactory工厂=新的ChannelFactory(新的BasicHttpBinding(),新的EndpointAddress(baseAddress));
ITest proxy=factory.CreateChannel();
string str=proxy.Echo(“Hello\r\nworld”);
控制台写入线(str);
Console.WriteLine(str[5]='\r'&&str[6]='\n');
((IClientChannel)代理).Close();
工厂关闭();
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}
Fiddler显示正在发送此请求:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header></s:Header><s:Body><Echo xmlns="http://tempuri.org/"><text>Hello
world</text></Echo></s:Body></s:Envelope>
你好
;
世界
响应还包含实体化的CR字符。您能否分享有关配置(包括绑定)的更多详细信息?如果您想使用不同的XmlWriter
,则需要使用自定义消息编码器。上的示例演示了如何编写一个
但我从未见过WCF丢失\r\n
字符-它正确地将\r
实体化为&XD代码>,至少在我检查的所有时间。当我运行下面的代码时,它显示正确返回的字符:
public class StackOverflow_12205872
{
[ServiceContract]
public interface ITest
{
[OperationContract]
string Echo(string text);
}
public class Service : ITest
{
public string Echo(string text)
{
return text;
}
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), "");
host.Open();
Console.WriteLine("Host opened");
ChannelFactory<ITest> factory = new ChannelFactory<ITest>(new BasicHttpBinding(), new EndpointAddress(baseAddress));
ITest proxy = factory.CreateChannel();
string str = proxy.Echo("Hello\r\nworld");
Console.WriteLine(str);
Console.WriteLine(str[5] == '\r' && str[6] == '\n');
((IClientChannel)proxy).Close();
factory.Close();
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
公共类StackOverflow_12205872
{
[服务合同]
公共接口测试
{
[经营合同]
字符串回显(字符串文本);
}
公共类服务:ITest
{
公共字符串回显(字符串文本)
{
返回文本;
}
}
公共静态无效测试()
{
string baseAddress=“http://“+Environment.MachineName+”:8000/服务”;
ServiceHost主机=新ServiceHost(类型(服务),新Uri(基地址));
AddServiceEndpoint(typeof(ITest),new BasicHttpBinding(),“”);
host.Open();
Console.WriteLine(“主机已打开”);
ChannelFactory工厂=新的ChannelFactory(新的BasicHttpBinding(),新的EndpointAddress(baseAddress));
ITest proxy=factory.CreateChannel();
string str=proxy.Echo(“Hello\r\nworld”);
控制台写入线(str);
Console.WriteLine(str[5]='\r'&&str[6]='\n');
((IClientChannel)代理).Close();
工厂关闭();
控制台。写入(“按ENTER键关闭主机”);
Console.ReadLine();
host.Close();
}
}
Fiddler显示正在发送此请求:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Header></s:Header><s:Body><Echo xmlns="http://tempuri.org/"><text>Hello
world</text></Echo></s:Body></s:Envelope>
你好
;
世界
响应还包含实体化的CR字符。您能否分享有关配置(包括绑定)的更多详细信息