1: internal static class WebServiceConfigurationReader
2: {
3: internal static WebServiceInfo Read(string webServiceUrl)
4: {
5: if (string.IsNullOrWhiteSpace(webServiceUrl)) throw new Exception("WebServiceUrl parameter required.");
6: var uri = GetUriFromString(webServiceUrl);
7: var request = CreateWebRequest(uri);
8: var serviceDescription = GetServiceDescription(request);
9: return serviceDescription != null ? ParseServiceDescription(serviceDescription) : null;
10: }
11:
12: internal static Uri GetUriFromString(string webServiceUrl)
13: {
14: UriBuilder uriBuilder = new UriBuilder(webServiceUrl);
15: uriBuilder.Query = "Wsdl";
16: return uriBuilder.Uri;
17: }
18:
19: internal static HttpWebRequest CreateWebRequest(Uri uri)
20: {
21: var webRequest = (HttpWebRequest)WebRequest.Create(uri);
22: webRequest.ContentType = "text/xml;charset="utf-8"";
23: webRequest.Method = "GET";
24: webRequest.Accept = "text/xml";
25: return webRequest;
26:
27: }
28:
29: internal static ServiceDescription GetServiceDescription(HttpWebRequest request)
30: {
31: using (var response = request.GetResponse())
32: {
33: using (var stream = response.GetResponseStream())
34: {
35: return stream != null ? ServiceDescription.Read(stream) : null;
36: }
37: }
38: }
39:
40: internal static WebServiceInfo ParseServiceDescription(ServiceDescription serviceDescription)
41: {
42: var webServiceInfo = new WebServiceInfo();
43: webServiceInfo.TargetNamespace = serviceDescription.TargetNamespace;
44: webServiceInfo.Methods = GetWebServiceMethods(serviceDescription);
45: return webServiceInfo;
46: }
47:
48: internal static List<WebServiceMethod> GetWebServiceMethods(ServiceDescription serviceDescription)
49: {
50: var webSerivceMethodList = new List<WebServiceMethod>();
51: foreach (PortType portType in serviceDescription.PortTypes)
52: {
53:
54: foreach (Operation operation in portType.Operations)
55: {
56: var method = new WebServiceMethod();
57: method.Name = operation.Name;
58: method.InputParameters = GetWebMethodParameters(serviceDescription, operation.Name);
59: method.OutputParameters = GetWebMethodParameters(serviceDescription, operation.Name + "Response");
60: Console.Out.WriteLine();
61: webSerivceMethodList.Add(method);
62:
63: }
64: }
65: return webSerivceMethodList;
66: }
67:
68: internal static List<WebServiceMethodParameter> GetWebMethodParameters(ServiceDescription serviceDescription, string messagePartName)
69: {
70: List<WebServiceMethodParameter> parameters = new List<WebServiceMethodParameter>();
71:
72: Types types = serviceDescription.Types;
73: System.Xml.Schema.XmlSchema xmlSchema = types.Schemas[0];
74:
75: foreach (object item in xmlSchema.Items)
76: {
77: System.Xml.Schema.XmlSchemaElement schemaElement = item as System.Xml.Schema.XmlSchemaElement;
78: if (schemaElement != null)
79: {
80: if (schemaElement.Name == messagePartName)
81: {
82: System.Xml.Schema.XmlSchemaType schemaType = schemaElement.SchemaType;
83: System.Xml.Schema.XmlSchemaComplexType complexType = schemaType as System.Xml.Schema.XmlSchemaComplexType;
84: if (complexType != null)
85: {
86: System.Xml.Schema.XmlSchemaParticle particle = complexType.Particle;
87: System.Xml.Schema.XmlSchemaSequence sequence = particle as System.Xml.Schema.XmlSchemaSequence;
88: if (sequence != null)
89: {
90: foreach (System.Xml.Schema.XmlSchemaElement childElement in sequence.Items)
91: {
92: string parameterName = childElement.Name;
93: string parameterType = childElement.SchemaTypeName.Name;
94: parameters.Add(new WebServiceMethodParameter(parameterName, parameterType));
95: }
96: }
97: }
98: }
99: }
100: }
101: return parameters;
102: }
103: }