如何解决无法使用 asp.net webservice localhost C#
我正在用 C# 做一个关于 webservices 的课程,课程的一部分是在 IIS localhost 中设置 webservice,一旦它工作,我打开一个新项目并创建一个客户端,进行引用,它正确引用,并且当我尝试从 WS 使用该方法时,它向我显示此错误:
System.ServiceModel.FaultException: 'System.Web.Services.Protocols.soapException: El servidor no puede procesar la seekud。 ---> system.invalidOperationException: No se puede serializar System.Security.Cryptography.KeySizes porque no tiene un constructor sin 参数。 zh-cn System.Xml.Serialization.TypeDesc.CheckSupported() zh System.Xml.Serialization.TypeScope.GetTypeDesc(Type type,MemberInfo source,Boolean directReference,Boolean throwOnError) zh System.Xml.Serialization.ReflectionAwareILGen.WriteReflectionInit(TypeScope scope) zh System.Xml.Serialization.XmlSerializationWriterILGen.GenerateBegin() zh-cn System.Xml.Serialization.TempAssembly.GenerateRefEmitAssembly(XmlMapping[] xmlMappings,Type[] types,String defaultNamespace,Evidence evidence) en System.Xml.Serialization.TempAssembly..ctor(XmlMapping[] xmlMappings,String location,Evidence evidence) zh_cn System.Xml.Serialization.XmlSerializer.FromMappings(XmlMapping[] mappings,Type type) en System.Web.Services.Protocols.soapServerType..ctor(类型类型,WebServiceProtocols 协议支持) zh 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 ---'
解决方法
这是我的代码:
[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 个或更多方法时仅调用其中一种方法吗?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。