在爲我們搜索了互聯網後,我和我的團隊決定採用不同的方法來擺脫nillable =「true」。我們所做的是以下情況:
- 註冊在應用程序啓動特殊
HttpModule
。
- 此模塊向BeginRequest事件註冊了一個處理程序。
- 在請求WSDL或XSD(通過查看查詢字符串進行檢查)的情況下,掛鉤處理程序將用裝飾器(Stream)替換默認的
Response.Filter
。
- 這個裝飾器會緩衝寫入的數據直到達到文檔末尾。
- 當所有數據被寫入時,裝飾器根據這些數據建立一個
XDocument
,並通過該文檔設置(除其他外,例如添加文檔)nillable =「false」。
這個作品都很可愛。
使用Response.Filter
的想法被發現here。
這裏的HTTP模塊:
public class WsdlInterceptionHttpModule : IHttpModule
{
public void Init(HttpApplication application)
{
application.BeginRequest += (sender, e) =>
{
var context = application.Context;
if (IsRequestForWsdl(context.Request))
{
context.Response.Filter =
new WsdlAnnotationsFilterDecorator(context.Response.Filter);
}
};
}
private static bool IsRequestForWsdl(HttpRequest request) { ... }
}
這裏是WsdlAnnotationsFilterDecorator
:
public class WsdlAnnotationsFilterDecorator : Stream
{
private const string DefinitionsEndOfFileMarker = "</wsdl:definitions>";
private const string SchemaEndOfFileMarker = "</xs:schema>";
private readonly Stream inputStream;
private readonly StringBuilder responseXml = new StringBuilder();
private bool firstWrite = true;
private string endOfFileMarker = DefinitionsEndOfFileMarker;
public WsdlAnnotationsFilterDecorator(Stream inputStream)
{
this.inputStream = inputStream;
this.responseXml = new StringBuilder();
}
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return true; } }
public override bool CanWrite { get { return true; } }
public override long Length { get { return 0; } }
public override long Position { get; set; }
public override void Close()
{
inputStream.Close();
}
public override void Flush()
{
inputStream.Flush();
}
public override long Seek(long offset, SeekOrigin origin)
{
return inputStream.Seek(offset, origin);
}
public override void SetLength(long length)
{
inputStream.SetLength(length);
}
public override int Read(byte[] buffer, int offset, int count)
{
return inputStream.Read(buffer, offset, count);
}
public override void Write(byte[] buffer, int offset, int count)
{
string valueToWrite = UTF8Encoding.UTF8.GetString(buffer, offset, count);
SetEndOfFileMarker(valueToWrite);
if (!valueToWrite.EndsWith(this.endOfFileMarker))
{
responseXml.Append(valueToWrite);
}
else
{
responseXml.Append(valueToWrite);
string finalXml = responseXml.ToString();
finalXml = WsdlAnnotator.Annotate(finalXml);
byte[] data = UTF8Encoding.UTF8.GetBytes(finalXml);
inputStream.Write(data, 0, data.Length);
}
}
private void SetEndOfFileMarker(string valueToWrite)
{
if (firstWrite)
{
int definitionTagIndex = valueToWrite.IndexOf("<wsdl:definitions");
int schemaTagIndex = valueToWrite.IndexOf("<xs:schema");
if (definitionTagIndex > -1 || schemaTagIndex > -1)
{
firstWrite = false;
if (definitionTagIndex > -1 && schemaTagIndex > -1)
{
endOfFileMarker =
definitionTagIndex < schemaTagIndex
? DefinitionsEndOfFileMarker : SchemaEndOfFileMarker;
}
else if (definitionTagIndex > -1)
{
endOfFileMarker = DefinitionsEndOfFileMarker;
}
else if (schemaTagIndex > -1)
{
endOfFileMarker = SchemaEndOfFileMarker;
}
}
}
}
}
的WsdlAnnotator
是所有魔術發生:
internal static class WsdlAnnotator
{
internal static string Annotate(string xml)
{
XDocument document = XDocument.Parse(xml);
try
{
// Your magic here.
}
catch (Exception ex)
{
throw new InvalidOperationException(
ex.Message + " Document: " + document.ToString(), ex);
}
return document.ToString(SaveOptions.None);
}
你必須寫自己WsdlExportExtension。 查看此答案的示例:http://stackoverflow.com/questions/10602749/setting-nillable-false-with-wcf/23366064#23366064 – Vizu 2014-04-29 13:33:05