Constructor on type 'XYZ.Profiler' not found.

Question

Thursday, November 7, 2019 9:41 AM

Hi,

I am using Dot net framework 2.0.... In am getting error in Bold and underline line... 

The error message is 

No parameterless constructor defined for this object. in c# or Constructor on type not found.

Please help me i am struggle since long.... Thanks in advance...

///Code  Patch....

Assembly a = Assembly.LoadFrom(Application.StartupPath + "\XYS.exe");

Type mytypes = a.GetType("XYS.Profiler");
Object obj = Activator.CreateInstance(mytypes,new object[] {true});
mytypes.InvokeMember("CheckForExistingService", BindingFlags.Default | BindingFlags.InvokeMethod, null, obj, new object[] { sPortNumber });

All replies (11)

Thursday, November 7, 2019 9:53 AM

Based on code you are trying to create instance of XYS.Profiler with constructor that has single boolean parameter and by error there is no such constructor. Are you sure there is matching constructor?


Thursday, November 7, 2019 10:13 AM

  Object obj = Activator.CreateInstance(mytypes);

This is the only line... I had added the extra parameter of Boolean to check ..


Thursday, November 7, 2019 10:39 AM

Then there probably is not public parameterless constructor. You should check what constructors XYS.Profiler has and what parameters existing constructors require. Then you need to pass values to parameters of constructor that Activator will try to use to create instance.

You can read documentation for examples:

/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8


Thursday, November 7, 2019 11:16 AM

Have you verified that XYZ.Profiler has public parametersless constructor?


Thursday, November 7, 2019 11:24 AM

Public Class DepthProfiler 


Thursday, November 7, 2019 12:25 PM

That means it is declared as public class, but it does not mean it has public parameterless constructor that error refers to.

The declaration of public constructor is like

public class DepthProfiler
{
    /// <summary>
    /// This is the constructor Activator is looking for.
    /// </summary>
    public DepthProfiler()
    { }
}

There might also be implicit default constructor if class does not have any parameter constructors. If there is constructors with parameters, then parameterless constructor must be declared explicitly.


Thursday, November 7, 2019 12:56 PM

Code of DepthProfiler

Imports Microsoft.SqlServer.Management.Trace

Imports Microsoft.SqlServer.Management.Common
Imports System.ServiceProcess
Imports System.Management

