Get Data from Webservice in C#Action

Asked

Viewed 1,099 times

3

I have an application that accesses third-party Webservice. To improve the testing process, I am setting up a website to simulate Webservice.

This site consists of several Actions, which return XML, simulating data. This part is okay.

My problem is that I’m not getting the input parameters, which comes from my application.

Remembering that my application continues with the same structure to send the data to the Webservice, but only with the URL pointing to my site.

I tried to use Request in the controller, and I couldn’t get the data.

Reference generated by C# when importing WSDL

[System.Web.Services.Protocols.SoapRpcMethodAttribute("", RequestNamespace="http://services.teste.com.br", ResponseNamespace="http://services.teste.com.br", Use=System.Web.Services.Description.SoapBindingUse.Literal)]
[return: System.Xml.Serialization.XmlElementAttribute("result")]
public moedasExportarOut Exportar(string user, string password, int encryption, moedasExportarIn parameters) {
    object[] results = this.Invoke("Exportar", new object[] {
                user,
                password,
                encryption,
                parameters});
    return ((moedasExportarOut)(results[0]));
}

Action

[HttpPost]
public ActionResult XML(string user, string password, int? encryption, string methodName,
    Moeda.moedasExportarIn parameters, 
    Moeda.moedasExportarIn arg, int? codEmpField)
{
    XmlDocument xd = new XmlDocument();
    var teste = "";
    teste =
        "<?xml version='1.0' encoding='UTF-8'?> " +
        "<S:Envelope xmlns:S=\"http://schemas.xmlsoap.org/soap/envelope/\"> " +
        "   <S:Body>" +
        "       <ns2:ExportarResponse xmlns:ns2=\"http://services.teste.com.br\">" +
        "           <result>" +
        "               <erroExecucao xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
        "               <finalizaramRegistros>S</finalizaramRegistros>" +
        "               <mensagemRetorno xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:nil=\"true\"/>" +
        "               <numLot>0</numLot>" +
        "               <tipoRetorno>1</tipoRetorno>" +
        "           </result>" +
        "       </ns2:ExportarResponse>" +
        "   </S:Body>" +
        "</S:Envelope>";

    xd.LoadXml(teste);
    return new XmlResult(xd);
}

Call from Webservice, made by my app

public Teste.Moedas.moedasExportarOut ExportarMoeda(DadosParaIntegracao dados, Teste.Moedas.moedasExportarIn parameters)
{
    System.Net.ServicePointManager.DefaultConnectionLimit = conexaoSimultaneaPorServico;
    var service = new Teste.Moedas.g5services();
    service.Url = dados.Configuracao.URL;
    service.Timeout = ConfiguarTimeOut(service, dados.Configuracao);
    ConfigurarProxy(service, dados.Proxy);

    try
    {
        ConfigurarIntegracao(service);
        System.Net.ServicePointManager.Expect100Continue = false;
        return service.Exportar(dados.Login.Login, dados.Login.Senha, 0, parameters);
    }
    finally
    {
        LimparIntegracao();
    }
}

I couldn’t even get the value of user, password or any other parameter.

  • How are you doing to call the Action?

  • @Ciganomorrisonmendez added the form that is called the Webservice

  • Not the Web Service. To Action.

  • @The Action simulates the return of the Webservice. Instead of my URL pointing to Webservice, point to my Action and send the same data if the data was sent to Webservice. In the above code was Controller is actually my Action. I’ve already set.

  • @Ciganomorrisonmendez some idea of how to do?

  • I confess that I still don’t understand how the call chain works. In theory, XML calling for ExportarMoeda, who calls the Webservice that’s right?

Show 1 more comment

1 answer

4


From what I researched, the correct term of what I wanted was mock, which is something to simulate a Webservice. My problem was getting what my Webserver client sends to the server. Searching the internet I found this:

var parametros = new StreamReader(Request.InputStream).ReadToEnd();

In the Request.InputStream had all the request. So I added other features, and I was able to read the data sent, and reply an XML and sinular the Webservice.

Thanks for the personal help.

My complete source was:

public class WSController : Controller
{

    private string RetornaPorta(string parametros)
    {
        try
        {
            var xml = new Regex("<soap:Body>(.*)</soap:Body>").Split(parametros)[1];
            return xml.Split(' ')[0].Substring(1);
        }
        catch
        {
            return "Erro na porta";
        }
    }

    private string RetornaChave(string parametros, ServicoWS servico)
    {
        if (String.IsNullOrEmpty(servico.Chave))
        {
            return "";
        }

        var retorno = "";
        foreach (var item in servico.Chave.Split(';'))
        {
            try
            {
                var valor = new Regex(String.Format("<{0}>(.*)</{0}>", item)).Split(parametros)[1];
                retorno += item + "=" + valor + ";";
            }
            catch
            {

            }
        }
        return retorno.Substring(0, retorno.Length - 1);
    }

    [WebMethod]
    public ActionResult ConfirmarResposta()
    {
        var parametros = new StreamReader(Request.InputStream).ReadToEnd();
        var servico = new ServicoWS();

        using (var db = new Conexao())
        {
            servico = db.ServicoWS.BuscaTipoEPorta("ConfirmarResposta", RetornaPorta(parametros));
            var chave = RetornaChave(parametros, servico);

            var xml = db.ServicoWSRetorno.Where(w =>
                w.ServicoWSID == servico.ServicoWSID &&
                w.Chave == chave).FirstOrDefault();

            XmlDocument xd = new XmlDocument();
            xd.LoadXml(xml.Retorno);
            return new XmlResult(xd);
        }
    }
}

Browser other questions tagged

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