How can I create a parameter expression to be used in a func<EndpointAddress> expression with a predefined value?

I am trying to build a Dictionary(Of Type, Func(Of ServiceModel.EndpointAddress)). The func represents a method which takes in a single string parameter which is the service url and returns a EndpointAddress.

Example target method signature: Public Function DnaService(url As String) As EndpointAddress your text Example dictionary entry: _endpointLookup.Add(GetType(DnaService.DnaServiceClient) _ , Function() SessionManager.Instance.URLManager.DnaService("services/DnaService.svc"))

I am trying to build the expression so that the func already has the input parameter value. It feels like this should be some easy to do but I haven't be able to solve for this.

Parameter Expression: Dim urlParameterExpression As ParameterExpression = Expression.Parameter(GetType(String), "url")

How can I create the parameter expression with the value already defined to be used in the func I described above.

Is it possible to define a parameter expression with the value included? If so, how can I do this?

To add some additional details. The end result is to have a centralized class to call wcf services. This is an existing application that is very old... The application is a winforms application, it has many service references. Each service reference has a controller class which basically wraps the methods in the service client. It's just a passthrough really, each controller method has the same signature as the service operation it intends to call. I am trying to replace the need to create these controller classes and also be able to use the existing helper methods used to initialize each service client.

Class Signature: Public Class ServiceController(Of TService As Class, TServiceClient As ServiceModel.ClientBase(Of TService))

Invoke Method:

Public Function Invoke(Of TResponse As Class)(serviceMethod As Expression(Of Func(Of TServiceClient, TResponse)), <CallerMemberName> Optional callingMemberName As String = Nothing) As TResponse
        Dim serviceOperationName As String = "Unknown"

        Try
            Dim serviceMethodCall As MethodCallExpression = serviceMethod.Body
            If (serviceMethodCall IsNot Nothing) Then
                serviceOperationName = serviceMethodCall.Method.Name
                Using serviceClient As TServiceClient = Me.GetServiceClient()
                    Return serviceMethod.Compile()(serviceClient)
                End Using
            Else
                Throw New ArgumentOutOfRangeException(NameOf(serviceMethod), $"Invoke parameter: {NameOf(serviceMethod)} must be a method call.")
            End If
        Catch ex As Exception
            _logger.Error($"A {ex.GetBaseException().GetType().Name} occurred while calling {GetType(TService).Name} operation: {serviceOperationName} from {callingMemberName}.", ex)
            Throw
        End Try
    End Function

Endpoint Lookup: Private Shared _endpointLookup As Dictionary(Of Type, Func(Of ServiceModel.EndpointAddress)) = New Dictionary(Of Type, Func(Of ServiceModel.EndpointAddress))

Method to get a service client:

Private Function GetServiceClient() As TServiceClient
        Dim serviceClient As TServiceClient = Nothing
        Dim serviceClientType As Type = GetType(TServiceClient)
        Dim endpointAddress As ServiceModel.EndpointAddress = Nothing
        Dim getEndpointAddress As Func(Of ServiceModel.EndpointAddress) = Nothing
        If (_endpointLookup _
            .TryGetValue(serviceClientType, getEndpointAddress)) Then
            serviceClient = Activator.CreateInstance(serviceClientType _
                , SessionManager.Instance.URLManager.CreateBinding() _
                , getEndpointAddress())

            SessionManager.Instance.URLManager.ApplyOperationBehavior(serviceClient.Endpoint)
            SessionManager.Instance.ApplyCredentials(serviceClient.ClientCredentials)
            serviceClient.ChannelFactory.Endpoint.EndpointBehaviors.Add(New MspServiceRequestHeaderBehavior())
        End If

        Return serviceClient
    End Function

Usage Example:

return new ServiceController<IVinService, VinServiceClient>()
     .Invoke((vinService) => vinService.DecodeVin(vehicleIdentificationNumber));

I great appreciate any assistance I can get on this. Answers can be in either c# or vb.net, I prefer c# but the project I am working in is written in vb.net.

Thanks in advance, Patrick

I tried to find a solution to my issue via Google and of course StackOverflow but I have yet to find an answer. Perhaps I'm just entering the right search terms in Google?

Upvotes: 0

Views: 66

Answers (0)

Related Questions