简体   繁体   中英

Passing file data to C# WCF Service function (Error 400 Bad Request)

istruzioniService.caricaDocumentoCompilato(formData)
                .then(function(res) {
                        if (res.Esito == true) {
                            console.log('allegaFileCompilato : OK ')
                        } else {
                            console.log('allegaFileCompilato : ERRORE Esito = false')
                        }

                    },
                    function(error) {
                        console.log('allegaFileCompilato : ERRORE - ' + error.body)
                    });
        }

result.caricaDocumentoCompilato = function(_stream) {
            var self = this;
            return $q(
                function(resolve, reject) {
                    try {
                        remoteService
                            .post(EasyWork.SERVER_LINK + '/json/CaricaDocumentoCompilato', { stream: _stream }, true)
                            .then(
                                function(res) {
                                    resolve(res);
                                },
                                function(error) {
                                    console.error('caricaDocumentoCompilato ERRORE -> ' + error.message);
                                    reject(error);
                                }
                            )
                    } catch (error) {
                        console.error('caricaDocumentoCompilato ERRORE -> ' + error.message);
                        reject(error);
                    }
                }
            );

        }

public EsitoOperazione CaricaDocumentoCompilato(Stream stream)
        {
            using (TransactionScope())
            {
                try
                {

                    BinaryReader reader = new BinaryReader(stream);
                    byte[] photo = reader.ReadBytes((int)stream.Length);
                    reader.Close();

                    // CODE

                    var esito = new EsitoOperazione();
                    esito.Esito = true;
                    esito.Messaggio = "Documento caricato correttamente";
                    return esito;
                }
                catch (Exception e)
                {
                    var esito = new EsitoOperazione();
                    esito.Esito = false;
                    esito.Messaggio = e.Message;
                    return esito;
                }
            }

        }

Il server ha riscontrato un errore durante l'elaborazione della richiesta. 
Messaggio dell'eccezione: 'Il messaggio in arrivo per l'operazione  
'CaricaDocumentoCompilato' (contratto '' con spazio dei nomi  
'http://tempuri.org/') contiene un valore 'Json' del formato del corpo del messaggio http non riconosciuto.  
Il valore previsto del formato del corpo del messaggio è 'Raw'.  
Ciò può essere dovuto a un WebContentTypeMapper non configurato nell'associazione. Vedere la documentazione di WebContentTypeMapper per maggiori dettagli.'. 
Per ulteriori dettagli, vedere i log del server. Traccia di stack dell'eccezione: 

in System.ServiceModel.Dispatcher.HttpStreamFormatter.GetStreamFromMessage(Message message, Boolean isRequest) 
in System.ServiceModel.Dispatcher.HttpStreamFormatter.DeserializeRequest(Message message, Object[] parameters) 
in System.ServiceModel.Dispatcher.UriTemplateDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) in System.ServiceModel.Dispatcher.CompositeDispatchFormatter.DeserializeRequest(Message message, Object[] parameters) 
in System.ServiceModel.Dispatcher.DispatchOperationRuntime.DeserializeInputs(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc) in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage41(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage4(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage3(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage2(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage11(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage1(MessageRpc& rpc) 
in System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet

Not sure if you declare the binding type in the configuration file, it needs to be explicitly declared, like this:

<endpoint address="" bindingConfiguration="BasicHttpBinding_IServicioSalud" binding="basicHttpBinding" contract="IServicioSalud" />

If set, you can check the WebContentTypeMapper documentation as prompted.

And take a look at other solutions to this error: HTTP Bad Request error when requesting a WCF service contract .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM