Web Services 如何自定义SOAP Web服务名称空间

r7s23pms  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(366)

我正在www.example.com核心中编写一个soap web服务asp.net,使用soapcore替换一个现有的web服务。调用者的请求xml不能更改,因为我们打算最大限度地减少对该端的更改。

<soapenv:Envelope
    xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:ser="http://vendornamespace.com/"
    xmlns:idat="http://schemas.datacontract.org/2004/07/iDataContract">
    <soapenv:Header/>
    <soapenv:Body>
        <ser:ActionRequest>
            <ser:composite>
                <idat:param1>H04</idat:param1>
                <idat:param2>100</idat:param2>
            </ser:composite>
        </ser:PlaceOrder>
    </soapenv:Body>
</soapenv:Envelope>

我的Web服务界面如下所示

public interface INewWebsServices
{
    [OperationContract(Action = "ActionRequest")]
    Task<WebSvcResponseClass> ActionRequest([MessageParameter(Name = "composite")] WebServiceReqactionMethod_A);
}

我的请求类看起来像

[DataContract (Namespace = "http://schemas.datacontract.org/2004/07/iDataContract", Name ="idat")]
    
[MessageContract()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://schemas.datacontract.org/2004/07/iDataContract")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://schemas.datacontract.org/2004/07/iDataContract", IsNullable = false)]
public class WebServiceReq
{
    [MessageBodyMember]
    public string param1 { get; set; }

    [MessageBodyMember]
    public string param2 { get; set; }
}

这产生了web服务请求:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" 
xmlns:ser="http://vendornamespace.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:PlaceOrder>
         <ser:composite>            
            <ser:param1>?</ser:param1>            
            <ser:param2>?</ser:param2>
         </ser:composite>
      </ser:PlaceOrder>
   </soapenv:Body>
</soapenv:Envelope>

很明显,标头中缺少idat名称空间,并且Web请求参数中未使用该名称空间。
如何修改WebServiceReq类以包含缺少的命名空间,以便SoapCore可以正确地反序列化传入的请求。

vyu0f0g1

vyu0f0g11#

您可以尝试在命名空间iDataContract中定义WebSvcResponseClass,也可以使用[DataMember]而不是[MessageBodyMember]来修饰属性。

public interface INewWebsServices
{

    [System.ServiceModel.OperationContractAttribute(Action = "http://domain/namespace", ]
        Task<iDataContract.WebSvcResponseClass> WebServiceReqactionMethod_A(iDataContract.WebserviceRequestclass composite); 
}

如果您尝试使用soapUI,则使用名称空间会在Web服务请求中生成 idat 作为前缀。

相关问题