XmlSerializer.Serialize 需要很长时间...为什么?
给出 RssXmlHelper.cs 中 RssToolkit 中的以下代码:
/// <summary>
/// Returns XML of the Generic Type.
/// </summary>
/// <param name="rssDocument">The RSS document.</param>
/// <typeparam name="T">RssDocumentBase</typeparam>
/// <returns>string</returns>
public static string ToRssXml<T>(T rssDocument) where T : RssDocumentBase
{
if (rssDocument == null)
{
throw new ArgumentNullException("rssDocument");
}
using (StringWriter output = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(output, rssDocument);
return output.ToString();
}
}
当在我的 WCF 服务中调用 serializer.Serialize() 时,需要花费大量时间。
我将其放在从项目中调用的 WCF 服务中。我走进去,果然,这就是问题点。
然后我在我的解决方案中引用该项目,没有问题。
在 WCF 服务中使用它时,我应该采取不同的做法吗?
-- 更新 --
好的,所以我安装了适用于 Windows 7 和 .NET Framework 4 的 Microsoft Windows SDK,运行 sgne.exe RssToolkit.dll 并收到以下错误:
Error: An attempt was made to load an assembly with in incorrect format [path to rsstoolkit.dll]
- Could not load file or assemply [path to rsstoolkit.dll] or one of its dependencies. This assembly was build by a runtime newer than the currently loaded runtime and cannot be loaded.
我的 RssToolkit 项目的目标框架是设置为 4.0,匹配 Windows SDK for .Net 4。这不正确吗?
Given the following code from RssToolkit in RssXmlHelper.cs:
/// <summary>
/// Returns XML of the Generic Type.
/// </summary>
/// <param name="rssDocument">The RSS document.</param>
/// <typeparam name="T">RssDocumentBase</typeparam>
/// <returns>string</returns>
public static string ToRssXml<T>(T rssDocument) where T : RssDocumentBase
{
if (rssDocument == null)
{
throw new ArgumentNullException("rssDocument");
}
using (StringWriter output = new StringWriter(new StringBuilder(), CultureInfo.InvariantCulture))
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
serializer.Serialize(output, rssDocument);
return output.ToString();
}
}
When serializer.Serialize() is called in my WCF service, it takes a whole lot of time.
I have this sitting in a WCF service that I call from my project. I step into it, and sure enough, that's the problem point.
I then reference the project within my solution, and no problem.
Is there something I should be doing differently when using this in a WCF service?
-- UPDATE --
Ok, so I installed Microsoft Windows SDK for Windows 7 and .NET Framework 4, ran sgne.exe RssToolkit.dll and get the following error:
Error: An attempt was made to load an assembly with in incorrect format [path to rsstoolkit.dll]
- Could not load file or assemply [path to rsstoolkit.dll] or one of its dependencies. This assembly was build by a runtime newer than the currently loaded runtime and cannot be loaded.
The target framework for my RssToolkit project is set to 4.0, matching the Windows SDK for .Net 4. Is that not correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
sgen.exe< 生成序列化程序集/code>
,Windows SDK 附带的 XML 序列化器生成器工具。通过在大型课程中使用它,我看到了巨大的改进。
它基本上会生成知道如何序列化和反序列化域中的每个对象的代码。生成 DLL 后,您可以将其作为程序集引用包含在项目中,然后使用其中的类而不是您现在使用的
XmlSerializer
。You could generate serialization assemblies using
sgen.exe
, the XML Serializer Generator Tool that comes with the Windows SDK. I've seen drastic improvements by using it on large classes.It basically will generate code that knows how to serialize and deserialize every one of the objects you have in your domain. Once you have generated the DLL, you can include it in your project as an assembly reference, and then use the classes within it instead of the
XmlSerializer
you're using now.