none
Problem setting endpoint address URL dynamically at runtime in client

    Question

  • Hi

    I have a WCF web service and clients running on separate windows server 2003 machines. I'm using windows integrated authentication to authenticate the clients. All of the clients and the service are running in appPools in IIS6 as specified active directory users. I have added the service as a service reference to the client project in Visual Studio 2008. This works fine in the development network. To run it in the Production environment I need to change the URL and the UPN at runtime to the production values, but whatever I try it just throws errors? In fact the only thing that worked - but isn't really practical - is when I removed the development service reference and added the production service reference to the project using Visual Studio before deployment. Surely there is some way to change the endpoint at runtime??? I must be missing something simple. Please help! :)

    Here is the relevant part of the client endpoint:

             < endpoint address ="http://dev.happyfunland.com.au/FunService.svc" binding ="wsHttpBinding"
                  bindingConfiguration ="wsHttp" contract ="FunWebService.IFunService" name ="wsHttp">
                  < identity >
                     < userPrincipalName value ="Service_Dev@happyfunland.com.au">
                  </ identity >
             </ endpoint >
         </ client >
    </ system.serviceModel >

    And here is the code I am using to change the URL at runtime:

    Dim client As FunWebService.FunServiceClient = SetupFunService

    .
    .
    .

    Friend Shared Function SetupFunService() As FunWebService.FunServiceClient

        Dim service As New FunWebService.FunServiceClien t()

        service.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation

         service.Endpoint.Address = New EndpointAddress(AppSettings("URL_FunService" ))

         Return service

    End Function


    The AppSetting is specified in config files along with other values, so that we can change all these values at the same time by simply specifiying the config file in the web.config.
    ie:

    < appSettings file = "Production.config " />

    and in production.config:
    < add key = "URL_FunService " value = " http://live.happyfunland.com.au/FunService.svc"/>

    This just gives the error:
    Inner Exception Message : Security Support Provider Interface (SSPI) authentication failed. The server may not be running in an account with identity 'host/dev.happyfunland.com.au'. If the server is running in a service account (Network Service for example), specify the account's ServicePrincipalName as the identity in the EndpointAddress for the server. If the server is running in a user account, specify the account's UserPrincipalName as the identity in the EndpointAddress for the server.


    If I change the url and the identity in the endpoint  to the production values (like below) I get a different error: "Access is Denied"

          < endpoint address = "http://live .happyfunland.com.au/FunService.svc "

            binding = "wsHttpBinding " bindingConfiguration = "wsHttp " contract = "FunWebService.IFunService " name = "wsHttp ">

            < identity >

              < userPrincipalName value = "Service_Prod @ happyfunland .com.au " />

            </ identity >

          </ endpoint >

    I have also tried the following without success:
    Dim service As New FunWebService.FunServiceClien t( "wsHttp" , AppSettings("URL_FunService" ) )
    - and -
    service.Endpoint.Address = New EndpointAddress( New Uri(AppSettings("URL_FunService" )),EndpointIdentity.CreateUpnIdentity( "Service_Prod@happyfunland.com.au" ))


    PS Please excuse the horrible cut and paste job

    Friday, September 04, 2009 3:22 AM

Answers

  • Got it!

    I put my endpoint back to the original settings and I used the code below to change the URL and identity in the client to the values of the production service at runtime.


    Friend Shared Function SetupFunService() As FunWebService.FunServiceClient
           'create endpoint
           Dim ep As New EndpointAddress(New Uri(AppSettings("URL_FunService" )), EndpointIdentity.CreateUpnIdentity(AppSettings("Identity_FunService" )))
            'create proxy with new endpoint
           Dim service As New SugargliderBilling.SugargliderServiceClient("wsHttp" , ep)
           'allow client to impersonate user
           service.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
           'return our shiny new service
           Return service
    End Function

    ...and I've added this to my config file:
    < add key = "FunIdentity " value = "Service_Prod@happyfunland.com.au "/>

    Hopefully this helps someone else :)
    • Marked as answer by GoTheGoat Monday, September 07, 2009 6:43 AM
    Monday, September 07, 2009 6:43 AM

All replies

  • Could you diff the config when you run svcutil against the production service vs your dev service. ?


    http://blogs.msdn.com/sajay
    Friday, September 04, 2009 3:31 AM
    Moderator
  • Yes - that is where I got the idea of just replacing the endpoint in the web.config with the production values :)
    Friday, September 04, 2009 3:39 AM
  • Got it!

    I put my endpoint back to the original settings and I used the code below to change the URL and identity in the client to the values of the production service at runtime.


    Friend Shared Function SetupFunService() As FunWebService.FunServiceClient
           'create endpoint
           Dim ep As New EndpointAddress(New Uri(AppSettings("URL_FunService" )), EndpointIdentity.CreateUpnIdentity(AppSettings("Identity_FunService" )))
            'create proxy with new endpoint
           Dim service As New SugargliderBilling.SugargliderServiceClient("wsHttp" , ep)
           'allow client to impersonate user
           service.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation
           'return our shiny new service
           Return service
    End Function

    ...and I've added this to my config file:
    < add key = "FunIdentity " value = "Service_Prod@happyfunland.com.au "/>

    Hopefully this helps someone else :)
    • Marked as answer by GoTheGoat Monday, September 07, 2009 6:43 AM
    Monday, September 07, 2009 6:43 AM
  • Sure did! Thanks!
    Tuesday, November 02, 2010 4:46 PM