Condivido anche io a beneficio di tutti gli interessati il codice .NET CORE 5 autogenerato da linea di comando con il tool
dotnet-svcutil SdIRiceviFile_v1.0.wsdl
Il codice è simile a quello di @Antonio_Gabriele perchè ho utilizzato anche io la libreria WcfCoreMtomEncoder
per il MTOM, ma anzichè istanziare una ChannelFactory
ho incluso il set up del CustomBinding
direttamente nel metodo GetBindingForEndpoint
di SdIRiceviFileClient
, e ho invece lasciato il set up del certificato esternamente in InviaFattura
.
Il risultato è il seguente e funziona in .NET CORE 5
//------------------------------------------------------------------------------
// <auto-generated>
// Questo codice è stato generato da uno strumento.
//
// Le modifiche apportate a questo file possono causare un comportamento non corretto e andranno perse se
// il codice viene rigenerato.
// </auto-generated>
//------------------------------------------------------------------------------
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.ServiceModel.ServiceContractAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0", ConfigurationName = "SdIRiceviFile")]
public interface SdIRiceviFile
{
[System.ServiceModel.OperationContractAttribute(Action = "http://www.fatturapa.it/SdIRiceviFile/RiceviFile", ReplyAction = "*")]
[System.ServiceModel.XmlSerializerFormatAttribute(SupportFaults = true)]
System.Threading.Tasks.Task<RiceviFileResponse> RiceviFileAsync(RiceviFileRequest request);
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/types")]
public partial class fileSdIBase_Type
{
private string nomeFileField;
private byte[] fileField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 0)]
public string NomeFile
{
get
{
return this.nomeFileField;
}
set
{
this.nomeFileField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "base64Binary", Order = 1)]
public byte[] File
{
get
{
return this.fileField;
}
set
{
this.fileField = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/types")]
public partial class rispostaSdIRiceviFile_Type
{
private string identificativoSdIField;
private System.DateTime dataOraRicezioneField;
private erroreInvio_Type erroreField;
private bool erroreFieldSpecified;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, DataType = "integer", Order = 0)]
public string IdentificativoSdI
{
get
{
return this.identificativoSdIField;
}
set
{
this.identificativoSdIField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 1)]
public System.DateTime DataOraRicezione
{
get
{
return this.dataOraRicezioneField;
}
set
{
this.dataOraRicezioneField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute(Form = System.Xml.Schema.XmlSchemaForm.Unqualified, Order = 2)]
public erroreInvio_Type Errore
{
get
{
return this.erroreField;
}
set
{
this.erroreField = value;
}
}
/// <remarks/>
[System.Xml.Serialization.XmlIgnoreAttribute()]
public bool ErroreSpecified
{
get
{
return this.erroreFieldSpecified;
}
set
{
this.erroreFieldSpecified = value;
}
}
}
/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/types")]
public enum erroreInvio_Type
{
/// <remarks/>
EI01,
/// <remarks/>
EI02,
/// <remarks/>
EI03,
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class RiceviFileRequest
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/types", Order = 0)]
public fileSdIBase_Type fileSdIAccoglienza;
public RiceviFileRequest()
{
}
public RiceviFileRequest(fileSdIBase_Type fileSdIAccoglienza)
{
this.fileSdIAccoglienza = fileSdIAccoglienza;
}
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
[System.ServiceModel.MessageContractAttribute(IsWrapped = false)]
public partial class RiceviFileResponse
{
[System.ServiceModel.MessageBodyMemberAttribute(Namespace = "http://www.fatturapa.gov.it/sdi/ws/trasmissione/v1.0/types", Order = 0)]
public rispostaSdIRiceviFile_Type rispostaSdIRiceviFile;
public RiceviFileResponse()
{
}
public RiceviFileResponse(rispostaSdIRiceviFile_Type rispostaSdIRiceviFile)
{
this.rispostaSdIRiceviFile = rispostaSdIRiceviFile;
}
}
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
public interface SdIRiceviFileChannel : SdIRiceviFile, System.ServiceModel.IClientChannel
{
}
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.Tools.ServiceModel.Svcutil", "2.0.3")]
public partial class SdIRiceviFileClient : System.ServiceModel.ClientBase<SdIRiceviFile>, SdIRiceviFile
{
/// <summary>
/// Implementare questo metodo parziale per configurare l'endpoint servizio.
/// </summary>
/// <param name="serviceEndpoint">Endpoint da configurare</param>
/// <param name="clientCredentials">Credenziali del client</param>
static partial void ConfigureEndpoint(System.ServiceModel.Description.ServiceEndpoint serviceEndpoint, System.ServiceModel.Description.ClientCredentials clientCredentials);
public SdIRiceviFileClient() :
base(SdIRiceviFileClient.GetDefaultBinding(), SdIRiceviFileClient.GetDefaultEndpointAddress())
{
this.Endpoint.Name = EndpointConfiguration.SdIRiceviFile_port.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public SdIRiceviFileClient(EndpointConfiguration endpointConfiguration) :
base(SdIRiceviFileClient.GetBindingForEndpoint(endpointConfiguration), SdIRiceviFileClient.GetEndpointAddress(endpointConfiguration))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public SdIRiceviFileClient(EndpointConfiguration endpointConfiguration, string remoteAddress) :
base(SdIRiceviFileClient.GetBindingForEndpoint(endpointConfiguration), new System.ServiceModel.EndpointAddress(remoteAddress))
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public SdIRiceviFileClient(EndpointConfiguration endpointConfiguration, System.ServiceModel.EndpointAddress remoteAddress) :
base(SdIRiceviFileClient.GetBindingForEndpoint(endpointConfiguration), remoteAddress)
{
this.Endpoint.Name = endpointConfiguration.ToString();
ConfigureEndpoint(this.Endpoint, this.ClientCredentials);
}
public SdIRiceviFileClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<RiceviFileResponse> SdIRiceviFile.RiceviFileAsync(RiceviFileRequest request)
{
return base.Channel.RiceviFileAsync(request);
}
public System.Threading.Tasks.Task<RiceviFileResponse> RiceviFileAsync(fileSdIBase_Type fileSdIAccoglienza)
{
RiceviFileRequest inValue = new RiceviFileRequest();
inValue.fileSdIAccoglienza = fileSdIAccoglienza;
return ((SdIRiceviFile)(this)).RiceviFileAsync(inValue);
}
public virtual System.Threading.Tasks.Task OpenAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginOpen(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndOpen));
}
public virtual System.Threading.Tasks.Task CloseAsync()
{
return System.Threading.Tasks.Task.Factory.FromAsync(((System.ServiceModel.ICommunicationObject)(this)).BeginClose(null, null), new System.Action<System.IAsyncResult>(((System.ServiceModel.ICommunicationObject)(this)).EndClose));
}
private static System.ServiceModel.Channels.Binding GetBindingForEndpoint(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.SdIRiceviFile_port))
{
var encoding = new WcfCoreMtomEncoder.MtomMessageEncoderBindingElement(new System.ServiceModel.Channels.TextMessageEncodingBindingElement())
{
MessageVersion = System.ServiceModel.Channels.MessageVersion.CreateVersion(System.ServiceModel.EnvelopeVersion.Soap11, System.ServiceModel.Channels.AddressingVersion.None)
};
var transport = new System.ServiceModel.Channels.HttpsTransportBindingElement
{
RequireClientCertificate = true
};
var customBinding = new System.ServiceModel.Channels.CustomBinding(encoding, transport);
return customBinding;
}
throw new System.InvalidOperationException(string.Format("L\'endpoint denominato \'{0}\' non è stato trovato.", endpointConfiguration));
}
private static System.ServiceModel.EndpointAddress GetEndpointAddress(EndpointConfiguration endpointConfiguration)
{
if ((endpointConfiguration == EndpointConfiguration.SdIRiceviFile_port))
{
return new System.ServiceModel.EndpointAddress("https://servizi.fatturapa.it/ricevi_file");
}
throw new System.InvalidOperationException(string.Format("L\'endpoint denominato \'{0}\' non è stato trovato.", endpointConfiguration));
}
private static System.ServiceModel.Channels.Binding GetDefaultBinding()
{
return SdIRiceviFileClient.GetBindingForEndpoint(EndpointConfiguration.SdIRiceviFile_port);
}
private static System.ServiceModel.EndpointAddress GetDefaultEndpointAddress()
{
return SdIRiceviFileClient.GetEndpointAddress(EndpointConfiguration.SdIRiceviFile_port);
}
public enum EndpointConfiguration
{
SdIRiceviFile_port,
}
}
public class SdIClient
{
public string InviaFattura(string path)
{
RiceviFileResponse res;
fileSdIBase_Type file = new fileSdIBase_Type();
file.File = System.IO.File.ReadAllBytes(path);
file.NomeFile = System.IO.Path.GetFileName(path);
using (SdIRiceviFileClient client = new SdIRiceviFileClient())
{
// HTTP Proxy
foreach (System.ServiceModel.Channels.BindingElement be in ((System.ServiceModel.Channels.CustomBinding)client.ChannelFactory.Endpoint.Binding).Elements)
{
System.Type thisType = be.GetType();
if (be is System.ServiceModel.Channels.HttpTransportBindingElement)
{
System.ServiceModel.Channels.HttpTransportBindingElement httpElement = (System.ServiceModel.Channels.HttpTransportBindingElement)be;
httpElement.Proxy = new System.Net.WebProxy("my-proxy-server");
httpElement.Proxy.Credentials = new System.Net.NetworkCredential("user", "password");
}
}
// Certificato
System.Security.Cryptography.X509Certificates.X509Certificate2 certClient =
new System.Security.Cryptography.X509Certificates.X509Certificate2(System.IO.File.ReadAllBytes("\path\to\SDIClient-pk.pfx"), "password", System.Security.Cryptography.X509Certificates.X509KeyStorageFlags.Exportable);
client.ClientCredentials.ClientCertificate.Certificate = certClient;
client.ClientCredentials.ServiceCertificate.SslCertificateAuthentication =
new System.ServiceModel.Security.X509ServiceCertificateAuthentication
{
CertificateValidationMode = System.ServiceModel.Security.X509CertificateValidationMode.None,
RevocationMode = System.Security.Cryptography.X509Certificates.X509RevocationMode.NoCheck
};
res = System.Threading.Tasks.Task.Run(() => client.RiceviFileAsync(file)).GetAwaiter().GetResult();
}
return res.rispostaSdIRiceviFile.IdentificativoSdI;
}
}