Oppure

Loading
29/01/22 22:36
dylan666
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:

Imports MyServicePROXY.MyFirst_ServiceReference
Imports System.ServiceModel

Public Class Form1
    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click

        Dim EndpointUriSoapun As String = "https://" & txt_Domain.Text & "/myservice/v0001/soapun"
        Dim binding As BasicHttpsBinding = New BasicHttpsBinding()
        Dim address As EndpointAddress = New EndpointAddress(EndpointUriSoapun)

        binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.Basic
        binding.MaxReceivedMessageSize = Integer.MaxValue
        binding.ReceiveTimeout = New System.TimeSpan(0, 20, 0)
        binding.SendTimeout = New System.TimeSpan(0, 20, 0)
        Dim client As MyServiceClient = New MyServiceClient(binding, address)
        client.ClientCredentials.UserName.UserName = "Test"
        client.ClientCredentials.UserName.Password = "Test"

        Net.ServicePointManager.SecurityProtocol = 3072

'DA QUI IN POI CONTINUA IN MANIERA A ME NOTA

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:

    Public Function BeforeSendRequest(ByRef request As Message, ByVal channel As IClientChannel) As Object
        Dim httpRequest As Channels.HttpRequestMessageProperty
        httpRequest.Headers.Add("CustomerName", "Azienda1")
        Return Nothing
    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 29/01/22 22:42
aaa
31/01/22 18:47
dylan666
Ci sono riuscito....
In pratica si devono creare le due classi descritte qui:
stackoverflow.com/questions/964433/how-to-add-a-custom-http-header-to-every-wcf-call/…

E poi sarà possibile usare questa funzione nel codice principale (prima dei client.ClientCredentials.UserName)
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
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher

Public Class AddUserAgentEndpointBehavior
    Implements IEndpointBehavior

    Public Sub AddBindingParameters(endpoint As ServiceEndpoint, bindingParameters As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
        'Throw New NotImplementedException()
    End Sub

    Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        clientRuntime.MessageInspectors.Add(New AddUserAgentClientMessageInspector())
        'Throw New NotImplementedException()
    End Sub

    Public Sub ApplyDispatchBehavior(endpoint As ServiceEndpoint, endpointDispatcher As EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
        'Throw New NotImplementedException()
    End Sub

    Public Sub Validate(endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
        'Throw New NotImplementedException()
    End Sub
End Class


AddUserAgentClientMessageInspector.vb
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Dispatcher

Public Class AddUserAgentClientMessageInspector
    Implements IClientMessageInspector

    Private Function IClientMessageInspector_BeforeSendRequest(ByRef request As Message, channel As IClientChannel) As Object Implements IClientMessageInspector.BeforeSendRequest
        Dim Myproperty As HttpRequestMessageProperty = New HttpRequestMessageProperty()
        Dim userAgent = "Azienda1"
        Myproperty = New HttpRequestMessageProperty()
        Myproperty.Headers("CustomerName") = userAgent
        request.Properties.Add(HttpRequestMessageProperty.Name, Myproperty)

        Return Nothing
        'Throw New NotImplementedException()
    End Function

    Private Sub IClientMessageInspector_AfterReceiveReply(ByRef reply As Message, correlationState As Object) Implements IClientMessageInspector.AfterReceiveReply
        'Throw New NotImplementedException()
    End Sub
End Class


Ultima modifica effettuata da dylan666 01/02/22 7:59
aaa