来自 .NET C# 客户端的 Java Web 服务 - 命名空间故障?

发布于 2024-10-19 10:48:15 字数 1330 浏览 2 评论 0原文

我们已经使用 Spring-WS 构建了一个 Web 服务,并尝试从 C# .NET 客户端访问它。该服务在我们从 SoapUI 和其他 Java 客户端完成的所有测试中工作正常,但在 .NET 中陷入困境。

看来这是命名空间的问题。

例如,当仅在信封标记中声明名称空间并且没有元素包含名称空间或前缀时,此请求有效:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns="http://mycompany.org/schemas">
<soapenv:Header/>
<soapenv:Body>
  <authenticationRequest>
    <username>user</username>
    <password>password</password>
</authenticationRequest>
</soapenv:Body>
</soapenv:Envelope>

但是,这个看似等效的请求不起作用:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://mycompany.org/schemas">
<soapenv:Header/>
<soapenv:Body>
  <authenticationRequest xmlns="http://mycompany.org/schemas">
    <username>user</username>
    <password>password</password>
</authenticationRequest>
</soapenv:Body>
</soapenv:Envelope>

请注意,在这种情况下,名称空间再次在身份验证请求元素。 给出的错误是请求未根据 XML 模式进行验证,抱怨“用户名”元素未定义。

不幸的是,当在 .NET 中添加服务引用(以及 Web 引用)时,wsdl.exe 生成的代码总是在第二种情况下创建请求。

请有人解释一下为什么这两段 XML 不等效,以及我们如何在 C# 客户端中...
- 删除第二个命名空间声明?
- 或者为请求中的每个元素添加命名空间前缀?
- 或者添加一个没有前缀的命名空间声明到每个元素?

