تحميل قسم تكوين System.ServiceModel باستخدام ConfigurationManager
-
09-06-2019 - |
سؤال
باستخدام C# .NET 3.5 وWCF، أحاول كتابة بعض تكوينات WCF في تطبيق العميل (اسم الخادم الذي يتصل به العميل).
الطريقة الواضحة هي الاستخدام ConfigurationManager
لتحميل قسم التكوين وكتابة البيانات التي أحتاجها.
var serviceModelSection = ConfigurationManager.GetSection("system.serviceModel");
يبدو أنه يعود دائمًا فارغًا.
var serviceModelSection = ConfigurationManager.GetSection("appSettings");
يعمل على أكمل وجه.
قسم التكوين موجود في App.config ولكن لسبب ما ConfigurationManager
يرفض تحميل system.ServiceModel
قسم.
أريد تجنب تحميل الملف xxx.exe.config يدويًا واستخدام XPath ولكن إذا اضطررت إلى اللجوء إلى ذلك فسوف أفعل ذلك.فقط يبدو وكأنه قليلا من الاختراق.
أي اقتراحات؟
المحلول
ال <system.serviceModel>
العنصر مخصص لقسم التكوين مجموعة, ، وليس قسما.سوف تحتاج إلى استخدام System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup()
للحصول على المجموعة بأكملها.
نصائح أخرى
http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html
// Automagically find all client endpoints defined in app.config
ClientSection clientSection =
ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElementCollection endpointCollection =
clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
List<string> endpointNames = new List<string>();
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
endpointNames.Add(endpointElement.Name);
}
// use endpointNames somehow ...
ويبدو أن تعمل بشكل جيد.
هذا ما كنت أبحث عنه بفضل @marxidad على المؤشر.
public static string GetServerName()
{
string serverName = "Unknown";
Configuration appConfig = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup serviceModel = ServiceModelSectionGroup.GetSectionGroup(appConfig);
BindingsSection bindings = serviceModel.Bindings;
ChannelEndpointElementCollection endpoints = serviceModel.Client.Endpoints;
for(int i=0; i<endpoints.Count; i++)
{
ChannelEndpointElement endpointElement = endpoints[i];
if (endpointElement.Contract == "MyContractName")
{
serverName = endpointElement.Address.Host;
}
}
return serverName;
}
لا يدعم GetSectionGroup() أية معلمات (ضمن إطار العمل 3.5).
بدلا من ذلك استخدم:
Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ServiceModelSectionGroup group = System.ServiceModel.Configuration.ServiceModelSectionGroup.GetSectionGroup(config);
بفضل الملصقات الأخرى، هذه هي الوظيفة التي قمت بتطويرها للحصول على معرف URI لنقطة نهاية محددة.كما يقوم أيضًا بإنشاء قائمة بنقاط النهاية المستخدمة وملف التكوين الفعلي الذي تم استخدامه عند تصحيح الأخطاء:
Private Function GetEndpointAddress(name As String) As String
Debug.Print("--- GetEndpointAddress ---")
Dim address As String = "Unknown"
Dim appConfig As Configuration = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None)
Debug.Print("app.config: " & appConfig.FilePath)
Dim serviceModel As ServiceModelSectionGroup = ServiceModelSectionGroup.GetSectionGroup(appConfig)
Dim bindings As BindingsSection = serviceModel.Bindings
Dim endpoints As ChannelEndpointElementCollection = serviceModel.Client.Endpoints
For i As Integer = 0 To endpoints.Count - 1
Dim endpoint As ChannelEndpointElement = endpoints(i)
Debug.Print("Endpoint: " & endpoint.Name & " - " & endpoint.Address.ToString)
If endpoint.Name = name Then
address = endpoint.Address.ToString
End If
Next
Debug.Print("--- GetEndpointAddress ---")
Return address
End Function