Search code examples
wcfportable-class-librarywshttpbinding

wcf PerSession (Wshttpbinding) with Portable Class Library (PCL)


seems that Portable Class Library does not support PerSession (Wshttpbinding) required for this.

Is there any work around for this?

i have xamarin forms application (server client) that users can connect to their own database and get or update data. once the user provide his username and password some information must be stored to their sessions like database connection string and when data request from user then the appropriate connection string that is stored to his session will be used for that database and send the data back.

otherwise without persession i have to pass on every function the connection string from the client.

how can i avoid this? Portable Class Library does not support wshttpbinding and therefore i cannot use PerSession

any help?


Solution

  • Too much Headache

    The obstacles are too much for anyone working with the following combination

    • Xamarin Forms
    • PCL (Portable Class Libray)
    • WCF
    • IClientMessageInspector

    The Problem:

    I was need to have a multi client application that each one can connect to his own database ( same schema all )

    For this purpose i was looking to store some information at a session level like (Connection String,User Information) so when a method called to know from which database i will retreive the data .

    Problem # 1 ( took me 5 days to understand and realized)

    • that Sessions cannot be worked with PCL ( WCF PerSession) why? Because PerSession Needs WsHttpBinding but PCL Does not support WsHttpBinding only BasicHttp

    Problem # 2 (took me 3 days to understand why is not working)

    • Then i saw many posts here and there about implementing IClientMessageInspector (see examples online how to implement IClientMessageInspector it's easy) so every time a client request a call to send some extra info to the server together with the call in my case was the connectionString. Here i was getting an exception that

      NotImplementedException. Why? because of this BUG in MONO https://bugzilla.xamarin.com/show_bug.cgi?id=40064

    in simple words

    When creating a WCF client in a PCL (targetting .NET Core) you must use EndpointBehaviors and not Behaviors. This works fine in a Windows RT application, but Mono has not implemented this so produces NotImplementedException in Android and iOS.

    The answer is on the link above by another reply from someone else and i thank him for that .

    In simple words

    Instead of

        MyService.Endpoint.EndpointBehaviors(New MyBehavior)
    

    Use

        Dim prop = MyService.Endpoint.GetType.GetTypeInfo.GetDeclaredProperty("Behaviors")
        Dim obj = CType(prop.GetValue(MyService.Endpoint), KeyedCollection(Of Type, IEndpointBehavior))
        obj.Add(New Behavior)
    

    and instead of

    clientRuntime.ClientMessageInspectors.Add(New MyInspector)
    

    Use

    Public Sub ApplyClientBehavior(endpoint As ServiceEndpoint, clientRuntime As ClientRuntime) Implements IEndpointBehavior.ApplyClientBehavior
        Dim prop = clientRuntime.GetType.GetTypeInfo.GetDeclaredProperty("MessageInspectors")
        Dim obj = CType(prop.GetValue(clientRuntime), ICollection(Of IClientMessageInspector))
        obj.Add(New MyInspector)
    End Sub
    

    No changes needed to the configuration file

    Hope this help someday someone.