I've got a method, that gets parameters of service operations using reflection (based on a URL). The issue I am getting is that when the request of the operation inherits from classes, they are not getting included into the Parameter list.
The point of the below service is to dynamically get details of an WCF service/web service.
Code is below:
public ServiceDiscoveryResponse GetServiceDefinitions(ServiceDiscoveryRequest serviceDiscoveryRequest)
{
try
{
var requestNamspaceList = new List<RequestNamspace>();
var response = new List<Operation>();
Type parameterType = null;
var operationList = new List<string>();
var wsdl = ServiceDiscoveryHelper.SetupWsdl(serviceDiscoveryRequest);
ServiceDiscoveryHelper.GetNamespaceValues(wsdl, requestNamspaceList);
var proxyAssembly = wsdl.ProxyAssembly;
if (proxyAssembly == null) return null;
foreach (var type in proxyAssembly.GetTypes())
{
if (!TreeNodeProperty.IsWebService(type)) continue;
var proxy = ServiceDiscoveryHelper.SetupProxy(serviceDiscoveryRequest, type);
var protocol2 = proxy as SoapHttpClientProtocol;
if (protocol2 != null)
{
protocol2.CookieContainer = new CookieContainer();
protocol2.AllowAutoRedirect = true;
}
foreach (var info in type.GetMethods())
{
if (!TreeNodeProperty.IsWebMethod(info)) continue;
operationList.Add(info.Name);
if (!(info is MethodInfo)) continue;
var parameters = info.GetParameters();
var returnType = (TypeInfo)info.ReturnType;
var returnTypeFields = returnType.IsArray && ServiceDiscoveryHelper.IsNativeType(returnType) ? ((TypeInfo)returnType.GetElementType()).DeclaredFields : returnType.DeclaredFields;
var parameterList = new List<Parameter>();
var returnList = new List<Parameter>();
if (!ServiceDiscoveryHelper.IsNativeType(returnType))
{
returnTypeFields.ToList().ForEach(returnTypeField =>
{
if ((returnTypeField).FieldType.IsArray)
{
returnList.Add(new Parameter
{
DataType = !ServiceDiscoveryHelper.IsNativeType(returnTypeField.FieldType)
? returnTypeField.FieldType.BaseType.Name
: returnTypeField.Name,
Name = returnTypeField.FieldType.Name,
Parameters = returnTypeField.FieldType.IsClass && !ServiceDiscoveryHelper.IsNativeType(returnTypeField.FieldType) ?
ServiceDiscoveryHelper.GetParameterMembers(returnTypeField.FieldType) : new List<Parameter>(),
});
}
else
{
returnList.Add(new Parameter
{
Name = !ServiceDiscoveryHelper.IsNativeType(returnTypeField.FieldType)
? returnTypeField.FieldType.BaseType.Name
: returnTypeField.Name,
DataType = returnTypeField.FieldType.Name,
Parameters = returnTypeField.FieldType.IsClass && !ServiceDiscoveryHelper.IsNativeType(returnTypeField.FieldType) ?
ServiceDiscoveryHelper.GetParameterMembers(returnTypeField.FieldType) : new List<Parameter>(),
});
}
});
}
foreach (var parameter in parameters)
{
if (!parameter.IsOut && !parameter.ParameterType.IsByRef && parameter.IsOut) continue;
parameterType = parameter.ParameterType;
if (parameterType == typeof(string))
if (parameterType.IsByRef)
{
parameterType = parameterType.GetElementType();
}
var val = TreeNodeProperty.CreateNewInstance(parameterType);
if (val != null && val.ToString() == string.Empty || (parameterType == typeof(int) && (int)val == 0))
{
if (parameterType.IsClass && !ServiceDiscoveryHelper.IsNativeType(parameterType))
{
((TypeInfo)parameterType).DeclaredMembers.Select(item =>
new Parameter
{
Name = item.Name,
Parameters = null,
DataType = item.DeclaringType.Name
});
}
parameterList.Add(new Parameter
{
DataType = parameterType.Name,
Name = parameter.Name,
});
}
else
{
var typeInfo = (TypeInfo)val.GetType();
var declaredFields = typeInfo.IsArray && !ServiceDiscoveryHelper.IsNativeType(typeInfo.GetElementType()) ? ((TypeInfo)typeInfo.GetElementType()).DeclaredFields : typeInfo.DeclaredFields;
var baseType = typeInfo.BaseType;
if (declaredFields != null && declaredFields.Any())
{
declaredFields.ToList().ForEach(item =>
{
parameterList.Add(new Parameter
{
DataType = item.FieldType.Name,
Name = item.Name,
Parameters = item.FieldType.IsClass && !ServiceDiscoveryHelper.IsNativeType(item.FieldType) ?
ServiceDiscoveryHelper.GetParameterMembers(item.FieldType) : new List<Parameter>(),
});
});
}
if (!parameterType.IsClass && ServiceDiscoveryHelper.IsNativeType(parameterType) && ServiceDiscoveryHelper.IsNativeType(parameterType.BaseType))
{
if (baseType != null && baseType != typeof(string) && baseType != typeof(Array))
{
((TypeInfo)baseType).DeclaredFields.ToList().ForEach(item =>
{
parameterList.Add(new Parameter
{
DataType = item.FieldType.Name,
Name = item.Name,
});
});
}
declaredFields.ToList().ForEach(item =>
{
var fieldInfos = ((TypeInfo)item.FieldType).DeclaredFields;
var enumList = new List<string>();
fieldInfos.ToList().ForEach(enumValue => { enumList.Add(enumValue.Name); });
parameterList.Add(new Parameter
{
DataType = item.FieldType.Name,
Parameters = item.FieldType.IsClass && !ServiceDiscoveryHelper.IsNativeType(item.FieldType) ? ServiceDiscoveryHelper.GetParameterMembers(item.FieldType) : new List<Parameter>(),
Name = item.Name,
EnumValueList = item.FieldType.IsEnum ? enumList : new List<string>()
});
});
}
}
}
var namespaceValue = requestNamspaceList.ToList().Where(namespaceItem => namespaceItem.Name == TreeNodeProperty.CreateNewInstance(parameterType).ToString());
ServiceDiscoveryHelper.CreateResponse(response, info, parameterType, namespaceValue, parameterList, returnType, returnList, serviceDiscoveryRequest.EndpointUrl);
}
return new ServiceDiscoveryResponse { Operation = response };
}
}
catch (Exception exception)
{
Console.WriteLine(exception);
throw;
}
return null;
}
Aucun commentaire:
Enregistrer un commentaire