1

我正在努力将一些 VB 代码(来自我在网上找到的示例)迁移到 C#。

它是调用第 3 方 Java Web 服务的客户端代码。我收到 secBindingElement 的 NullReferenceException 异常,因为 SecurityBindingElement 是一个抽象类(请参阅我在底部尝试过的内容)。

我的假设是:VB 隐式创建对象的一个​​实例,而在 C# 中我必须继承/覆盖该类或使用抽象类静态方法。我不确定我必须重写哪些方法,或者如何完成在 SecurityBindingElement 类上调用静态方法所必需的操作。这是我第一次编写客户端应用程序,感谢您的帮助!

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);

我尝试用空方法覆盖类,但是当我尝试覆盖它们时,有两个成员 (GetIndividualISecurityCapabilities和) 出现错误:“找不到合适的方法来覆盖”CreateSecurityProtocolFactory<TChannel>(System.ServiceModel.Channels.BindingContext, System.ServiceModel.Security.SecurityCredentialsManager, bool, System.ServiceModel.Channels.BindingContext)

配置文件

<?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>
4

0 回答 0