配置 WCF 服务以接受不合格的参数 [英] Configure WCF service to accept unqualified parameter

查看:29
本文介绍了配置 WCF 服务以接受不合格的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

例如,此请求将产生您输入:21"的结果.

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>

但是对这个请求的响应是你输入了:0".

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>

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

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

推荐答案

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

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>

这篇关于配置 WCF 服务以接受不合格的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