1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
| using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.IO;
using System.Net;
using System.Reflection;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web.Services.Description;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace de.Soderer.Utilities
{
public class GenericWebserviceClient
{
#region MemberVariablen
private String webserviceName;
private String webserviceEndpointUrl;
private SoapHttpClientProtocol webserviceInstance;
#endregion
#region Konstruktor
public GenericWebserviceClient(String webserviceEndpointUrl)
: this(webserviceEndpointUrl, null, null, 0)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, int timeoutInMilliseconds)
: this(webserviceEndpointUrl, null, null, timeoutInMilliseconds)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, WebProxy webproxy)
: this(webserviceEndpointUrl, webproxy, null, 0)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, WebProxy webproxy, int timeoutInMilliseconds)
: this(webserviceEndpointUrl, webproxy, null, timeoutInMilliseconds)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, X509Certificate2 userCertificate)
: this(webserviceEndpointUrl, null, userCertificate, 0)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, X509Certificate2 userCertificate, int timeoutInMilliseconds)
: this(webserviceEndpointUrl, null, userCertificate, timeoutInMilliseconds)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, IWebProxy webproxy, X509Certificate2 userCertificate)
: this(webserviceEndpointUrl, webproxy, userCertificate, 0)
{
}
public GenericWebserviceClient(String webserviceEndpointUrl, IWebProxy webproxy, X509Certificate2 userCertificate, int timeoutInMilliseconds)
{
this.webserviceEndpointUrl = webserviceEndpointUrl;
loadWsdlInformation(webproxy, userCertificate, timeoutInMilliseconds);
}
#endregion
#region Methoden
private void loadWsdlInformation(IWebProxy webproxy, X509Certificate2 userCertificate, int timeoutInMilliseconds)
{
Constant.logDebug("<loadWsdlInformation>");
try
{
Constant.logDebug("WSDL-Abruf: " + webserviceEndpointUrl + "?wsdl");
Uri wsdlUri = new Uri(webserviceEndpointUrl + "?wsdl");
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(wsdlUri);
webRequest.Proxy = webproxy;
if (userCertificate != null)
{
Constant.logDebug("Authentifikation by Certificate");
webRequest.PreAuthenticate = true;
webRequest.ClientCertificates.Add(userCertificate);
}
else
webRequest.PreAuthenticate = false;
if (timeoutInMilliseconds >= 0)
webRequest.Timeout = timeoutInMilliseconds;
Stream requestStream = webRequest.GetResponse().GetResponseStream();
ServiceDescription serviceDescription = ServiceDescription.Read(requestStream);
Constant.logDebug("WSDL auswerten");
Service webservice = serviceDescription.Services[0];
webserviceName = webservice.Name;
ServiceDescriptionImporter servImport = new ServiceDescriptionImporter();
servImport.AddServiceDescription(serviceDescription, String.Empty, String.Empty);
servImport.ProtocolName = "Soap";
servImport.CodeGenerationOptions = CodeGenerationOptions.GenerateProperties;
Constant.logDebug("WebserviceClient Generierung");
CodeNamespace nameSpace = new CodeNamespace();
CodeCompileUnit codeCompileUnit = new CodeCompileUnit();
codeCompileUnit.Namespaces.Add(nameSpace);
ServiceDescriptionImportWarnings warnings = servImport.Import(nameSpace, codeCompileUnit);
if (warnings == 0)
{
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
String[] assemblyReferences = new String[] { "System.dll", "System.Web.Services.dll", "System.Web.dll", "System.Xml.dll" };
CompilerParameters compilerParameters = new CompilerParameters(assemblyReferences);
compilerParameters.GenerateInMemory = true; // Save assembly in memory only
CompilerResults results = provider.CompileAssemblyFromDom(compilerParameters, codeCompileUnit);
if (results.Errors.Count > 0)
throw new Exception("Can't compile code");
else
{
webserviceInstance = (SoapHttpClientProtocol)results.CompiledAssembly.CreateInstance(webserviceName);
Constant.logDebug("webserviceInstance:" + (webserviceInstance != null));
webserviceInstance.Url = webserviceEndpointUrl;
}
}
else
{
throw new Exception("Compile-Fehler: " + warnings);
}
}
catch (Exception ex)
{
throw new FatalException("Fehler beim Auslesen der WebserviceDefinition unter der Url '" + webserviceEndpointUrl + "': " + ex);
}
Constant.logDebug("</loadWsdlInformation>");
}
public Object callWebMethod(String webserviceMethodName, params Object[] webserviceMethodParameters)
{
String requestIdForLogging = Utilities.createNewVorgangsID();
try
{
MethodInfo webserviceMethodInfo = webserviceInstance.GetType().GetMethod(webserviceMethodName);
if (webserviceMethodInfo == null)
throw new Exception("Unbekannte WebMethode '" + webserviceMethodName + "'");
ParameterInfo[] parameterInfos = webserviceMethodInfo.GetParameters();
if (webserviceMethodParameters.Length != parameterInfos.Length)
throw new Exception("Ungültige Parameteranzahl im Aufruf des Webservice: Es wurden " + webserviceMethodParameters.Length + " bei erwarteten " + parameterInfos.Length + " Parametern geliefert");
//Prüfen der Parametertypen
StringBuilder errorneousParameterTypes = new StringBuilder();
for (int i = 0; i < parameterInfos.Length; i++)
{
if (webserviceMethodParameters[i] != null
&& parameterInfos[i].ParameterType.FullName != webserviceMethodParameters[i].GetType().FullName)
{
if (errorneousParameterTypes.Length > 0)
errorneousParameterTypes.Append("; \n");
errorneousParameterTypes.Append("Für den Parameter '" + parameterInfos[i].Name + "' wurde Typ '" + webserviceMethodParameters[i].GetType().FullName + "' gefunden statt erwartetem Typ '" + parameterInfos[i].ParameterType.FullName + "'");
}
}
if (errorneousParameterTypes.Length > 0)
throw new Exception("Ungültige Parameterarten im Aufruf des Webservice: \n" + errorneousParameterTypes.ToString());
if (PersistentConfiguration.AtsTestMode)
{
StringBuilder webserviceRequestData = new StringBuilder();
//Wenn ein UserZertifikat genutzt wird, dann auch dieses Loggen
for (int i = 0; i < ClientCertificates.Count; i++)
{
if (webserviceRequestData.Length > 0)
webserviceRequestData.Append("; \n");
webserviceRequestData.Append("UserCertificate(" + (i + 1) + ")=" + ((X509Certificate2)ClientCertificates[i]).GetNameInfo(X509NameType.SimpleName, false));
}
//Loggen der Parameter
for (int i = 0; i < parameterInfos.Length; i++)
{
if (webserviceRequestData.Length > 0)
webserviceRequestData.Append("; \n");
if (parameterInfos[i].Name.ToUpper().Contains(Constant.PASSWORD_WS_PARAMETER_NAMEPART))
webserviceRequestData.Append(parameterInfos[i].Name + "(" + parameterInfos[i].ParameterType.Name + ")=" + Constant.PASSWORD_REPLACEMENT);
else if (webserviceMethodParameters[i] is byte[])
webserviceRequestData.Append(parameterInfos[i].Name + "(" + parameterInfos[i].ParameterType.Name + ")=" + Utilities.convertByteArrayToString((byte[])webserviceMethodParameters[i]));
else
webserviceRequestData.Append(parameterInfos[i].Name + "(" + parameterInfos[i].ParameterType.Name + ")=" + webserviceMethodParameters[i]);
}
Utilities.writeWebserviceRequestInTempFile(
webserviceEndpointUrl,
webserviceMethodName,
webserviceRequestData.ToString(),
requestIdForLogging);
}
Object returnObject = webserviceMethodInfo.Invoke(webserviceInstance, webserviceMethodParameters);
if (PersistentConfiguration.AtsTestMode)
{
if (returnObject == null)
Utilities.writeAnswerInTempFile("<kein Rückgabewert>", requestIdForLogging);
else
Utilities.writeAnswerInTempFile(returnObject.ToString(), requestIdForLogging);
}
return returnObject;
}
catch (TargetInvocationException ex)
{
if (PersistentConfiguration.AtsTestMode)
Utilities.writeAnswerInTempFile(Utilities.Exception2Message(ex), requestIdForLogging);
if (ex.InnerException != null && ex.InnerException is SoapException)
throw new FatalException("Fehler beim Versenden des Webservice-Requests: " + ex.InnerException.Message);
else
throw new FatalException("Fehler beim WebserviceAufruf '" + webserviceName + "' unter der Url '" + webserviceEndpointUrl + "' Methode '" + webserviceMethodName + "': " + ex);
}
catch (Exception ex)
{
if (PersistentConfiguration.AtsTestMode)
Utilities.writeAnswerInTempFile(Utilities.Exception2Message(ex), requestIdForLogging);
throw new FatalException("Fehler beim WebserviceAufruf '" + webserviceName + "' unter der Url '" + webserviceEndpointUrl + "' Methode '" + webserviceMethodName + "': " + ex);
}
}
#endregion
#region Getter /Setter
public String WebserviceName
{
get
{
return webserviceName;
}
}
public bool PreAuthenticate
{
set
{
webserviceInstance.PreAuthenticate = value;
}
get
{
return webserviceInstance.PreAuthenticate;
}
}
public X509CertificateCollection ClientCertificates
{
get
{
return webserviceInstance.ClientCertificates;
}
}
public Encoding RequestEncoding
{
set
{
webserviceInstance.RequestEncoding = value;
}
get
{
return webserviceInstance.RequestEncoding;
}
}
public int Timeout
{
set
{
webserviceInstance.Timeout = value;
}
get
{
return webserviceInstance.Timeout;
}
}
public IWebProxy Proxy
{
set
{
webserviceInstance.Proxy = value;
}
get
{
return webserviceInstance.Proxy;
}
}
#endregion
}
} |
54055(+102) Besucher