Example #1
0
        ActionResult Invoke(ServiceAction action, IDictionary <string, string> arguments,
                            Encoding encoding, int retry, bool isFallback)
        {
            // Because we may do several fallbacks, we serialize to a memory stream and copy that
            // to the network stream. That way we only have to serialize once (unless we have to
            // try a different encoding scheme).
            using (var stream = new MemoryStream()) {
                try {
                    var headers  = new WebHeaderCollection();
                    var settings = new XmlWriterSettings {
                        Encoding = encoding
                    };
                    using (var writer = XmlWriter.Create(stream, settings)) {
                        action.SerializeRequest(arguments, headers, writer);
                    }

                    using (var response = GetResponse(action, headers, retry, stream)) {
                        if (response.StatusCode == HttpStatusCode.OK)
                        {
                            return(action.DeserializeResponse(response));
                        }
                        else if (response.StatusCode == HttpStatusCode.InternalServerError)
                        {
                            if (isFallback)
                            {
                                action.DeserializeResponseFault(response);
                            }
                            return(null);
                        }
                        else if (response.StatusCode == HttpStatusCode.BadRequest)
                        {
                            return(null);
                        }
                        else
                        {
                            throw new UpnpException(string.Format(
                                                        "There was an unknown error while invoking the action: " +
                                                        "the service returned status code {0}.", response.StatusCode));
                        }
                    }
                } catch (Exception e) {
                    throw new UpnpException("There was an unknown error while invoking the action.", e);
                }
            }
        }