Public Class DepthProfiler
    Dim t1 As New System.Threading.Thread(AddressOf DepthTracer)
    Public Trace As TraceServer
    Dim Processids As String = ""
    Dim Ports As String = ""
    Dim dt As DataTable
    Public Function CheckForExistingService(ByVal iPorNumer As String) As Boolean
        Diagnostics.EventLog.WriteEntry("SolutionGuru Distributor", iPorNumer)
        Try
            Shell("sc Create " & "SolutionGuru_REMOTE_WIN_SERVER_" & iPorNumer & " start= auto binPath= """ + Application.StartupPath & "\ & iPorNumer & "\SolutionGuru_REMOTE_WIN_SERVER.exe" + """  DisplayName=  SolutionGuru_REMOTE_WIN_SERVER_" & iPorNumer & " ")
            Shell("sc failure " & "SolutionGuru_REMOTE_WIN_SERVER_" & iPorNumer & " reset= 30 actions= restart/5000")
            SaveSettings(iPorNumer)
            Shell("sc  Start " & "SolutionGuru_REMOTE_WIN_SERVER_" & iPorNumer & " ")
            Return True
        Catch ex As Exception
            Diagnostics.EventLog.WriteEntry("SolutionGuru Distributor", ex.Message)
            Return False
        End Try
    End Function
    Public Function RestartServer(ByVal iPorNumer As String) As Boolean
        fp_GetProcessIds(iPorNumer)
        Try
            Shell("sc  Start " & "SolutionGuru_REMOTE_WIN_SERVER_" & iPorNumer & " ")
            Return True
        Catch ex As Exception
            Diagnostics.EventLog.WriteEntry("SolutionGuru Distributor", ex.Message)
            Return False
        End Try
    End Function
    Public Sub fp_GetProcessIds(ByVal iPorNumer As String)
        Try
            For Each Prid As System.Diagnostics.Process In Process.GetProcesses
                If Prid.ProcessName.ToUpper = "SolutionGuru_REMOTE_WIN_SERVER" Then
                    Dim iProcessId As Integer = Split(Mid(Prid.MainModule.FileName, InStrRev(Prid.MainModule.FileName, "\, -1, CompareMethod.Text) - 4), "\)(0)
                    If iProcessId = iPorNumer Then
                        Prid.Kill()
                        Exit For
                    End If
                End If
            Next
        Catch ex As Exception

        End Try
    End Sub
    Public Sub SaveSettings(ByVal iPorNumer As String)
        Dim dsRCF As New DataSet
        Dim dsPemoteAdmin As New DataSet

        dsPemoteAdmin.ReadXml(Application.StartupPath & "\ & "SolutionGuru_REMOTE_DISTRIBUTOR.exe.config")
        Dim str As String
        str = Application.StartupPath
        Diagnostics.EventLog.WriteEntry("SolutionGuru Distributor", str.ToString)
        Try

            dsRCF.ReadXml(Application.StartupPath & "\ & iPorNumer & "\SolutionGuru_REMOTE_WIN_SERVER.exe.config")
            dsRCF.Tables("CHANNEL").Rows(0)("port") = iPorNumer
            dsRCF.Tables("CHANNEL").Rows(0)("password") = dsPemoteAdmin.Tables("CHANNEL").Rows(0)("password")
            dsRCF.Tables("CHANNEL").Rows(0)("username") = dsPemoteAdmin.Tables("CHANNEL").Rows(0)("username")

            dsRCF.Tables("SETTING").Rows(0)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(0)("VALUE")
            dsRCF.Tables("SETTING").Rows(1)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(1)("VALUE")
            dsRCF.Tables("SETTING").Rows(2)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(2)("VALUE")
            dsRCF.Tables("SETTING").Rows(3)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(3)("VALUE")
            dsRCF.Tables("SETTING").Rows(4)("VALUE") = "tcp"
            dsRCF.Tables("SETTING").Rows(5)("VALUE") = iPorNumer.Trim
            dsRCF.Tables("SETTING").Rows(6)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(6)("VALUE")
            dsRCF.Tables("SETTING").Rows(7)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(7)("VALUE")
            dsRCF.Tables("SETTING").Rows(8)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(8)("VALUE")
            dsRCF.Tables("SETTING").Rows(9)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(9)("VALUE")
            dsRCF.Tables("SETTING").Rows(10)("VALUE") = dsPemoteAdmin.Tables("SETTING").Rows(10)("VALUE")
            'dsRCF.Tables("SETTING").Rows(11)("VALUE") = ""
            'dsRCF.Tables("SETTING").Rows(12)("VALUE") = ""
            dsRCF.Tables("SETTING").Rows(13)("VALUE") = GetIPAddress()
            'dsRCF.Tables("SETTING").Rows(13)("VALUE") = "192.168.3.18"
            dsRCF.WriteXml(Application.StartupPath & "\ & iPorNumer & "\SolutionGuru_REMOTE_WIN_SERVER.exe.config")

        Catch ex As Exception
            Diagnostics.EventLog.WriteEntry("SolutionGuru Distributor", ex.Message)
        End Try
    End Sub
    Shared Function GetIPAddress() As String
        Dim oAddr As System.Net.IPAddress
        Dim sAddr As String
        With System.Net.Dns.GetHostByName(System.Net.Dns.GetHostName())
            oAddr = New System.Net.IPAddress(.AddressList(0).Address)
            sAddr = oAddr.ToString
        End With
        GetIPAddress = sAddr
    End Function
    Private Sub DepthTracer()
        Try
            While (Trace.Read())
                If IsNothing(Trace("HostName")) = True Then Continue While
                Dim v As String = Trace("HostName")
                If Processids.Contains(Trace(4).ToString) = True And Trace("HostName").ToString.ToUpper.Contains(My.Computer.Name) = True Then
                    dt.Rows.Add()
                    dt.Rows(dt.Rows.Count - 1)(0) = Trace("StartTime")
                    dt.Rows(dt.Rows.Count - 1)(1) = Trace(0)
                    For Each str As String In Processids.Split(",")
                        If str.Contains(Trace(4).ToString) = True Then
                            dt.Rows(dt.Rows.Count - 1)(2) = str.Split("#")(1)
                            dt.Rows(dt.Rows.Count - 1)(3) = Trace("HostName").ToString  'str.Split("#")(1)
                        End If
                    Next
                End If
            End While
        Catch ex As Exception
            MsgBox(ex.InnerException.Message)
        End Try

    End Sub
    Public Sub New(ByVal Servername As String, ByVal sUsername As String, ByVal sPassword As String, ByRef dtable As DataTable)
        Try
            GetProcessids()
            Dim conninfo As ConnectionInfoBase = New SqlConnectionInfo
            CType(conninfo, SqlConnectionInfo).ServerName = Servername '"192.168.3.247\QA"
            CType(conninfo, SqlConnectionInfo).UserName = sUsername '"sa"
            CType(conninfo, SqlConnectionInfo).Password = sPassword ' "quality"
            CType(conninfo, SqlConnectionInfo).UseIntegratedSecurity = False
            Trace = New TraceServer()
            Trace.InitializeAsReader(conninfo, Application.StartupPath & "\Tracer.tdf")
            t1.Start()
            dt = dtable

        Catch ex As Exception
            MsgBox("Server Not Able to Trace For Server  : " & Servername & " ")
        End Try
    End Sub
    
    Private Sub GetProcessids()

        Processids = ""
        If Diagnostics.Process.GetProcessesByName("SolutionGuru_REMOTE_WIN_SERVER").Length > 0 Then
            For Each Process1 As Process In Diagnostics.Process.GetProcessesByName("SolutionGuru_REMOTE_WIN_SERVER")
                Ports = Split(Mid(Process1.MainModule.FileName, InStrRev(Process1.MainModule.FileName, "\, -1, CompareMethod.Text) - 4), "\)(0)
                Processids = Processids & "," & Process1.Id & "#" & Ports
            Next
            Processids = Processids.Substring(1)
        End If

    End Sub
    Protected Overrides Sub Finalize()
        '    If IsNothing(Trace) = False Then Trace.Stop()
        MyBase.Finalize()
    End Sub

End Class


Thursday, November 7, 2019 1:05 PM

There is only this constructor

Public Sub New(ByVal Servername As String, ByVal sUsername As String, ByVal sPassword As String, ByRef dtable As DataTable)

So you need to pass value for those 4 parameters to activator to use that constructor to create instance. There might also be better way to create instance if you describe the problem you try to solve?


Friday, November 8, 2019 2:39 AM

Hi Harish,

Thank you for posting here.

This is a question about constructors.

One thing to note is that if you don't add a constructor to a class, C# will create a no-argument constructor by default, but if you add a constructor with parameters, the default no-argument constructor will no longer be provided.

If you still need to use a no-argument construct at this time, please add it yourself.

At this point, VB.Net and C# are the same.

Hope this helpful.

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


Friday, November 8, 2019 9:55 AM

Thanks both of you Timon Yang and MasaSam problem got solved...

By adding constructor with no parameter....


Monday, November 11, 2019 7:17 AM

Hi Harish,

It seems that your problem has been solved. If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

Best Regards,

Timon

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.