Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
C# / VB.NET - [VB. NET] - Aggiunta ti un Header personalizzato per un Webservice SOAP
Forum - C# / VB.NET - [VB. NET] - Aggiunta ti un Header personalizzato per un Webservice SOAP

Avatar
dylan666 (Normal User)
Pro


Messaggi: 129
Iscritto: 08/09/2009

Segnala al moderatore
Postato alle 23:36
Sabato, 29/01/2022
Ciao a tutti,
sto approcciando un primo (goffo) tentativo di sfruttare i webservice di un servizio particolare.
Per la parte di accesso "classico" l'approccio è abbastanza standard:

Codice sorgente - presumibilmente VB.NET

  1. Imports MyServicePROXY.MyFirst_ServiceReference
  2. Imports System.ServiceModel
  3.  
  4. Public Class Form1
  5.     Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
  6.  
  7.         Dim EndpointUriSoapun As String = "https://" & txt_Domain.Text & "/myservice/v0001/soapun"
  8.         Dim binding As BasicHttpsBinding = New BasicHttpsBinding()
  9.         Dim address As EndpointAddress = New EndpointAddress(EndpointUriSoapun)
  10.  
  11.         binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic
  12.         binding.MaxReceivedMessageSize = Integer.MaxValue
  13.         binding.ReceiveTimeout = New System.TimeSpan(0, 20, 0)
  14.         binding.SendTimeout = New System.TimeSpan(0, 20, 0)
  15.         Dim client As MyServiceClient = New MyServiceClient(binding, address)
  16.         client.ClientCredentials.UserName.UserName = "Test"
  17.         client.ClientCredentials.UserName.Password = "Test"
  18.  
  19.         Net.ServicePointManager.SecurityProtocol = 3072
  20.  
  21. 'DA QUI IN POI CONTINUA IN MANIERA A ME NOTA
  22.  
  23. End Class



Esiste però una variante di accesso in cui (oltre a username e password) si deve passare anche un header personalizzato chiamato "CustomerName" in cui va passato un nome (es. "Azienda1").
La cosa funziona se metto l'header a mano ad esempio in Postman (dove per la verità uso Rest che mi è un po' più comodo) ma non ho capito come ottenere la stessa cosa nel codice sopra.
Da qualche parte ho letto dell'usare "BeforeSendRequest" e un codice come questo:

Codice sorgente - presumibilmente VB.NET

  1. Public Function BeforeSendRequest(ByRef request As Message, ByVal channel As IClientChannel) As Object
  2.         Dim httpRequest As Channels.HttpRequestMessageProperty
  3.         httpRequest.Headers.Add("CustomerName", "Azienda1")
  4.         Return Nothing
  5.     End Function



Ma non ho capito ne dove inserire la funzione ne come richiamarla...
Grazie a che avrà la pazienza di spiegarmelo :)

Ultima modifica effettuata da dylan666 il 29/01/2022 alle 23:42
PM Quote
Avatar
dylan666 (Normal User)
Pro


Messaggi: 129
Iscritto: 08/09/2009

Segnala al moderatore
Postato alle 19:47
Lunedì, 31/01/2022
Ci sono riuscito....
In pratica si devono creare le due classi descritte qui:
https://stackoverflow.com/questions/964433/how-to-add-a-cus ...

E poi sarà possibile usare questa funzione nel codice principale (prima dei client.ClientCredentials.UserName)
Codice sorgente - presumibilmente Plain Text

  1. client.Endpoint.Behaviors.Add(new AddUserAgentEndpointBehavior())



Il problema più grande era che nel passare il codice del link da C# a VB.Net il convertitore che avevo usato (il telerik) aveva "tradotto" come "Inherits" quelli che dovevano essere invece degli "Implements"
Correggendo quello poi il Visual Studio ha riscritto da solo tutte le "Public Sub" delle classi ed io ci ho ri-aggiunto le parti personalizzate e commentato tutti i "Throw New NotImplementedException()"

Non essendo esperto per me è stata un'impresa! :om:

Ecco le due classi complete

AddUserAgentEndpointBehavior.vb
Codice sorgente - presumibilmente VB.NET

  1. Imports System.ServiceModel.Channels
  2. Imports System.ServiceModel.Description
  3. Imports System.ServiceModel.Dispatcher
  4.  
  5. Public Class AddUserAgentEndpointBehavior
  6.     Implements IEndpointBehavior
  7.  
  8.     Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
  9.         'Throw New NotImplementedException()
  10.     End Sub
  11.  
  12.     Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
  13.         clientRuntime.MessageInspectors.Add(New AddUserAgentClientMessageInspector())
  14.         'Throw New NotImplementedException()
  15.     End Sub
  16.  
  17.     Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
  18.         'Throw New NotImplementedException()
  19.     End Sub
  20.  
  21.     Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
  22.         'Throw New NotImplementedException()
  23.     End Sub
  24. End Class



AddUserAgentClientMessageInspector.vb
Codice sorgente - presumibilmente VB.NET

  1. Imports System.ServiceModel
  2. Imports System.ServiceModel.Channels
  3. Imports System.ServiceModel.Dispatcher
  4.  
  5. Public Class AddUserAgentClientMessageInspector
  6.     Implements IClientMessageInspector
  7.  
  8.     Private Function IClientMessageInspector_BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
  9.         Dim Myproperty As HttpRequestMessageProperty = New HttpRequestMessageProperty()
  10.         Dim userAgent = "Azienda1"
  11.         Myproperty = New HttpRequestMessageProperty()
  12.         Myproperty.Headers("CustomerName") = userAgent
  13.         request.Properties.Add(HttpRequestMessageProperty.Name, Myproperty)
  14.  
  15.         Return Nothing
  16.         'Throw New NotImplementedException()
  17.     End Function
  18.  
  19.     Private Sub IClientMessageInspector_AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
  20.         'Throw New NotImplementedException()
  21.     End Sub
  22. End Class




Ultima modifica effettuata da dylan666 il 01/02/2022 alle 8:59
PM Quote