WebService wsdl响应

时间:2015-12-30 13:58:01

标签: c#

我的WebService中的方法返回一个包含5 variables的DataSet。其中2个是string,1 int和其他2个boolean

发布WebService后,wsdl中的具体响应如下所示:<s:element minOccurs="0" maxOccurs="1" name="myMethodResult">

现在,使用WebService的人问我是否可以以某种方式更改它,以便wsdl最终看起来像这样:

<s:element minOccurs="1" maxOccurs="1" name="variable1" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="variable2" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="variable3" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="variable4" type="s:boolean" />
<s:element minOccurs="1" maxOccurs="1" name="variable5" type="s:boolean" />

我怎样才能做到这一点?

1 个答案:

答案 0 :(得分:0)

如果这是一个WCF Web服务,您可以创建一个实现IContractBehaviorIWsdlExportExtension的属性,如this post中所述。

以下是摘录:

void IWsdlExportExtension.ExportContract(WsdlExporter exporter, WsdlContractConversionContext context)
{
    foreach (var operation in context.Contract.Operations)
    {
        var inputMessage = operation.Messages.
            Where(m => m.Direction == MessageDirection.Input).First();
        var parameters = operation.SyncMethod.GetParameters();

        for (int i = 0; i < parameters.Length; i++)
        {
            object[] attributes = parameters[i].GetCustomAttributes(
                typeof(OptionalAttribute), false);

            if (attributes.Length == 0)
            {
                // The parameter has no [Optional] attribute, add it
                // to the list of parameters that we need to adjust
                // the XML schema for later on.
                _requiredParameter.Add(new RequiredMessagePart()
                    {
                        Namespace = inputMessage.Body.Parts[i].Namespace,
                        Message = operation.Name,
                        Name = inputMessage.Body.Parts[i].Name
                    });
            }
        }
    }
}

void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
    foreach (var p in _requiredParameter)
    {
        var schemas = exporter.GeneratedXmlSchemas.Schemas(p.Namespace);

        foreach (XmlSchema schema in schemas)
        {
            var message = (XmlSchemaElement)schema.Elements[p.XmlQualifiedName];
            var complexType = (XmlSchemaComplexType)message.ElementSchemaType;
            var sequence = (XmlSchemaSequence)complexType.Particle;

            foreach (XmlSchemaElement item in sequence.Items)
            {
                if (item.Name == p.Name)
                {
                    item.MinOccurs = 1;
                    item.MinOccursString = "1";
                }
            }
        }
    }
}

感谢作者 Marcel Veldhuizen