Domanda

Ho un servizio Web WCF, che genera eccezioni al momento della presentazione dei dati non validi. I dati vengono inviati tramite un HTTP Post utilizzando l'oggetto WebClient.

Ecco il codice per il servizio Web:

[WebInvoke(UriTemplate = "update", Method = "POST")]
public JsonValue Update(HttpRequestMessage message)
{
    var context = new Entities();
    dynamic response = new JsonObject();

    // in order to retrieve the submitted data easily, reference the data as a dynamic object
    dynamic data = message.Content.ReadAs(typeof(JsonObject), new[] { new FormUrlEncodedMediaTypeFormatter() });

    // retrieve the submitted data
    int requestId = data.requestId;
    int statusId = data.statusId;
    string user = data.user;
    string encryptedToken = data.token;
    string notes = data.notes;

    // retrieve the request with a matching Id
    var request = context.Requests.Find(requestId);

    // make sure the request exists
    if (request == null)
        throw new FaultException("The supplied requestId does not exist.");

    // make sure the submitted encrypted token is valid
    var token = DecryptToken(encryptedToken);
    if (token == null)
        throw new FaultException("Invalid security token.");

    // TODO: Validate other token properties (e.g. email)?
    if (!request.User.UserName.Equals(token.UserName))
        throw new FaultException("Invalid security token.");

    // additional logic removed ...
}

E qui è il codice che invia i dati al servizio Web:

            // use the WebClient object to submit data to the WCF web service
            using (var client = new WebClient())
            {
                client.Encoding = Encoding.UTF8;

                // the data will be submitted in the format of a form submission
                client.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";

                var data = new NameValueCollection();

                // prepare the data to be submitted
                data.Add("requestId", requestId.ToString());
                data.Add("statusId", this.StatusId);
                data.Add("token", token.ToString());
                data.Add("user", this.User);
                data.Add("notes", this.Notes);

                // submit the data to the web service
                var response = client.UploadValues(this.Address, data);
           }

Continuo a ricevere un'eccezione con il messaggio:. "The remote server returned an error: (500) Internal Server Error" a client.UploadValues(this.Address, data);

C'è un modo per fare in modo che le informazioni più dettagliate viene restituito al WebClient?

Inoltre, come posso fare in modo che queste eccezioni (nel servizio WCF) vengono registrati nel EventLog? (Fondamentalmente ho solo bisogno di sapere cosa è successo).

È stato utile?

Soluzione

Date un'occhiata a HttpResponseException (Microsoft.ApplicationServer.Http.Dispatcher namespace) - sono il modo in cui è possibile controllare la risposta per i casi di errore. È possibile specificare il codice di stato, e si ha il controllo del HttpResponseMessage, in cui è possibile controllare il corpo del messaggio.

Sul lato client, quando si chiama WebClient.UploadValues, avvolgere quella chiamata e prendere un WebException. Se restituisce il servizio una risposta con un codice di stato non-successo (ad esempio, 500, 400), la proprietà Response del WebException avrà il corpo, in cui si può leggere nel client.

Un'altra opzione è quella di utilizzare HttpClient al posto del WebClient, nel qual caso si può semplicemente guardare il HttpResponseMessage direttamente.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top