简体   繁体   English

WebService wsdl响应

[英]WebService wsdl response

A method in my WebService returns a DataSet containing 5 variables . 我的WebService中的方法返回一个包含5个variables的DataSet。 2 of them are string , 1 int and the 2 others boolean . 其中2个是string ,1个int ,另外2个是boolean

After publishing the WebService, that specific response in the wsdl looks like: <s:element minOccurs="0" maxOccurs="1" name="myMethodResult"> 发布WebService后, wsdl中的特定响应如下: <s:element minOccurs="0" maxOccurs="1" name="myMethodResult">

Now, people consuming the WebService ask me if I can change it somehow so the wsdl ends up looking something like this: 现在,使用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" />

How can I achieve that? 我该如何实现?

If this is a WCF web service, you can create an attribute that implements IContractBehavior and IWsdlExportExtension as described in this post. 如果这是一个WCF Web服务,您可以创建一个实现属性IContractBehaviorIWsdlExportExtension在描述这个职位。

Here is the excerpt of it: 这是摘录:

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

with thanks to the author Marcel Veldhuizen . 感谢作者Marcel Veldhuizen

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM