1

一部の VB コード (オンラインで見つけた例から) を C# に移行するのに苦労しています。

これは、サードパーティの Java Web サービスを呼び出しているクライアント側のコードです。SecurityBindingElement は抽象クラスであるため、secBindingElement の NullReferenceException を取得しています (下部で試したことを参照してください)。

私の仮定は次のとおりです。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);

空のメソッドでクラスをオーバーライドしようとしましたが、2 つのメンバー (GetIndividualISecurityCapabilitiesCreateSecurityProtocolFactory<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