开发者

Configure WCF service to accept unqualified parameter

开发者 https://www.devze.com 2023-03-26 11:18 出处:网络
My WCF service does not recognize request parameter values that are sent in unqualified form and substitutes default values instead.

My WCF service does not recognize request parameter values that are sent in unqualified form and substitutes default values instead.

For example, this request will yield a result of "You entered: 21".

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.example.org/SampleService/">
   <soapenv:Header/>
   <soapenv:Body>
      <sam:GetData>
         <sam:value>21</sam:value>
      </sam:GetData>
   </soapenv:Body>
</soapenv:Envelope>

But the response to this request is "You entered: 0".

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sam="http://www.example.org/SampleService/">
   <soapenv:Header/>
   <soapenv:Body>
      <sam:GetData>
         <value>21</value>
      </sam:GetData>
   </soapenv:Body>
</soapenv:Envelope>

How can I modify my service so that both types of requests will use the value I send in?

IService1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary2
{
    // NOTE: If you change the interface name "IService1" here, you must also update the reference to "IService1" in App.config.
    [ServiceContract(Namespace = "http://www.example.org/SampleService/", Name = "sampleservice")]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        // TODO: Add your service operations here
    }

    // Use a data contract as illustrated in the sample below to add composite types to service operations
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

Service1.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace WcfServiceLibrary2
{
   public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }

        public CompositeType GetDataUsingDataContract(CompositeType composite)
        {
            if (composite.BoolValue)
           开发者_开发技巧 {
                composite.StringValue += "Suffix";
            }
            return composite;
        }
    }
}


Those two requests are not equivalent XML and don't conform to the service WSDL. The "unqualified" request will parse the value element into the default XML namespace which will depend on the overall XML for that request. WCF doesn't understand value as being part of the soap envelop XML and can't match it to any DataContract class. You can try making the default XML namespace be your service XML namespace as shown below and see if the service will then process it correctly:

<soapenv:Envelope
   xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
   xmlns="http://www.example.org/SampleService/">
   <soapenv:Header/>
   <soapenv:Body>
      <GetData>
         <value>21</value>
      </GetData>
   </soapenv:Body>
</soapenv:Envelope>
0

精彩评论

暂无评论...
验证码 换一张
取 消