Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Olá.
Estou consumindo o WS da SeFaz de MG no Visual Studio 2008.
Ao ser realizado o consumo, é criado por padrão o código (passarei apenas o de autorizacao pois é o que está apresentando dificuldade, focando em "public object[] NfeAutorizacaoLote"):
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.1433
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
//
// This source code was auto-generated by Microsoft.VSDesigner, Version 2.0.50727.1433.
//
#pragma warning disable 1591
namespace BibliotecaNFe.MG.NfeAutorizacao.Homologacao {
using System.Diagnostics;
using System.Web.Services;
using System.ComponentModel;
using System.Web.Services.Protocols;
using System;
using System.Xml.Serialization;
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="NfeAutorizacaoSoapBinding", Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
public partial class NfeAutorizacao : System.Web.Services.Protocols.SoapHttpClientProtocol {
private nfeCabecMsg nfeCabecMsgValueField;
private System.Threading.SendOrPostCallback NfeAutorizacaoLoteOperationCompleted;
private System.Threading.SendOrPostCallback NfeAutorizacaoLoteZipOperationCompleted;
private bool useDefaultCredentialsSetExplicitly;
/// <remarks/>
public NfeAutorizacao() {
this.SoapVersion = System.Web.Services.Protocols.SoapProtocolVersion.Soap12;
this.Url = global::BibliotecaNFe.Properties.Settings.Default.BibliotecaNFe_MG_NfeAutorizacao_Homologacao_NfeAutorizacao;
if ((this.IsLocalFileSystemWebService(this.Url) == true)) {
this.UseDefaultCredentials = true;
this.useDefaultCredentialsSetExplicitly = false;
}
else {
this.useDefaultCredentialsSetExplicitly = true;
}
}
public nfeCabecMsg nfeCabecMsgValue {
get {
return this.nfeCabecMsgValueField;
}
set {
this.nfeCabecMsgValueField = value;
}
}
public new string Url {
get {
return base.Url;
}
set {
if ((((this.IsLocalFileSystemWebService(base.Url) == true)
&& (this.useDefaultCredentialsSetExplicitly == false))
&& (this.IsLocalFileSystemWebService(value) == false))) {
base.UseDefaultCredentials = false;
}
base.Url = value;
}
}
public new bool UseDefaultCredentials {
get {
return base.UseDefaultCredentials;
}
set {
base.UseDefaultCredentials = value;
this.useDefaultCredentialsSetExplicitly = true;
}
}
/// <remarks/>
public event NfeAutorizacaoLoteCompletedEventHandler NfeAutorizacaoLoteCompleted;
/// <remarks/>
public event NfeAutorizacaoLoteZipCompletedEventHandler NfeAutorizacaoLoteZipCompleted;
/// <remarks/>
[System.Web.Services.Protocols.SoapHeaderAttribute("nfeCabecMsgValue", Direction=System.Web.Services.Protocols.SoapHeaderDirection.InOut)]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao/NfeAutorizacaoLote", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
[return: System.Xml.Serialization.XmlArrayAttribute("nfeAutorizacaoLoteResult", Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
[return: System.Xml.Serialization.XmlArrayItemAttribute("retEnviNFe", IsNullable=false)]
public object[] NfeAutorizacaoLote([System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")] System.Xml.XmlNode nfeDadosMsg) {
object[] results = this.Invoke("NfeAutorizacaoLote", new object[] {
nfeDadosMsg});
return ((object[])(results[0]));
}
/// <remarks/>
public void NfeAutorizacaoLoteAsync(nfeDadosMsg nfeDadosMsg) {
this.NfeAutorizacaoLoteAsync(nfeDadosMsg, null);
}
/// <remarks/>
public void NfeAutorizacaoLoteAsync(nfeDadosMsg nfeDadosMsg, object userState) {
if ((this.NfeAutorizacaoLoteOperationCompleted == null)) {
this.NfeAutorizacaoLoteOperationCompleted = new System.Threading.SendOrPostCallback(this.OnNfeAutorizacaoLoteOperationCompleted);
}
this.InvokeAsync("NfeAutorizacaoLote", new object[] {
nfeDadosMsg}, this.NfeAutorizacaoLoteOperationCompleted, userState);
}
private void OnNfeAutorizacaoLoteOperationCompleted(object arg) {
if ((this.NfeAutorizacaoLoteCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.NfeAutorizacaoLoteCompleted(this, new NfeAutorizacaoLoteCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
[System.Web.Services.Protocols.SoapHeaderAttribute("nfeCabecMsgValue", Direction=System.Web.Services.Protocols.SoapHeaderDirection.InOut)]
[System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao/NfeAutorizacaoLoteZip", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Bare)]
[return: System.Xml.Serialization.XmlArrayAttribute("nfeAutorizacaoLoteResult", Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
[return: System.Xml.Serialization.XmlArrayItemAttribute("retEnviNFe", IsNullable=false)]
public object[] NfeAutorizacaoLoteZip([System.Xml.Serialization.XmlElementAttribute(Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")] nfeDadosMsgZip nfeDadosMsgZip) {
object[] results = this.Invoke("NfeAutorizacaoLoteZip", new object[] {
nfeDadosMsgZip});
return ((object[])(results[0]));
}
/// <remarks/>
public void NfeAutorizacaoLoteZipAsync(nfeDadosMsgZip nfeDadosMsgZip) {
this.NfeAutorizacaoLoteZipAsync(nfeDadosMsgZip, null);
}
/// <remarks/>
public void NfeAutorizacaoLoteZipAsync(nfeDadosMsgZip nfeDadosMsgZip, object userState) {
if ((this.NfeAutorizacaoLoteZipOperationCompleted == null)) {
this.NfeAutorizacaoLoteZipOperationCompleted = new System.Threading.SendOrPostCallback(this.OnNfeAutorizacaoLoteZipOperationCompleted);
}
this.InvokeAsync("NfeAutorizacaoLoteZip", new object[] {
nfeDadosMsgZip}, this.NfeAutorizacaoLoteZipOperationCompleted, userState);
}
private void OnNfeAutorizacaoLoteZipOperationCompleted(object arg) {
if ((this.NfeAutorizacaoLoteZipCompleted != null)) {
System.Web.Services.Protocols.InvokeCompletedEventArgs invokeArgs = ((System.Web.Services.Protocols.InvokeCompletedEventArgs)(arg));
this.NfeAutorizacaoLoteZipCompleted(this, new NfeAutorizacaoLoteZipCompletedEventArgs(invokeArgs.Results, invokeArgs.Error, invokeArgs.Cancelled, invokeArgs.UserState));
}
}
/// <remarks/>
public new void CancelAsync(object userState) {
base.CancelAsync(userState);
}
private bool IsLocalFileSystemWebService(string url) {
if (((url == null)
|| (url == string.Empty))) {
return false;
}
System.Uri wsUri = new System.Uri(url);
if (((wsUri.Port >= 1024)
&& (string.Compare(wsUri.Host, "localHost", System.StringComparison.OrdinalIgnoreCase) == 0))) {
return true;
}
return false;
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao", IsNullable=false)]
public partial class nfeCabecMsg : System.Web.Services.Protocols.SoapHeader {
private string cUFField;
private string versaoDadosField;
private System.Xml.XmlAttribute[] anyAttrField;
/// <remarks/>
public string cUF {
get {
return this.cUFField;
}
set {
this.cUFField = value;
}
}
/// <remarks/>
public string versaoDados {
get {
return this.versaoDadosField;
}
set {
this.versaoDadosField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlAnyAttributeAttribute()]
public System.Xml.XmlAttribute[] AnyAttr {
get {
return this.anyAttrField;
}
set {
this.anyAttrField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
public partial class nfeDadosMsg {
private System.Xml.XmlNode[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[] Any {
get {
return this.anyField;
}
set {
this.anyField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "2.0.50727.1433")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")]
public partial class nfeDadosMsgZip {
private System.Xml.XmlNode[] anyField;
/// <remarks/>
[System.Xml.Serialization.XmlTextAttribute()]
[System.Xml.Serialization.XmlAnyElementAttribute()]
public System.Xml.XmlNode[] Any {
get {
return this.anyField;
}
set {
this.anyField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
public delegate void NfeAutorizacaoLoteCompletedEventHandler(object sender, NfeAutorizacaoLoteCompletedEventArgs e);
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class NfeAutorizacaoLoteCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
private object[] results;
internal NfeAutorizacaoLoteCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState) {
this.results = results;
}
/// <remarks/>
public object[] Result {
get {
this.RaiseExceptionIfNecessary();
return ((object[])(this.results[0]));
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
public delegate void NfeAutorizacaoLoteZipCompletedEventHandler(object sender, NfeAutorizacaoLoteZipCompletedEventArgs e);
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Web.Services", "2.0.50727.1433")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
public partial class NfeAutorizacaoLoteZipCompletedEventArgs : System.ComponentModel.AsyncCompletedEventArgs {
private object[] results;
internal NfeAutorizacaoLoteZipCompletedEventArgs(object[] results, System.Exception exception, bool cancelled, object userState) :
base(exception, cancelled, userState) {
this.results = results;
}
/// <remarks/>
public object[] Result {
get {
this.RaiseExceptionIfNecessary();
return ((object[])(this.results[0]));
}
}
}
}
#pragma warning restore 1591
Em nossa chamada para essa operação na nossa classe, utilizamos o seguinte código:
public string NFeAutorizacao(string XmlEnvio, string VersaoSchema, short Ambiente, short CodIbgeUfEmi, string SiglaWS, string PathCertificado, string SenhaCertificado, ref string MsgErr, ref short FlagErr)
{
try
{
// Converte dados do xml para XmlNode
XmlDocument oXmlDoc = new XmlDocument();
oXmlDoc.LoadXml(XmlEnvio);
XmlNode oNode = oXmlDoc.DocumentElement;
// Carrega o certificado
X509Certificate certificado = new X509Certificate();
certificado = SelecionarCertificado(PathCertificado, SenhaCertificado, ref MsgErr, ref FlagErr);
// Se conseguiu localizar o certificado digital
if (FlagErr == 0)
{
// Identifica o WebService a ser utilizado: "SEFAZ Estadual", "Ambiente Nacional" ou "SCAN"
switch (SiglaWS)
{
case "SEFAZEST": // WebService da SEFAZ Estadual ou Virtual
// Identifica o ambiente a ser utilizado
switch (Ambiente)
//Ambiente de Homologação
#region
case 2: // HOMOLOGAÇÃO
// Identifica o código IBGE do Estado
switch (CodIbgeUfEmi)
{
case 31: // MG
// Declara as variáveis do WebService
MG.NfeAutorizacao.Homologacao.NfeAutorizacao wsMG = new BibliotecaNFe.MG.NfeAutorizacao.Homologacao.NfeAutorizacao();
MG.NfeAutorizacao.Homologacao.nfeCabecMsg nfeCabecMsgMG = new BibliotecaNFe.MG.NfeAutorizacao.Homologacao.nfeCabecMsg();
MG.NfeAutorizacao.Homologacao.nfeDadosMsg nfeDadosMsgMG = new BibliotecaNFe.MG.NfeAutorizacao.Homologacao.nfeDadosMsg();
// Coloca os valores no cabeçalho
nfeCabecMsgMG.cUF = CodIbgeUfEmi.ToString().Trim();
nfeCabecMsgMG.versaoDados = VersaoSchema;
wsMG.nfeCabecMsgValue = nfeCabecMsgMG;
// Coloca o certificado
wsMG.ClientCertificates.Add(certificado);
wsMG.Timeout = 120000;
// Comunica com o WebService
return wsMG.NfeAutorizacaoLote(nfeDadosMsgMG.Any[0]).ToString();
Cortei o código pela metade pois essa parte que irá nos interessar!
O problema é o seguinte:
Originalmente, o código montado pelo Visual Studio esta com a linha de NFeAutorizacaoLote como:
public object[] NfeAutorizacaoLote(system.Xml.Serialization.XmlElementAttribute(Namespace="[http://www.portalfiscal.inf.br/nfe/wsdl/NfeAutorizacao")] nfeDadosMsg nfeDadosMsg)
Foi alterado para "System.Xml.XmlNode nfeDadosMsg)"
E na classe que criamos, o return estava como:
**return wsGO.nfeAutorizacaoLote(oNode).OuterXml;**
Ao realizar os ajustes, o Visual Studio não apresentou nenhum tipo de erro ou mensagem, porém ao pedir para realizar a autorização de uma nota em homologação, ele retorna o seguinte erro:
"Erro: Não foi possível enviar o lote de NF-e à SEFAZ. Referência de objeto não definida para uma instância de um objeto."
Gostaria que vocês me ajudassem a enxergar o que não está sendo instanciado, pois já estou quebrando a cabeça com esse retorno!
Se necessitarem de mais informações me avisem!
Agradeço desde já.
Carregando comentários...