将一些代码从C#转换为VB.net会在编译时抛出错误

时间:2012-10-05 13:06:48

标签: c# vb.net events

我正在尝试将以下代码转换为VB.net

private void Application_Start(object sender, EventArgs e)
    {
        var defaultTableData = new DefaultTableData();
        defaultTableData.CheckAndUpdate();

        if (ConfigurationManager.AppSettings["RSAConfigSwitch"].ToString().ToUpper() == "ON")
        {
            FederatedAuthentication.ServiceConfigurationCreated += new EventHandler<ServiceConfigurationCreatedEventArgs>(FederatedAuthentication_ServiceConfigurationCreated);

        }

    }

    void FederatedAuthentication_ServiceConfigurationCreated(object sender, Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs e)
    {
        String certName = ConfigurationManager.AppSettings["CertificateName"].ToString(); // read from web.config
        System.Security.Cryptography.X509Certificates.X509Store store = new System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine);
        store.Open(OpenFlags.ReadOnly);
        System.Security.Cryptography.X509Certificates.X509Certificate2Collection col = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, true);
        var cookieProtectionCertificate = col[0];

        e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(
            new SessionSecurityTokenHandler(new System.Collections.ObjectModel.ReadOnlyCollection<CookieTransform>(
                new List<CookieTransform> 
                    { 
                        new DeflateCookieTransform(), 
                        new RsaEncryptionCookieTransform(cookieProtectionCertificate), 
                        new RsaSignatureCookieTransform(cookieProtectionCertificate) 
                    })
            ));
    }

转换VB代码

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs) 
        ' Fires when the application is started 
        FederatedAuthentication.ServiceConfigurationCreated += New EventHandler(Of ServiceConfigurationCreatedEventArgs)(FederatedAuthentication_ServiceConfigurationCreated)





    End Sub
Private Sub FederatedAuthentication_ServiceConfigurationCreated(ByVal sender As Object, ByVal e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)  

        Try 
            Dim appCertificateName As String = System.Configuration.ConfigurationManager.AppSettings("adfsCertName") 
            If String.IsNullOrEmpty(appCertificateName) Then 
                Throw New Exception("ADFS_CERTIFICATE in config is empty") 
            End If 
            Dim store As X509Store = New X509Store(StoreName.My, StoreLocation.LocalMachine) 
            store.Open(OpenFlags.ReadOnly) 
            Dim col As X509Certificate2Collection = store.Certificates.Find(X509FindType.FindBySubjectName, appCertificateName, True)

            Dim cookieProtectionCertificate As X509Certificate2 = col(0) 
           e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform)() With { _

        New DeflateCookieTransform(), _ 
        New RsaEncryptionCookieTransform(cookieProtectionCertificate), _ 
        New RsaSignatureCookieTransform(cookieProtectionCertificate) _ 
})))                                                                          


        Catch ex As Exception 
            Throw ex 
        End Try 
    End Sub

但我收到了以下错误 错误103'公共共享事件ServiceConfigurationCreated(sender As Object,e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)'是一个事件,不能直接调用。使用'RaiseEvent'语句来引发事件。

有人可以帮我转换代码吗?

3 个答案:

答案 0 :(得分:0)

http://www.developerfusion.com/将您的C#代码转换为VB.net后,我从那里得到了以下代码。只是这个。,

Private Sub Application_Start(sender As Object, e As EventArgs) Dim defaultTableData = New DefaultTableData() defaultTableData.CheckAndUpdate()

If ConfigurationManager.AppSettings("RSAConfigSwitch").ToString().ToUpper() = "ON" Then

    FederatedAuthentication.ServiceConfigurationCreated += New EventHandler(Of ServiceConfigurationCreatedEventArgs)(AddressOf FederatedAuthentication_ServiceConfigurationCreated)
End If

End Sub

Private Sub FederatedAuthentication_ServiceConfigurationCreated(sender As Object, e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs) Dim certName As [String] = ConfigurationManager.AppSettings("CertificateName").ToString() ' read from web.config Dim store As New System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine) store.Open(OpenFlags.[ReadOnly]) Dim col As System.Security.Cryptography.X509Certificates.X509Certificate2Collection = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, True) Dim cookieProtectionCertificate = col(0)

e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform)() From { _
    New DeflateCookieTransform(), _
    New RsaEncryptionCookieTransform(cookieProtectionCertificate), _
    New RsaSignatureCookieTransform(cookieProtectionCertificate) _
})))

End Sub

希望这可能会有所帮助。

答案 1 :(得分:0)

编译器错误告诉您事件处理程序有问题。 VB不支持添加处理程序的+ =语法。改为使用AddHandler / AddressOf关键字:

   AddHandler  FederatedAuthentication.ServiceConfigurationCreated, AddressOf FederatedAuthentication_ServiceConfigurationCreated

答案 2 :(得分:0)

转换过程中有一些问题 - 试试这个:

Private Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
    Dim defaultTableData = New DefaultTableData()
    defaultTableData.CheckAndUpdate()

    If ConfigurationManager.AppSettings("RSAConfigSwitch").ToString().ToUpper() = "ON" Then
        AddHandler FederatedAuthentication.ServiceConfigurationCreated, AddressOf FederatedAuthentication_ServiceConfigurationCreated
    End If
End Sub

Private Sub FederatedAuthentication_ServiceConfigurationCreated(ByVal sender As Object, ByVal e As Microsoft.IdentityModel.Web.Configuration.ServiceConfigurationCreatedEventArgs)
    Dim certName As String = ConfigurationManager.AppSettings("CertificateName").ToString() ' read from web.config
    Dim store As New System.Security.Cryptography.X509Certificates.X509Store(System.Security.Cryptography.X509Certificates.StoreName.My, System.Security.Cryptography.X509Certificates.StoreLocation.LocalMachine)
    store.Open(OpenFlags.ReadOnly)
    Dim col As System.Security.Cryptography.X509Certificates.X509Certificate2Collection = store.Certificates.Find(System.Security.Cryptography.X509Certificates.X509FindType.FindBySubjectName, certName, True)
    Dim cookieProtectionCertificate = col(0)

    e.ServiceConfiguration.SecurityTokenHandlers.AddOrReplace(New SessionSecurityTokenHandler(New System.Collections.ObjectModel.ReadOnlyCollection(Of CookieTransform)(New List(Of CookieTransform) From {
        New DeflateCookieTransform(),
        New RsaEncryptionCookieTransform(cookieProtectionCertificate),
        New RsaSignatureCookieTransform(cookieProtectionCertificate)
    })))
End Sub