public ServiceEndpoint GetEndpoint<TInterface>(string configFilename, string endpointConfigurationName)
|
|
|
Die App.config kann nach der Installation nicht mehr geändert werden ... das ist alles.
Zieht jetzt der Webservice um oder wird anders konfiguriert muss wieder ein Setup geliefert werden. Wenn ich die Konfiguration aus der DB lese reicht ein Update. – PinBack 15.12.2011
|
||
|
Wieso kann die app.config nicht mehr geändert werden? Wie Matthias schon sagte, ist es üblich die app.config hierfür zu verwenden. Das ist eine XML-Datei mit der Endung "config" und kann mit dem Editor bearbeitet werden.
– Jürgen Luhr 15.12.2011
|
||
|
Auf den Rechner (Business-Umfeld) hat der Benutzer weder die Berechtigung noch die nötige Fähigkeit eine XML-Datei zu editieren. Die Softwareverteilung wäre dazu in der Lage ... macht es aber nicht. Ein neues Setup zu installieren wenn sich lediglich eine Konfiguration ändert ist auch nicht die Lösung ==> zurück zu der Frage
– PinBack 15.12.2011
|
|
|
|
Danke. Das sieht schon mal sehr gut aus.
Dummerweise hab ich jedoch nur .NET 3.5 zur Verfügung. Aber das wäre ja ein Grund auf 4.0 zu wechseln. – PinBack 15.12.2011
|
|
|
|
Wie du schon bemerkt hast, ist das keine Anforderung die ich mir mal gerade ausgedacht habe.
Den Blog Eintrag hab ich auch schon gesehen. Könnte ich eventuell auch so machen. Schreibrechte auf die lokale App.config kann ich vergessen. Es sollte doch aber möglich sein eine beliebige XML-Datei für die Konfiguration eines Service zu verwenden. Sooo abwegig ist die Anforderung ja wohl nicht. Bin gerade dabei mir die Framework-Klassen anzuschauen. Wobei die Auswertung wohl in einer internen Klasse gemacht wird … mal schauen vielleicht hilft Reflection. – PinBack 15.12.2011
|
var loChannelFactory = new ChannelFactory<IMyInterface>(
ServiceModelHelper.GetEndpoint<IMyInterface>(lsTempfile));
public static class ServiceModelHelper
{
public static ServiceEndpoint GetEndpoint<TContract>(string psConfigFilename)
{
ExeConfigurationFileMap loFileMap = new ExeConfigurationFileMap()
{
ExeConfigFilename = psConfigFilename
};
Configuration loConfiguration = ConfigurationManager.OpenMappedExeConfiguration(loFileMap, ConfigurationUserLevel.None);
var loServiceGroup = ServiceModelSectionGroup.GetSectionGroup(loConfiguration);
ServiceEndpoint loServiceEndpoint = null;
foreach (ChannelEndpointElement loEndpointElement in loServiceGroup.Client.Endpoints)
{
if (loEndpointElement.Contract == typeof(TContract).FullName)
{
loServiceEndpoint = new ServiceEndpoint(
ContractDescription.GetContract(typeof(TContract)),
CreateBinding(loServiceGroup, loEndpointElement),
CreateEndPointAdress(loEndpointElement));
if (loServiceEndpoint.Behaviors.Count == 0 && !String.IsNullOrEmpty(loEndpointElement.BehaviorConfiguration))
{
LoadBehaviors(loServiceGroup, loEndpointElement.BehaviorConfiguration, loServiceEndpoint);
}
break;
}
}
return loServiceEndpoint;
}
private static Binding CreateBinding(ServiceModelSectionGroup poServiceGroup, ChannelEndpointElement poEndpointElement)
{
BindingCollectionElement loBindingCollection = poServiceGroup.Bindings[poEndpointElement.Binding];
IBindingConfigurationElement loBindingConfiguration =
loBindingCollection.ConfiguredBindings.First
(item => item.Name == poEndpointElement.BindingConfiguration);
Binding loBinding = CreateBinding(loBindingConfiguration);
loBindingConfiguration.ApplyConfiguration(loBinding);
return loBinding;
}
private static Binding CreateBinding(IBindingConfigurationElement poBindingConfiguration)
{
const string InvokeProperty = "BindingElementType";
Type loType = poBindingConfiguration.GetType();
var loProperty = loType.GetProperties(
BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance).First
(item => item.Name == InvokeProperty);
Type loCreateType = loProperty.GetValue(poBindingConfiguration, null) as Type;
return Activator.CreateInstance(loCreateType) as Binding;
}
private static EndpointAddress CreateEndPointAdress(ChannelEndpointElement poEndpointElement)
{
return new EndpointAddress(poEndpointElement.Address, CreateEndpointIdentity(poEndpointElement.Identity), poEndpointElement.Headers.Headers);
}
private static EndpointIdentity CreateEndpointIdentity(IdentityElement poIdentityElement)
{
const string InvokeClass = "System.ServiceModel.Description.ConfigLoader";
const string InvokeMethode = "LoadIdentity";
EndpointIdentity loEndpointIdentity = null;
if (poIdentityElement != null)
{
Type loType = typeof(ServiceEndpoint).Assembly.
GetType(InvokeClass);
loEndpointIdentity = loType.InvokeMember(
InvokeMethode,
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Static,
null,
null,
new object[] { poIdentityElement }) as EndpointIdentity;
}
return loEndpointIdentity;
}
/// <summary>
/// Siehe: http://social.msdn.microsoft.com/forums/en-US/wcf/thread/f33e620a-e332-4fd4-ae21-88c750437355/
/// </summary>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.Type.InvokeMember", Justification = "This is a real hack, but there is no other way of doing it :(")]
private static void LoadBehaviors(ServiceModelSectionGroup group, string behaviorConfiguration, ServiceEndpoint serviceEndpoint)
{
EndpointBehaviorElement behaviorElement = group.Behaviors.EndpointBehaviors[behaviorConfiguration];
for (int i = 0; i < behaviorElement.Count; i++)
{
BehaviorExtensionElement behaviorExtension = behaviorElement[i];
object extension = behaviorExtension.GetType().InvokeMember
(
"CreateBehavior",
BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance,
null,
behaviorExtension,
null,
CultureInfo.InvariantCulture
);
if (extension != null)
{
serviceEndpoint.Behaviors.Add((IEndpointBehavior)extension);
}
}
}
}
|
|
"When in Rome, do as the Romans do."