Error picking web service return variable

Asked

Viewed 60 times

2

I have a variable that I return that is of the type x509Certificate2, But once you get back into the system, it always comes null, but from the webserver, it returns perfectly, I’m not sure how to convert. I created a class with the same name, and the same variable, and I do so:

 ResultadoCert a = new ResultadoCert(); 
                    a.Certificado = (await client.CertificadoAsync(item.Serial));

It returns the following error:

Cannot convert type implicitly "Servicereference1.Resultadocert" in "System.Security.Cryptography.X509certificates.X509certificate2"

How can I fix it? I have tried to collect on var, and pass to x509Certificate2 but he comes null

EDIT

That’s the class to receive, I changed a little now:

public class ResultadoCert {
    [IgnoreDataMember]
    public X509Certificate2 Certificado { get; set; }
}

And here’s what I return from webservice

 public ResultadoCert Certificado(string serial)
    {
        ResultadoCert resultado = new ResultadoCert();
        X509Certificate2Collection collection = Certificados.SelecionarCertificado(serial);

        if (collection.Count == 1)
        {
            resultado.Certificado = collection[0];
            resultado.Result = true;
            return resultado;
        }
        else { resultado.Result = false; resultado.Certificado = null; return resultado; }
    }

Here’s how I’m trying to get paid:

 ResultadoCert a = new ResultadoCert();
                    var cert = await client.CertificadoAsync(item.Serial);
                    if (cert.Result)
                    {
                        a.Certificado = cert.Certificado;
                        //a.Certificado = client.CertificadoAsync(item.Serial).Result.Certificado;
                    }

Debugging in webservice, it returns correctly, but when I pass the feedback I’m having problems.

  • i don’t know how your code is, but could you check two things ? 1º - Make sure you’re on Client Right, I imagine you’ve done it a thousand times, but just to confirm. 2º - Make sure the return of your method is a Task<x509Certificate2>, for methods Async We need to put this in the return so that it can come properly. If you can show all the code to us, your method, your class, the type, and especially the return, it would help the community a lot to help you.

  • @Richardwillian edited the question.

1 answer

2

@marianac_costa, there on your Return of the Web Service, you see that your return is Resultadocert then, there would be no way you could assign directly to your object a.Certificado, because it’s the same as you force your Certificado turn into a ResultadoCert.

To fix this, you should change the Return of its method in the Service, or return the Resultadocert even. It would look like this:

Returning to Resultadocert

public Task<ResultadoCert> Certificado(string serial)
{
        ResultadoCert resultado = new ResultadoCert();
        X509Certificate2Collection collection = 
        Certificados.SelecionarCertificado(serial);

        if (collection.Count == 1)
        {
            resultado.Certificado = collection[0];
            resultado.Result = true;
            return resultado;
        }
        else 
        { 
          resultado.Result = false; resultado.Certificado = null; 
          return resultado; 
        }
}

Obs: Note that I added the Task<Objeto>, this would solve the problem of being returning nullwhen the variable is of the type var(that you had commented).

Service Call (Returning Resultadocert)

ResultadoCert a = new ResultadoCert();
var resultadoCertoRetornado = await client.CertificadoAsync(item.Serial);
if (resultadoCertoRetornado.Certificado.Result)
{
    a.Certificado = await resultadoCertoRetornado.Certificado;
}

Returning Certificate

public Task<Certificado> Certificado(string serial)
    {
            ResultadoCert resultado = new ResultadoCert();
            X509Certificate2Collection collection = 
            Certificados.SelecionarCertificado(serial);

            if (collection.Count == 1)
            {
                return collection[0];
            }
            else 
            { 
              return null; 
            }
    }

Service Call (Returning Certificate)

ResultadoCert a = new ResultadoCert();

var certificadoRetornadaService = await client.CertificadoAsync(item.Serial);
if (certificadoRetornadaService.IsNotNull())
{
        a.Certificado = await certificadoRetornadaService;
}
  • He is returning me the following error as a result. Cannot implicitly convert "Wsmoduloseg.Uteis.Resultadocert" to "System.Threading.Tasks.Task<Wsmoduloseg.Uteis.Resultcert>"

  • @marianac_costa, N method call, in object assignment, puts await, like this: a.Certificado = await certificadoRetornadaService; I will update the response.

  • I did so, it does not even enter the return of the certificate, it already returns the error An Exception was thrown while attempting to evaluate a LINQ query Parameter Expression. To show Additional information call Enablesensitivedatalogging() when Overriding Dbcontext.OnConfiguring. --> System.Nullreferenceexception: Object Reference not set to an instance of an Object. lambat da_method(Closure )

  • And I have a method before, that the return works, this gives error.

  • Gosh, I don’t understand so, like, your initial problem was just because in your method, you were returning ResultadoCert and you were assigning to a Certificado. The same as trying to assign a int in a string. But then you said you put var to return anything, and came null. And the Task<>was supposed to fix it. Weird, I don’t know what can be, sorry @marianac_costa

  • In the webservice it returns normal, no error occurs, and at breakpoint, you can see that it is getting right, and in the system, it returns error when serialize, I tried several ways, but none of the right.

  • You have no other method that works and can return ? Because now analyzing your class, I have not seen the DataAnnotation [Serializable], don’t need to ? Or you just don’t show up in your question ?

  • It’s because I tried to put it on but it didn’t work either, so I did the test without

  • This method here, Certificados.SelecionarCertificado(serial); does a database query? Is that you said you are returning the data correctly, but this kind of problem DbContextis related to Entity Framework, ORM, etc. WebService, then it is only a doubt of conscience disengagement.

  • No, it returns the certificates on the machine, I use the same method p sign and it works, only in this one when I just search and need to return the certificate that does not work

  • I discovered that the problem ta when passing the variable x509Certificate2, when passing only another common, it worked, only the certificate that.

  • Good, I’m glad you made it, and what’s wrong with this class x509Certificate2, would you say ? I’m curious now.

  • I haven’t figured it out yet, I’m trying to see what it can be, why he doesn’t get the data from her, the guy being the same.

  • @marianac_costa, when you changed the property type, you removed the [IgnoreDataMember]from above the property ? Test next time with the [DataMember] and uses its class x509Certificate2.

  • I tried and still did not give, when researching I saw that it is of a complex type, but if the return is of the same type, should pass without problems.

  • I couldn’t return the type x509Certificate2, So I wouldn’t waste too much time, I had to change the part where I got it, so it didn’t have to be passed. If you find any alternative in the future I’ll return to answer. Thank you for your help Richard.

  • I couldn’t help much, but no kkk. I’d just like to ask you to put this classe here in the comments for me to take a look at her ? Try to put her whole if possible. And if you find the answer in the future, please tell us, because this was complicated kkkk

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.