从 WCF 服务返回 XmlDocument 不起作用

发布于 2024-12-28 08:08:08 字数 132 浏览 1 评论 0原文

我正在尝试更新一些返回字符串以返回 XmlDocument 对象的 WCF 服务方法。我尝试按原样返回它并将其封装在数据契约对象中。不管怎样,我在尝试更新服务引用时遇到了错误。该错误建议将其封装在带有我正在执行的操作合约的数据合约中。这有什么技巧吗?

I am trying to update some WCF service methods that return strings to return XmlDocument objects. I've tried returning it as-is and encapsulating it in a datacontract object. Either way I'm hitting an error upon attempting to update the service reference. The error suggest encapsulating it in a datacontract with an operations contract which I am doing. Is there a trick to this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ぽ尐不点ル 2025-01-04 08:08:08

有一种方法可以从 WCF 返回 XmlDocument,但您需要使用 XmlSerializer 而不是默认序列化程序 (DataContractSerialier) - 下面的代码显示了如何完成它。话虽如此,请考虑使用注释中提到的数据传输对象,除非您的方案确实需要传输 XmlDocument。

public class StackOverflow_8951319
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
        [OperationContract, XmlSerializerFormat]
        XmlDocument GetDocument();
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }

        public XmlDocument GetDocument()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"<products>
  <product id='1'>
    <name>Bread</name>
  </product>
  <product id='2'>
    <name>Milk</name>
  </product>
  <product id='3'>
    <name>Coffee</name>
  </product>
</products>");
            return doc;
        }
    }
    static Binding GetBinding()
    {
        var result = new WSHttpBinding(SecurityMode.None);
        //Change binding settings here
        return result;
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));

        Console.WriteLine(proxy.GetDocument().OuterXml);

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}

There's a way to return a XmlDocument from WCF, but you need to use the XmlSerializer instead of the default serializer (DataContractSerialier) - the code below shows how it can be done. Having said that, do consider using data transfer objects as mentioned in the comments, unless your scenario really requires a XmlDocument to be transferred.

public class StackOverflow_8951319
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]
        string Echo(string text);
        [OperationContract, XmlSerializerFormat]
        XmlDocument GetDocument();
    }
    public class Service : ITest
    {
        public string Echo(string text)
        {
            return text;
        }

        public XmlDocument GetDocument()
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(@"<products>
  <product id='1'>
    <name>Bread</name>
  </product>
  <product id='2'>
    <name>Milk</name>
  </product>
  <product id='3'>
    <name>Coffee</name>
  </product>
</products>");
            return doc;
        }
    }
    static Binding GetBinding()
    {
        var result = new WSHttpBinding(SecurityMode.None);
        //Change binding settings here
        return result;
    }
    public static void Test()
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
        host.AddServiceEndpoint(typeof(ITest), GetBinding(), "");
        host.Open();
        Console.WriteLine("Host opened");

        ChannelFactory<ITest> factory = new ChannelFactory<ITest>(GetBinding(), new EndpointAddress(baseAddress));
        ITest proxy = factory.CreateChannel();
        Console.WriteLine(proxy.Echo("Hello"));

        Console.WriteLine(proxy.GetDocument().OuterXml);

        ((IClientChannel)proxy).Close();
        factory.Close();

        Console.Write("Press ENTER to close the host");
        Console.ReadLine();
        host.Close();
    }
}
扛起拖把扫天下 2025-01-04 08:08:08

如果您希望能够在线传递任意 XML,最好的方法是使用 XElement 而不是 XmlDocument

XmlDocument 不可序列化

If you want to be able to pass arbitrary XML on the wire the best way to do it is to use XElement rather than XmlDocument

XmlDocument isn't serializable

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文