/// <summary>
 /// Handle the server response.
 /// </summary>
 /// <param name="request">The request messages.</param>
 /// <param name="response">The response messages.</param>
 /// <param name="isSchemaValidated">Whether the schema is validated.</param>
 protected void ExchangeServiceBinding_ResponseEvent(
     BaseRequestType request,
     BaseResponseMessageType response,
     bool isSchemaValidated)
 {
     this.IsSchemaValidated = isSchemaValidated;
 }
        public void Test()
        {
            if (AisgPortClient != null)
            {
                try
                {
                    BaseRequestType type = new BaseRequestType
                    {
                        CisloPozadavku = Guid.NewGuid().ToString(),
                        ICO_VCP        = ConfigurationManager.AppSettings.Get("icovcp").ToString()
                    };

                    fileSerializer.SaveAsJson(type, paramsItem.OutputJSONRequestFilename);

                    var response = AisgPortClient.Test(type);
                    fileSerializer.SaveAsJson(response, paramsItem.OutputJSONResponseFilename);
                }
                catch (FaultException <ErrorMessageType> errMsg)
                {
                    Console.WriteLine($"{errMsg.Detail.Kod}: {errMsg.Detail.Popis}");
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }
        }
 /// <summary>
 /// Handle the server response.
 /// </summary>
 /// <param name="request">The request messages.</param>
 /// <param name="response">The response messages.</param>
 /// <param name="isSchemaValidated">Whether the schema is validated.</param>
 protected void ExchangeServiceBinding_ResponseEvent(
     BaseRequestType request,
     BaseResponseMessageType response,
     bool isSchemaValidated)
 {
     this.IsSchemaValidated = isSchemaValidated;
 }
        /// <summary>
        /// Handle the server response.
        /// </summary>
        /// <param name="request">The request messages.</param>
        /// <param name="response">The response messages.</param>
        /// <param name="isSchemaValidated">Verify the schema.</param>
        private void ExchangeServiceBinding_ResponseEvent(
            BaseRequestType request,
            BaseResponseMessageType response,
            bool isSchemaValidated)
        {
            this.IsSchemaValidated = isSchemaValidated;

            // A flag represents the response contains the item information or not.
            bool hasItemInfo = false;
            if (response is SetUserPhotoResponseMessageType)
            {
                return;
            }

            foreach (ResponseMessageType responseMessage in response.ResponseMessages.Items)
            {
                if (responseMessage is ItemInfoResponseMessageType)
                {
                    hasItemInfo = true;
                    break;
                }
            }

            BaseItemIdType[] itemIds;
            if (hasItemInfo)
            {
                itemIds = Common.GetItemIdsFromInfoResponse(response);
            }
            else
            {
                itemIds = new BaseItemIdType[0];
                return;
            }

            foreach (ItemIdType itemId in itemIds)
            {
                bool notExist = true;
                foreach (ItemIdType exist in this.ExistContactItems)
                {
                    if (exist.Id == itemId.Id)
                    {
                        // Check if the Id of the item in response exists in previous recorded item list.
                        notExist = false;
                        break;
                    }
                }

                // If the Id of Item is not exist, add the item into item list.
                if (notExist)
                {
                    this.ExistContactItems.Add(itemId);
                }
            }
        }
        /// <summary>
        /// Overload .NET framework Invoke method to provide extra XML schema validation function.
        /// </summary>
        /// <param name="methodName">The name of the XML Web service method.</param>
        /// <param name="parameters">An array of objects that contains the parameters to pass to the XML Web service. The order of the values in the array corresponds to the order of the parameters in the calling method of the derived class.</param>
        /// <returns>An array of objects that contains the return value and any reference or out parameters of the derived class method.</returns>
        public new object[] Invoke(string methodName, object[] parameters)
        {
            this.rawRequestXml  = null;
            this.rawResponseXml = null;
            WebResponse webResponse = null;
            WebRequest  webRequest  = null;

            object[]      objArray;
            StringBuilder sb = new StringBuilder();

            using (StringWriter sw = new StringWriter(sb))
            {
                Assembly        assembly    = Assembly.GetAssembly(typeof(ItemType));
                BaseRequestType requestInfo = (BaseRequestType)parameters[0];
                Type            type        = typeof(BaseRequestType);
                Type[]          types       = assembly.GetTypes();
                foreach (Type t in types)
                {
                    if (requestInfo.GetType().Equals(t))
                    {
                        type = t;
                    }
                }

                // Serialize the request.
                XmlSerializer xs = new XmlSerializer(type);
                xs.Serialize(sw, requestInfo);
            }

            try
            {
                // Creates a WebRequest for the specified uri.
                webRequest = this.GetWebRequest(this.baseURI.GetValue(this, null) as Uri);
                webRequest.PreAuthenticate = true;

                // Check PropertyInfo is null or not.
                Trace.Assert(this.basePendingSyncRequest != null, "PropertyInfo can not be NULL");

                // Sets the value of PendingSyncRequest.
                this.basePendingSyncRequest.SetValue(this, webRequest, null);

                // Invoke method of HttpWebClientProtocol.
                SoapClientMessage message       = this.InstanceInvokeBase("BeforeSerialize", webRequest, methodName, parameters) as SoapClientMessage;
                Stream            requestStream = webRequest.GetRequestStream();
                try
                {
                    Trace.Assert(this.setStream != null, "MethodInfo can not be NULL");
                    this.setStream.Invoke(message, new object[] { requestStream });
                    this.InstanceInvokeBase("Serialize", message);
                }
                finally
                {
                    requestStream.Close();
                }

                // Get the actual request xml by using xmlWriterHookInstance. The xmlWriterHookInstance is appended to the GetWriterForMessage method of the proxy class.
                string requestXmlString = this.xmlWriterHookInstance.Xml.ToString();

                // Load the actual request xml to an XmlElement
                if (!string.IsNullOrEmpty(requestXmlString))
                {
                    XmlDocument xmlDocOfReadRequest = new XmlDocument();
                    xmlDocOfReadRequest.LoadXml(requestXmlString);
                    this.rawRequestXml = xmlDocOfReadRequest.DocumentElement;
                    this.site.Log.Add(LogEntryKind.Debug, "The raw xml request message is:\r\n{0}", ((XmlElement)this.rawRequestXml).OuterXml);
                }

                webResponse = this.GetWebResponse(webRequest);
                HttpWebResponse httpWebResponse = (HttpWebResponse)webResponse;
                if (httpWebResponse.StatusCode != HttpStatusCode.OK)
                {
                    throw new WebException(httpWebResponse.StatusDescription);
                }

                Stream responseStream = null;
                try
                {
                    responseStream = webResponse.GetResponseStream();
                    string streamString = string.Empty;
                    using (StreamReader sr = new StreamReader(responseStream))
                    {
                        responseStream = null;

                        StringBuilder xmlString = new StringBuilder();
                        while (sr.Peek() > -1)
                        {
                            string strInput = sr.ReadLine();
                            xmlString.Append(strInput);
                        }

                        streamString = xmlString.ToString();
                        Trace.TraceInformation(streamString);
                    }

                    using (Stream streamObjRawXmlResponse = new MemoryStream(ASCIIEncoding.Default.GetBytes(streamString)))
                    {
                        XmlDocument responseXml = new XmlDocument();
                        responseXml.LoadXml(streamString);
                        this.rawResponseXml = responseXml.DocumentElement;
                        this.site.Log.Add(LogEntryKind.Debug, "The raw xml response message is:\r\n{0}", ((XmlElement)this.rawResponseXml).OuterXml);

                        objArray = this.InstanceInvoke("ReadResponse", message, webResponse, streamObjRawXmlResponse, false) as object[];

                        // Gets SOAP header from the response.
                        string soapHeader = this.GetSoapElement(responseXml, "Header");

                        // Gets SOAP body from the response.
                        string soapBody = this.GetSoapElement(responseXml, "Body");

                        this.XmlValidater(soapHeader, soapBody);

                        if (ExchangeServiceBinding.ServiceResponseEvent != null)
                        {
                            if (objArray[0] is BaseResponseMessageType)
                            {
                                ExchangeServiceBinding.ServiceResponseEvent(
                                    (BaseRequestType)parameters[0],
                                    (BaseResponseMessageType)objArray[0],
                                    this.IsSchemaValidated);
                            }
                        }
                    }
                }
                catch (XmlException exception)
                {
                    if (exception.Message.Contains("The following elements are not closed"))
                    {
                        throw new InvalidOperationException("The xml is not complete.", exception);
                    }
                    else
                    {
                        throw new InvalidOperationException("WebResponseBadXml", exception);
                    }
                }
                finally
                {
                    if (responseStream != null)
                    {
                        responseStream.Dispose();
                    }
                }
            }
            finally
            {
                if (webRequest == this.basePendingSyncRequest.GetValue(this, null) as WebRequest)
                {
                    if (this.basePendingSyncRequest.CanWrite)
                    {
                        this.basePendingSyncRequest.SetValue(this, null, null);
                    }
                }
            }

            return(objArray);
        }