1

I'm struggling with migrating some VB code (from an example I found online) to C#.

It is client-side code which is calling a 3rd Party Java web service. I am getting NullReferenceException for secBindingElement because SecurityBindingElement is an abstract class (please see what I have tried at the bottom).

My assumption is: VB creates an instance of the object implicitly where as in C# I would have to inherit/override the class or use the abstract classes static methods. I am not sure which methods I would have to override or how to accomplish what is necessary calling static methods on the SecurityBindingElement class. This is my first time writing a client application, thanks for the help!

VB

Public Function GetToken() As Xml.XmlElement  

        'Token Service doesn't like TLS, so use SSL  
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3  

        Dim aDPSAuthClient As New DPSAuthService.dpsauthenticationSoapClient()  

        'Set username and password  
        aDPSAuthClient.ClientCredentials.UserName.UserName = "USERNAME" 
        aDPSAuthClient.ClientCredentials.UserName.Password = "PASSWORD" 

        'Need to remove the timestamp for this call for some reason...  
        Dim aElements As System.ServiceModel.Channels.BindingElementCollection = aDPSAuthClient.Endpoint.Binding.CreateBindingElements()  
        Dim aSecurityBindingElement As System.ServiceModel.Channels.SecurityBindingElement = aElements.Find(Of System.ServiceModel.Channels.SecurityBindingElement)()  
        aSecurityBindingElement.IncludeTimestamp = False 
        aDPSAuthClient.Endpoint.Binding = New System.ServiceModel.Channels.CustomBinding(aElements)  

        'Request the token  
        Dim tToken As String = aDPSAuthClient.DPSrequestToken("PARM1")  

        'Clean up the token (remove all the nasty non-XML characters - why are they even in there?!)  
        tToken = tToken.Replace("<![CDATA[", String.Empty).Replace("\n]]>", String.Empty)  
        tToken = tToken.Replace("]]>", String.Empty)  

        'Create an XML Document and load it up with the Token XML  
        Dim anXMLDoc As New Xml.XmlDocument  
        anXMLDoc.LoadXml(tToken)  

        Return anXMLDoc.DocumentElement  

    End Function  

C#

BindingElementCollection bindingElementCollection = dpsAuthSoapClient.Endpoint.Binding.CreateBindingElements();

SecurityBindingElement secBindingElement  = bindingElementCollection.Find<SecurityBindingElement>();

secBindingElement.IncludeTimestamp = false;
dpsAuthSoapClient.Endpoint.Binding = new CustomBinding(secBindingElement);

I have tried overriding the class with empty methods, but there are two members (GetIndividualISecurityCapabilities and CreateSecurityProtocolFactory<TChannel>(System.ServiceModel.Channels.BindingContext, System.ServiceModel.Security.SecurityCredentialsManager, bool, System.ServiceModel.Channels.BindingContext)) that when I try to override them I get and error: "no suitable method found to override"

Config File

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
            <section name="NINO_VERIFICATION_REQUEST.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        </sectionGroup>
    </configSections>
    <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="dpsauthenticationSoap">
              <security mode="Transport" />
            </binding>
            <binding name="dpsauthenticationSoap1" />

          </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://www.tpvs.hmrc.gov.uk/dpsauthentication/dpsauthentication.jws"
                binding="basicHttpBinding" bindingConfiguration="dpsauthenticationSoap"
                contract="DPSAuthenticationTest.dpsauthenticationSoap" name="dpsauthenticationSoap" />

        </client>
    </system.serviceModel>
    <applicationSettings>
        <NINO_VERIFICATION_REQUEST.Properties.Settings>
            <setting name="NINO_VERIFICATION_REQUEST_WebReference_dpsauthentication"
                serializeAs="String">
                <value>https://www.tpvs.hmrc.gov.uk/dpsauthentication/dpsauthentication.jws</value>
            </setting>
        </NINO_VERIFICATION_REQUEST.Properties.Settings>
    </applicationSettings>
</configuration>
Zzz
  • 2,927
  • 5
  • 36
  • 58
  • 4
    I suspect your C# application's config file is missing the endpoint/binding defintions for the WCF service you're trying to use, and that's why you're getting a null reference exception. It's not because of the abstract class. – RogerN Jul 02 '13 at 18:40
  • @RogerN I checked my web config file and it looks good to me. – Zzz Jul 02 '13 at 18:48
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Jul 02 '13 at 18:55
  • 1
    Did you check the security element of the binding? In the example you linked the author mentions having to add the security element. – RogerN Jul 02 '13 at 18:56
  • @RogerN Yes – Zzz Jul 02 '13 at 19:04
  • Can you post your config file? Also, I don't think it's a case of the class being abstract - the .Find method is going to return the binding element, not create one. – Tim Jul 03 '13 at 03:17
  • @Tim I posted my config file :) – Zzz Jul 03 '13 at 16:37

0 Answers0