【问题标题】:Can't consume asp.net webservice localhost C#无法使用 asp.net webservice localhost C#
【发布时间】:2021-03-29 15:46:27
【问题描述】:

我正在做一门关于 C# 中 web 服务的课程,课程的一部分是在 IIS localhost 中设置 web 服务,一旦它工作,我打开一个新项目并创建一个客户端,进行引用,它正确引用,并且当我尝试使用 WS 中的方法时,它显示了这个错误:

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 parametros。 zh System.Xml.Serialization.TypeDesc.CheckSupported() en System.Xml.Serialization.TypeScope.GetTypeDesc(类型类型,MemberInfo 源,布尔型 directReference,布尔型 throwOnError) zh System.Xml.Serialization.ReflectionAwareILGen.WriteReflectionInit(TypeScope 范围) zh 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) zh System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] 映射,类型类型) zh System.Web.Services.Protocols.SoapServerType..ctor(类型类型,WebServiceProtocols 协议支持) zh System.Web.Services.Protocols.SoapServerProtocol.Initialize() en System.Web.Services.Protocols.ServerProtocolFactory.Create(类型类型,HttpContext 上下文,HttpRequest 请求,HttpResponse 响应,Boolean& abortProcessing) --- Fin del seguimiento de la pila de la excepción interna ---'

【问题讨论】:

  • 您使用的是什么版本的 C#,课程使用的是什么?我最近没有做太多事情,但是我在从 4.x 转换到 .NET Core 时遇到了一些问题,因为生成器没有使用正确的命名空间或其他东西(我的记忆有点模糊)。
  • net framework 4.7.2,课程显示那个版本,是我正在使用的版本
  • 你能分享代码/课程吗?它说没有无参数构造函数,这意味着它具有的构造函数需要传入参数,但您没有传递任何参数。如果您查看定义/实现,它应该向您显示可用构造函数的列表。在带有 VS Code 的 Windows 上,这对我来说是 F12 或 CTRL + F12。

标签: c# localhost local consumption


【解决方案1】:

这是我的代码:

[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;
        }
    }

当发布2个webMethod并尝试调用其中一个时,错误显示,似乎我需要调用webservice中的所有方法,如果我从第二个中删除“WebMethod”,它会正确调用HelloWorld方法,你呢知道如何在发布 2 个或多个时只调用其中一个方法吗?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-07
    • 2013-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 2017-12-11
    相关资源
    最近更新 更多