SmoMetadataProvider throws bare NullReferenceException when the current database cannot be resolved (Azure/Fabric endpoint)
評估
研究方向
閱讀 src/Microsoft.SqlServer.Management.SmoMetadataProvider/Server.cs 和 DatabaseObject.cs,重點關注 SqlAzureDatabase 分支以及 DictionaryCollectionBase.Add 之前的資料庫查找。在 Release 中執行無相依項目的重現,然後驗證針對無法解析的目前資料庫所選擇的行為——要麼拋出一個指出該資料庫名稱的型別化錯誤,要麼帶有 trace 地正常略過——並使用回歸測試涵蓋最終行為。
由索引模型根據 Issue 內容生成。
描述
Summary
SmoMetadataProvider.CreateConnectedProvider(...) throws an unhandled
System.NullReferenceException while building server metadata when the connection's
current database (Initial Catalog) does not resolve in the server's SMO database
collection. The exception surfaces with the useless message
"Object reference not set to an instance of an object." and no indication of which
database or what went wrong.
This bites tools that reuse SmoMetadataProvider for T-SQL IntelliSense / query
validation (Azure Data Studio, the VS Code SQL extension, and Fabric's Data Agent
"NL2SQL" example-query validation), against Azure SQL / Fabric SQL analytics endpoints.
Environment
- Component:
Microsoft.SqlServer.Management.SmoMetadataProvider - Source: reproduces against current
main
(src/Microsoft/SqlServer/Management/SmoMetadataProvider/Server.cs). - Endpoint: Azure/Fabric SQL endpoint (
ServerType == SqlAzureDatabase), connected mode.
Observed stack trace
System.NullReferenceException: Object reference not set to an instance of an object.
at Microsoft.SqlServer.Management.SqlParser.MetadataProvider.DictionaryCollectionBase`2.Add(T item)
at Microsoft.SqlServer.Management.SmoMetadataProvider.Server..ctor(Server smoMetadataObject, Boolean isConnected)
at Microsoft.SqlServer.Management.SmoMetadataProvider.SmoMetadataProvider..ctor(Server server, Boolean isConnected)
at Microsoft.SqlServer.Management.SmoMetadataProvider.ConnectedSmoMetadataProvider..ctor(ServerConnection ...)
Root cause (in current source)
In Server..ctor (Server.cs), for a SqlAzureDatabase endpoint smoMasterDb is forced
to null, so the constructor takes the "no access to master, only add the current
database" else-branch:
else
{
var smoDb =
this.m_smoMetadataObject.Databases[this.m_smoMetadataObject.ConnectionContext.DatabaseName];
databases.Add(new Database(smoDb, this)); // no null-check on smoDb
}
The SMO Databases[name] indexer returns null when the current database cannot be
resolved by name (inaccessible, or the endpoint catalog does not expose that exact name).
smoDb is then wrapped and added with no null-check. The DatabaseObject<S,P> base ctor
(DatabaseObject.cs) guards its argument only with a Debug.Assert (compiled out of
Release builds), so it stores the null, and its Name getter dereferences it:
protected DatabaseObject(S smoMetadataObject, P parent)
{
Debug.Assert(smoMetadataObject != null, ...); // no-op in Release
this.m_smoMetadataObject = smoMetadataObject; // stores null
}
public string Name => this.m_smoMetadataObject.Name; // NRE when null
DictionaryCollectionBase.Add reads the item's Name to key the collection, so the null
is dereferenced during Add — matching the reported stack.
Reproduction
A dependency-free program that models the exact code shape of Server.cs +
DatabaseObject.cs reproduces the NRE deterministically (run in Release so the
Debug.Assert is compiled out, as it is in shipped SMO):
- Case 1: current DB resolves → OK.
- Case 2: current DB does not resolve →
System.NullReferenceException. - Case 3: with a null-check → a clear "database not found / not accessible" error.
To reproduce against a live endpoint: call
SmoMetadataProvider.CreateConnectedProvider(new ServerConnection(sqlConn)) with an
Initial Catalog whose database is connectable but does not resolve in the enumerated
catalog.
Expected behavior
CreateConnectedProvider should not throw a bare NullReferenceException. When the
current database cannot be resolved it should either (a) throw a clear, typed exception
naming the database, or (b) skip the unresolved database gracefully (consistent with the
"only add the current database" intent).
Proposed fix
Null-check smoDb in the else-branch of Server..ctor before wrapping/adding it (and/or
make the DatabaseObject constructor throw on null instead of only Debug.Assert). Happy
to send a PR — would you prefer option (a) throw a typed error naming the database, or
option (b) skip + trace the unresolved database for graceful IntelliSense degradation?
I have both prepared.
- 主要語言
- C#
- 星號
- 143
- 分支
- 29
- PR 合併指標
- 30 天內沒有已合併 PR
環境準備
這個專案沒有提供開發容器、Dockerfile 或貢獻指南,環境需要你自己搭建:先看它的 README,通用步驟見我們的新手貢獻指南。
從這裡開始
- 先讀完整個 Issue,再讀專案的貢獻指南。
- 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
- Fork 儲存庫,在一個分支上完成修改。
- 送出 Pull Request,並在描述裡引用這個 Issue 編號。
microsoft/sqlmanagementobjects 的其他 Issue
-
Path parsing functions are incorrect when running on Linux可能已有人在做 @shueybubbles 於 4 天前認領。 未關閉
難度 2/5 1-3 小時 新手友好度 70/100
-
難度 2/5 1-3 小時 新手友好度 62/100
-
難度 3/5 1-2 天 新手友好度 72/100
-
難度 4/5 3-5 天 新手友好度 74/100
-
難度 3/5 1-2 天 新手友好度 78/100
查看 microsoft/sqlmanagementobjects 的全部 Issue
相似的 Issue
-
難度 2/5 1-3 小時 新手友好度 72/100
PCL-Community/PCL-CE#3652 ·
維護者通常 1 天內回覆
-
area:frontend bug FE P3
難度 2/5 1-3 小時 新手友好度 68/100
klasolsson81/jobbliggaren#2010 ·
維護者通常 1 天內回覆
-
agentic-workflows untriaged
難度 1/5 1 小時以內 新手友好度 65/100
維護者通常 1 天內回覆
-
area: homeblaze type: bug
難度 2/5 1-3 小時 新手友好度 74/100
RicoSuter/Namotion.Interceptor#630 ·
維護者通常 1 天內回覆
-
Akka.Hosting enhancement
難度 2/5 1-3 小時 新手友好度 65/100