2015-12-30 127 views
0

我的WebService中的一個方法返回一個DataSet,其中包含5個variables。其中2人是string,1 int和另外2人booleanWebService wsdl響應

發佈的WebService後,在wsdl特定響應的樣子:<s:element minOccurs="0" maxOccurs="1" name="myMethodResult">

現在,人們消費的W​​ebService問我,如果我可以讓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" /> 

我該如何做到這一點?

回答

0

如果這是一個WCF Web服務,您可以創建一個屬性,實現IContractBehaviorIWsdlExportExtension,如this後所述。

這裏是它的摘錄:

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

與感謝作者馬塞爾Veldhuizen

+0

對不起,提供的信息不準確。不是WCF WS,而是常規SOAP。 –

+1

那不是那麼簡單。看看[這個](回答)。(http://stackoverflow.com/questions/3961112/how-to-make-a-dotnet-webservice-set-minoccurs-1-on-a-string-value)SO回答。您可以隨時手動更改或編寫一些自行更新文本的代碼。 –