c# WebService中的空参数

本文关键字:参数 WebService | 更新日期: 2023-09-27 18:11:05

我得到了一个我必须实现的WebService的WSDL,并从中生成了它的基本结构(使用。net SDK中的svcutils.exe和WSDL进行了尝试)。Visual Studio的蓝色插件)。有人会使用这个WebService,而我不能更改他们所做请求的类型。下面是我从客户端捕获的一个示例SOAP请求:

<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>
<env:Header></env:Header>
<env:Body>
<ns1:setNetworkActivatorResponse xmlns:ns1='http://client.response.ws.meh/'>
    <OrderResponse>
        <orderHeader>
            <clientID>trD-125205</clientID>
            <description>0</description>
            <operation>trD</operation>
            <orderKey>
                <applicationContext>
                    <URL>http://xxx.xxx.xxx.xxx:8181/</URL>
                    <hostName>xxx.xxx.xxx.xxx</hostName>
                </applicationContext>
<applicationDN>XYPT</applicationDN>
                    <primaryKey>170809</primaryKey>
                </orderKey>
                <priority>1</priority>
            </orderHeader>
            <info>
                Yadda Yadda Yadda text.
            </info>
        </OrderResponse>
    </ns1:setNetworkActivatorResponse>
    </env:Body>
    </env:Envelope>

此请求调用WebServer上的正确方法(setNetworkActivatorResponse()),但是该方法的参数OrderResponse被传递为null。我相信它与名称空间有关,因为如果我修改请求并将ns1名称空间添加到OrderResponse(如<ns1:OrderResponse> </ns1:OrderResponse>),则参数被正确传递,并且OrderResponse对象在c#方法中可用。

我的方法定义为一个典型的c# .NET WebService:

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[WebService(Namespace = "http://client.response.ws.meh/")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://client.response.ws.meh/")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://client.response.ws.meh/", ConfigurationName = "ResponseClientWebService")]
public class ResponseClientWebService : System.Web.Services.WebService
{
    [WebMethod]
    public void setNetworkActivatorResponse(OrderResponse OrderResponse)
    {
        String meh = OrderResponse.info;
    }
}

这是我的OrderResponse类:

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.233")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://client.response.ws.na.agorang.ptinovacao.pt/")]
public partial class OrderResponse
{
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string messageID;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public OrderHeader orderHeader;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
public string info;
}

所需的类(OrderResponse等)也被定义和实现。知道我可以在我的代码中更改什么吗(看到我不能更改客户端的请求以在OrderResponse上具有ns1名称空间),以使其能够接受SOAP请求?谢谢!

c# WebService中的空参数

OrderResponse类上的XmlTypeAttribute行是您的web服务要求存在于特定名称空间中的原因。要解决这个问题,我很确定您可以从文件中删除这行代码。

但是,从生成的代码中删除这一行并不是最佳解决方案,因为从WSDL中重新生成将把这一行放回代码中。理想情况下,您应该修改WSDL,以便将元素定义为存在于空白的目标名称空间中。