配置WCF服务以接受不合格的参数

时间:2011-08-04 15:54:59

标签: wcf web-services

我的WCF服务无法识别以非限定格式发送的请求参数值,而是替换默认值。

例如,此请求将产生“您输入: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>

但是对此请求的回复是“你输入了: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>

如何修改我的服务,以便这两种类型的请求都会使用我发送的值?

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;
        }
    }
}

1 个答案:

答案 0 :(得分:2)

这两个请求是等效的XML,并且不符合服务WSDL。 “不合格”请求会将value元素解析为默认的XML命名空间,这将取决于该请求的整体XML。 WCF不理解value是soap envelop XML的一部分,并且无法将其与任何DataContract类相匹配。您可以尝试将默认的XML命名空间作为服务XML命名空间,如下所示,然后查看该服务是否会正确处理它:

<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>