第09章-命名空间管理
第09章:命名空间管理
9.1 命名空间概述
命名空间(Namespace)在 GeoServer 中用于定义要素类型的 URI标识符,确保不同工作空间中的要素类型具有全局唯一性。每个工作空间都关联一个命名空间。
9.1.1 命名空间的作用
- 唯一标识:为要素类型提供全局唯一的 URI 标识
- XML 命名空间:在 GML 和其他 XML 输出中使用
- WFS 支持:WFS 服务使用命名空间区分不同的要素类型
- 避免冲突:防止不同工作空间中同名要素的冲突
9.1.2 命名空间与工作空间的关系
工作空间(myWorkspace)
↓ 关联
命名空间(http://example.com/myWorkspace)
↓ 标识
要素类型(myWorkspace:cities)
9.2 NamespaceService 核心功能
9.2.1 服务接口
public class NamespaceService
{
private readonly IGeoServerHttpClient _httpClient;
public NamespaceService(IGeoServerHttpClient httpClient)
{
_httpClient = httpClient;
}
// 核心方法
public async Task<Namespace[]> GetNamespacesAsync();
public async Task<Namespace> GetNamespaceAsync(string prefix);
public async Task CreateNamespaceAsync(Namespace namespaceObj);
public async Task UpdateNamespaceAsync(string prefix, Namespace namespaceObj);
public async Task DeleteNamespaceAsync(string prefix);
}
9.2.2 数据模型
public class Namespace
{
[JsonProperty("prefix")]
public string Prefix { get; set; }
[JsonProperty("uri")]
public string URI { get; set; }
[JsonProperty("isolated")]
public bool? Isolated { get; set; }
}
9.3 获取命名空间列表
using GeoServerDesktop.GeoServerClient.Configuration;
using GeoServerDesktop.GeoServerClient.Services;
var options = new GeoServerClientOptions
{
BaseUrl = "http://localhost:8080/geoserver",
Username = "admin",
Password = "geoserver"
};
using var factory = new GeoServerClientFactory(options);
var namespaceService = factory.CreateNamespaceService();
// 获取所有命名空间
var namespaces = await namespaceService.GetNamespacesAsync();
Console.WriteLine($"找到 {namespaces.Length} 个命名空间:");
foreach (var ns in namespaces)
{
Console.WriteLine($"- {ns.Prefix}: {ns.URI}");
}
9.4 创建命名空间
var namespace = new Namespace
{
Prefix = "myapp",
URI = "http://example.com/myapp"
};
await namespaceService.CreateNamespaceAsync(namespace);
Console.WriteLine("命名空间创建成功!");
// 验证创建
var created = await namespaceService.GetNamespaceAsync("myapp");
Console.WriteLine($"前缀: {created.Prefix}");
Console.WriteLine($"URI: {created.URI}");
9.5 更新命名空间
// 获取命名空间
var namespace = await namespaceService.GetNamespaceAsync("myapp");
// 更新 URI
namespace.URI = "http://newdomain.com/myapp";
await namespaceService.UpdateNamespaceAsync("myapp", namespace);
Console.WriteLine("命名空间更新成功!");
9.6 删除命名空间
try
{
await namespaceService.DeleteNamespaceAsync("old_namespace");
Console.WriteLine("命名空间删除成功!");
}
catch (GeoServerRequestException ex) when (ex.StatusCode == 404)
{
Console.WriteLine("命名空间不存在");
}
9.7 命名空间与工作空间协同管理
public class WorkspaceNamespaceManager
{
private readonly WorkspaceService _workspaceService;
private readonly NamespaceService _namespaceService;
public WorkspaceNamespaceManager(
WorkspaceService workspaceService,
NamespaceService namespaceService)
{
_workspaceService = workspaceService;
_namespaceService = namespaceService;
}
/// <summary>
/// 创建工作空间和对应的命名空间
/// </summary>
public async Task<bool> CreateWorkspaceWithNamespaceAsync(
string workspaceName,
string namespaceUri)
{
try
{
// 1. 创建工作空间
await _workspaceService.CreateWorkspaceAsync(workspaceName);
Console.WriteLine($"✓ 工作空间 '{workspaceName}' 创建成功");
// 2. 创建命名空间
var namespace = new Namespace
{
Prefix = workspaceName,
URI = namespaceUri
};
await _namespaceService.CreateNamespaceAsync(namespace);
Console.WriteLine($"✓ 命名空间 '{namespaceUri}' 创建成功");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"✗ 创建失败: {ex.Message}");
return false;
}
}
/// <summary>
/// 删除工作空间和对应的命名空间
/// </summary>
public async Task<bool> DeleteWorkspaceWithNamespaceAsync(
string workspaceName,
bool recurse = false)
{
try
{
// 1. 删除工作空间
await _workspaceService.DeleteWorkspaceAsync(workspaceName, recurse);
Console.WriteLine($"✓ 工作空间 '{workspaceName}' 删除成功");
// 2. 删除命名空间
await _namespaceService.DeleteNamespaceAsync(workspaceName);
Console.WriteLine($"✓ 命名空间 '{workspaceName}' 删除成功");
return true;
}
catch (Exception ex)
{
Console.WriteLine($"✗ 删除失败: {ex.Message}");
return false;
}
}
}
// 使用示例
var manager = new WorkspaceNamespaceManager(workspaceService, namespaceService);
await manager.CreateWorkspaceWithNamespaceAsync(
"project_data",
"http://mycompany.com/project/data");
9.8 命名空间最佳实践
9.8.1 URI 命名规范
public static class NamespaceURIConventions
{
/// <summary>
/// 生成标准的命名空间 URI
/// </summary>
public static string GenerateURI(
string domain,
string organization,
string project)
{
return $"http://{domain}/{organization}/{project}";
}
/// <summary>
/// 验证 URI 格式
/// </summary>
public static bool IsValidURI(string uri)
{
return Uri.TryCreate(uri, UriKind.Absolute, out var result) &&
(result.Scheme == Uri.UriSchemeHttp ||
result.Scheme == Uri.UriSchemeHttps);
}
}
// 使用示例
var uri = NamespaceURIConventions.GenerateURI(
"example.com",
"gis",
"basemap");
// 结果: "http://example.com/gis/basemap"
if (NamespaceURIConventions.IsValidURI(uri))
{
Console.WriteLine("URI 格式正确");
}
9.8.2 命名空间同步工具
public class NamespaceSynchronizer
{
private readonly WorkspaceService _workspaceService;
private readonly NamespaceService _namespaceService;
/// <summary>
/// 同步工作空间和命名空间
/// </summary>
public async Task SynchronizeAsync()
{
var workspaces = await _workspaceService.GetWorkspacesAsync();
var namespaces = await _namespaceService.GetNamespacesAsync();
var namespaceDict = namespaces.ToDictionary(ns => ns.Prefix);
foreach (var workspace in workspaces)
{
if (!namespaceDict.ContainsKey(workspace.Name))
{
Console.WriteLine($"⚠ 工作空间 '{workspace.Name}' 缺少对应的命名空间");
// 自动创建缺失的命名空间
var ns = new Namespace
{
Prefix = workspace.Name,
URI = $"http://geoserver.org/{workspace.Name}"
};
await _namespaceService.CreateNamespaceAsync(ns);
Console.WriteLine($"✓ 已为工作空间 '{workspace.Name}' 创建命名空间");
}
}
}
}
9.9 本章小结
本章学习了:
- 命名空间的概念和作用
- NamespaceService 的核心方法
- 命名空间的 CRUD 操作
- 与工作空间的协同管理
- URI 命名规范和最佳实践
下一章将学习服务设置与配置管理。
相关资源:

浙公网安备 33010602011771号