简体   繁体   中英

Can't consume asp.net webservice localhost C#

I'm doing a course about webservices in C#, part of the course is set the webservice in IIS localhost, once it is working, I open a new project and create a client, make the reference, it refers correctly, and when I try to consume the method from the WS, it shows me this error:

enter image description here

System.ServiceModel.FaultException: 'System.Web.Services.Protocols.SoapException: El servidor no puede procesar la solicitud. ---> System.InvalidOperationException: No se puede serializar System.Security.Cryptography.KeySizes porque no tiene un constructor sin parámetros. en System.Xml.Serialization.TypeDesc.CheckSupported() en System.Xml.Serialization.TypeScope.GetTypeDesc(Type type, MemberInfo source, Boolean directReference, Boolean throwOnError) en System.Xml.Serialization.ReflectionAwareILGen.WriteReflectionInit(TypeScope scope) en System.Xml.Serialization.XmlSerializationWriterILGen.GenerateBegin() en System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, Evidence evidence) en System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings, Type[] types, String defaultNamespace, String location, Evidence evidence) en System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings, Type type) en S ystem.Web.Services.Protocols.SoapServerType..ctor(Type type, WebServiceProtocols protocolsSupported) en System.Web.Services.Protocols.SoapServerProtocol.Initialize() en System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- Fin del seguimiento de la pila de la excepción interna ---'

This is my code:

[WebMethod]
    public string HelloWorld()
    {
        return "HelloWorld";
    }

    //-----------------------------------------------------------
    public static String ObtenerTimeStamp(DateTime fechaInicio, DateTime fechaFinal)
    {
        string timeStamp = "<u:Timestamp xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\" u:Id=\"_0\"><u:Created>" + fechaInicio.ToString("yyyy-MM-dd'T'HH:mm:ss") + "</u:Created><u:Expires>" + fechaFinal.ToString("yyyy-MM-dd'T'HH:mm:ss") + "</u:Expires></u:Timestamp>";
        return timeStamp;
    }
    private static string ObtenerSignedInfo(string digestValue)
    {
        return "<SignedInfo xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"></CanonicalizationMethod><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"></SignatureMethod><Reference URI=\"#_0\"><Transforms><Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"></Transform></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"></DigestMethod><DigestValue>" + digestValue + "</DigestValue></Reference></SignedInfo>";
    }
    private static string CreaDigestValue(byte[] bytesTimeStamp)
    {
        try
        {
            byte[] digestValueBytes = Org.BouncyCastle.Security.DigestUtilities.CalculateDigest("sha1", bytesTimeStamp);
            var digestValue = Convert.ToBase64String(digestValueBytes);
            return digestValue;
        }
        catch { return null; }
    }
    private static string ObtenerSoap(string fechaInicio, string fechaFinal, string certificado, string signatureValue, string digestValue)
    {
        String guid = "uuid-b246ed31-bfec-804a-5212-095ac6d97d3c-1";
        String envelope = "<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:u=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd\"><s:Header><o:Security s:mustUnderstand=\"1\" xmlns:o=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd\"><u:Timestamp u:Id=\"_0\"><u:Created>" + fechaInicio + "</u:Created><u:Expires>" + fechaFinal + "</u:Expires></u:Timestamp><o:BinarySecurityToken u:Id=\"" + guid + "\" ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3\" EncodingType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary\">" + certificado + "</o:BinarySecurityToken><Signature xmlns=\"http://www.w3.org/2000/09/xmldsig#\"><SignedInfo><CanonicalizationMethod Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/><SignatureMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#rsa-sha1\"/><Reference URI=\"#_0\"><Transforms><Transform Algorithm=\"http://www.w3.org/2001/10/xml-exc-c14n#\"/></Transforms><DigestMethod Algorithm=\"http://www.w3.org/2000/09/xmldsig#sha1\"/><DigestValue>" + digestValue + "</DigestValue></Reference></SignedInfo><SignatureValue>" + signatureValue + "</SignatureValue><KeyInfo><o:SecurityTokenReference><o:Reference ValueType=\"http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3\" URI=\"" + "#uuid-b246ed31-bfec-804a-5212-095ac6d97d3c-1" + "\"/></o:SecurityTokenReference></KeyInfo></Signature></o:Security></s:Header><s:Body><Autentica xmlns=\"http://DescargaMasivaTerceros.gob.mx\"/></s:Body></s:Envelope>";
        return envelope;
    }
    //----------------------------------------------------------

    [WebMethod]
    public string ConversionXML(DateTime FechaI, DateTime FechaF, X509Certificate2 cert)
    {
        if (FechaI == null && FechaF == null && cert == null)
        {
            return null;
        }
        else
        {
            string timeStamp = ObtenerTimeStamp(FechaI, FechaF);
            var digest = CreaDigestValue(System.Text.Encoding.UTF8.GetBytes(timeStamp));
            var signedInfo = ObtenerSignedInfo(digest);
            var signatureValue = Firma.Firmar(System.Text.Encoding.UTF8.GetBytes(signedInfo), cert);
            var certString = Convert.ToBase64String(cert.GetRawCertData());
            string soap = ObtenerSoap(FechaI.ToString("yyyy-MM-dd'T'HH:mm:ss"), FechaI.AddMinutes(5).ToString("yyyy-MM-dd'T'HH:mm:ss"), certString, signatureValue, digest);
            return soap;
        }
    }

When 2 webMethod published and trying to call one of them, the error shows, It seems I need to call all methods in webservice, if I delete "WebMethod" from the second one, It calls correctly the HelloWorld Method, do you know how can I call only one of the methods when 2 or more published?

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