我们已经尝试了几个小时:(

谢谢!

We've built a webservice with Spring-WS, and are trying to access it from a C# .NET client. the service works OK with all the tests we've done from SoapUI and other java clients, but gets stuck in .NET.

It seems this is a problem with namespaces.

For example, this request works, when the namespace is declared only in the envelope tag, and no element has namespace or prefix in it:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"     xmlns="http://mycompany.org/schemas">
<soapenv:Header/>
<soapenv:Body>
  <authenticationRequest>
    <username>user</username>
    <password>password</password>
</authenticationRequest>
</soapenv:Body>
</soapenv:Envelope>

However, this seemingly equivalent request does NOT work:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://mycompany.org/schemas">
<soapenv:Header/>
<soapenv:Body>
  <authenticationRequest xmlns="http://mycompany.org/schemas">
    <username>user</username>
    <password>password</password>
</authenticationRequest>
</soapenv:Body>
</soapenv:Envelope>

Notice that in this case, the namespace is declared again in authenticationRequest element.
The error given is that the request does not validate against the XML schema, complaining that the 'username' element is not defined.

Unfortunately, when adding a service reference (and also a web reference) in .NET, the code generated by wsdl.exe always creates the request in the second case.

Could someone, please, explain why these two pieces of XML are not equivalent, and how we could, in the C# client...
- remove the second namespace declaration?
- or add a namespace prefix to every element in the request?
- or add a namespace declaration without prefix to each element?

We've been trying for hours :(

Thanks!

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

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

发布评论

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

评论(1

被你宠の有点坏 2024-10-26 10:48:16

我在从 .NET 客户端访问 Java WS 时也遇到过类似的问题。我还没有找到解决该问题的优雅方法,但我确实通过使用 SoapExtension 更改传出/传入 SOAP 消息来解决它:

  1. 实现 SoapExtensionAttribute,如下所示:

    类 SoapExtAttribute :SoapExtensionAttribute
    {
        int 优先级 = 0;
    
        公共覆盖类型 ExtensionType
        {
            获取 { 返回 typeof(SoapExt); }
        }
    
        公共覆盖 int 优先级
        {
            得到
            {
                返回优先级;
            }
            放
            {
                优先级=值;
            }
        }
    }
    
  2. 实现 SoapExtension,如下所示:

    类 SoapExt :SoapExtension
    {
        私有流 mWireStream = null;
        私有流 mApplicationStream = null;
    
        公共覆盖对象 GetInitializer(Type serviceType)
        {
            返回服务类型;
        }
    
        公共覆盖对象 GetInitializer(LogicalMethodInfo methodInfo,SoapExtensionAttribute 属性)
        {
            返回 (SoapExtAttribute)attr;
        }
    
        公共覆盖无效初始化(对象初始值设定项)
        {
        }
    
        公共重写 Stream ChainStream(Stream 流)
        {
            mWireStream = 流;
            mApplicationStream = new MemoryStream();
            返回mApplicationStream;
        }
    
    
        公共重写 void ProcessMessage(SoapMessage 消息)
        {
            StreamWriter 作家 = null;
            bool fIsServer = (message.GetType() == typeof(SoapServerMessage));
            开关(消息.阶段)
            {
                案例 SoapMessageStage.BeforeDeserialize:
                    字符串 resp = new StreamReader(mWireStream).ReadToEnd();
                    StreamWriter w = new StreamWriter(mApplicationStream);
                    w.WriteLine(resp);
                    w.Flush();
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    休息;
                案例 SoapMessageStage.AfterSerialize:
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    字符串 reqXml = new StreamReader(mApplicationStream).ReadToEnd();
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(reqXml);
    
                    修改(文档);
    
                    reqXml = doc.InnerXml;
    
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    writer = new StreamWriter(mWireStream);
                    writer.WriteLine(reqXml);
                    writer.Flush();
    
                    XmlDocument d = new XmlDocument();
                    d.LoadXml(reqXml);
                    ServiceManager.RequestSoap = d.LastChild.OuterXml;
                    休息;
            }
        }
    
        私有无效修改(XmlDocument doc)
        {
            // 以您想要的任何方式更改文档,例如删除/添加前缀
        }
    }
    

I've had similar problems with accessing a Java WS from .NET client. I havent found an elegant solution to the problem, but I did solve it by changing the outgoing/incoming SOAP messages with a SoapExtension:

  1. Implement a SoapExtensionAttribute, like this:

    class SoapExtAttribute : SoapExtensionAttribute
    {
        int priority = 0;
    
        public override Type ExtensionType
        {
            get { return typeof(SoapExt); }
        }
    
        public override int Priority
        {
            get
            {
                return priority;
            }
            set
            {
                priority = value;
            }
        }
    }
    
  2. Implement a SoapExtension, like this:

    class SoapExt : SoapExtension
    {
        private Stream mWireStream = null;
        private Stream mApplicationStream = null;
    
        public override object GetInitializer(Type serviceType)
        {
            return serviceType;
        }
    
        public override object GetInitializer(LogicalMethodInfo methodInfo, SoapExtensionAttribute attribute)
        {
            return (SoapExtAttribute)attr;
        }
    
        public override void Initialize(object initializer)
        {
        }
    
        public override Stream ChainStream(Stream stream)
        {
            mWireStream = stream;
            mApplicationStream = new MemoryStream();
            return mApplicationStream;
        }
    
    
        public override void ProcessMessage(SoapMessage message)
        {
            StreamWriter writer = null;
            bool fIsServer = (message.GetType() == typeof(SoapServerMessage));
            switch (message.Stage)
            {
                case SoapMessageStage.BeforeDeserialize:
                    string resp = new StreamReader(mWireStream).ReadToEnd();
                    StreamWriter w = new StreamWriter(mApplicationStream);
                    w.WriteLine(resp);
                    w.Flush();
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    break;
                case SoapMessageStage.AfterSerialize:
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    string reqXml = new StreamReader(mApplicationStream).ReadToEnd();
    
                    XmlDocument doc = new XmlDocument();
                    doc.LoadXml(reqXml);
    
                    Modify(doc);
    
                    reqXml = doc.InnerXml;
    
                    mApplicationStream.Seek(0, SeekOrigin.Begin);
                    writer = new StreamWriter(mWireStream);
                    writer.WriteLine(reqXml);
                    writer.Flush();
    
                    XmlDocument d = new XmlDocument();
                    d.LoadXml(reqXml);
                    ServiceManager.RequestSoap = d.LastChild.OuterXml;
                    break;
            }
        }
    
        private void Modify(XmlDocument doc)
        {
            // Change the doc in whatever way you want, e.g. remove/add the prefixes
        }
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文