在WCF服务生成的WSDL中定义Response对象的MinOccure和Nillable属性时遇到问题。 MinOccure仍然为0,Nillable仍然为真。我找到了代码https://github.com/covermymeds/integration-feedback-reference/blob/master/IntegrationPartnerEndpoint/RequiredParametersBehaviorAttribute.cs,并对其进行了修改,以也可以编辑输出参数(方法IWsdlExportExtension.ExportContract):
var outputMessage = operation.Messages.Where(m => m.Direction == MessageDirection.Output).First();
var outParam = operation.SyncMethod.ReturnParameter;
object[] outAttributes = outParam.GetCustomAttributes(typeof(OptionalAttribute), false);
if (outAttributes.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 = outputMessage.Body.ReturnValue.Namespace,
Message = $"{operation.Name}Response",
Name = outputMessage.Body.ReturnValue.Name
});
}
方法IWsdlExportExtension.ExportEndpoint设置MinOccures和IsNillable(这没有任何更改):
void IWsdlExportExtension.ExportEndpoint(WsdlExporter exporter, WsdlEndpointConversionContext context)
{
if (_requiredParameter == null)
{
// If we have defined two endpoints implementing the same contract within the same service,
// this method will be called twice. We only need to modify the schema once however.
return;
}
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 = message.ElementSchemaType as XmlSchemaComplexType;
var sequence = complexType.Particle as XmlSchemaSequence;
foreach (XmlSchemaElement item in sequence.Items)
{
if (item.Name == p.Name)
{
item.MinOccurs = 1;
item.MinOccursString = "1";
item.IsNillable = false;
break;
}
}
}
}
// Throw away the temporary list we generated
_requiredParameter = null;
}
此后,将参数MinOccures设置为1,但在生成的WSDL中缺少nillable(我认为因此默认值为false)。
有什么方法可以显示即使是假的nillable吗?