Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 897589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T14:54:32+00:00 2026-05-15T14:54:32+00:00

I have a WCF service that accepts an object as a parameter that has

  • 0

I have a WCF service that accepts an object as a parameter that has a URI and Method Name.
What I am trying to do is have a method that will look @ the URI, if it contains the words “localhost” it will use reflection and call a method, of the name that is passed in as a parameter, within the the same class, return a value and continue on.

public class Test
{
     public GetStatResponse GetStat(GetStatRequest request)
     {

        GetStatResponse returnValue = new GetStatResponse();

         if(Helpers.Contains(request.ServiceURI,"localhost", StringComparison.OrdinalIgnoreCase))
         {
             MethodInfo mi = this.GetType().GetMethod(request.ServiceMethod /*, BindingFlags.Public | BindingFlags.IgnoreCase*/);
             returnValue = (GetStatResponse)mi.Invoke(this,null);
         }

The above is the code segment pertaining to this question. I pull the MethodInfo no problem but I am running into issues on the mi.Invoke. The exception that I receive is “Exception has been thrown by the target of an invocation.” With an Inner Exception “Object reference not set to an instance of an object”. I have tried changing the code to (GetStatResponse)mi.Invoke(new Test(), null), with no luck. Test being the class.

I’m open to other suggestions as to how to resolve this, I just thought reflection might be the easiest.

The Method that I am calling with my testing is defined as

public GetStatResponse TestMethod() 
{
         GetStatResponse returnValue = new GetStatResponse(); 
         Stat stat = new Stat();
         Stat.Label = "This is my label";
         Stat.ToolTip = "This is my tooltip";
         Stat.Value = "this is my value"; 

         returnValue.Stat = stat;
         return returnValue;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T14:54:33+00:00Added an answer on May 15, 2026 at 2:54 pm

    Before calling the method you might want to make sure that the MethodInfo you are pulling through reflection is not null:

    MethodInfo mi = this.GetType().GetMethod(
        request.ServiceMethod, 
        BindingFlags.Public | BindingFlags.Instance | BindingFlags.IgnoreCase
    );
    
    // Make sure that the method exists before trying to call it
    if (mi != null)
    {
        returnValue = (GetStatResponse)mi.Invoke(this, null);
    }
    

    After your update it seems that the exception is thrown inside the method you are calling:

    GetStatResponse returnValue = new GetStatResponse(); 
    // Don't forget to initialize returnValue.Stat before using it:
    returnValue.Stat = new WhateverTheTypeIs();
    returnValue.Stat.Label = "This is my label";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My WCF Service has an OperationContract that accepts, as a parameter, an array of
I have a WCF service operation that accepts a data contract parameter of custom
I have a WCF chat service that accepts duplex tcp connections. A single duplex
I have a WCF Service that exposes a method GetCustomers(). The internals of the
I have a WCF restful service that I'm trying to upload an image to.
I have a WCF service that I have to reference from a .net 2.0
I have a WCF Service that should not enter the faulted state. If there's
I have a WCF service that is hosted in a windows application. The service
I have a WCF service that I call from a windows service. The WCF
I have a WCF service that uses basicHttpbinding in development. Now in product we

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.