/// <summary> /// Using the base url, invokes the WebMethod with the given name /// </summary> /// <param name="methodName">Web Method name</param> /// <param name="encode">Encode params</param> /// <param name="soapActionComplement">Includes more information on the SOAPAction parameter</param> public ResultType Invoke <ResultType>(string methodName, bool encode, string soapActionComplement) { try { if (string.IsNullOrWhiteSpace(methodName)) { throw new ArgumentNullException("methodName", "You tried to invoke a webservice without specifying the WebMethod."); } MemberInfo info = typeof(ResultType); var contract = ((WsContractAttribute)info.GetCustomAttributes(typeof(WsContractAttribute), true).FirstOrDefault()); if (string.IsNullOrEmpty(Url) && contract == null) { throw new MethodAccessException("You tried to invoke a webservice without specifying the WebService's Contract/URL."); } ValidateContract(contract); Result = new WebServiceResult(); ResponseMappers.GetMapperAttributes(typeof(ResultType)); this.InvokeService(methodName, encode, soapActionComplement); return(ExtractResultClass <ResultType>(methodName)); } finally { CleanLastInvoke(); } }
private void CleanLastInvoke() { Url = DefaultUrl; Namespace = DefaultNamespace; Parameters.Clear(); ResponseMappers.Clear(); ParametersMappers.Clear(); }
private ResultType ExtractResultClass <ResultType>(string methodName) { if (typeof(ResultType) == typeof(object)) { return(default(ResultType)); } var methodNameResult = methodName + "Result"; if (!Result.StringResult.Contains(methodNameResult)) { return((ResultType)Convert.ChangeType(Result.StringResult, typeof(ResultType))); } var xmlMapper = Result.StringResult.Replace(methodNameResult, typeof(ResultType).Name); xmlMapper = ResponseMappers.ApplyMappers(xmlMapper); XmlSerializer serializer = new XmlSerializer(typeof(ResultType)); var rdr = new StringReader(xmlMapper); var result = (ResultType)serializer.Deserialize(rdr); rdr.Close(); return(result); }