Search code examples
c#visual-studiowcffind-all-references

Find all references with WCF OperationContract and DataContracts


I'm trying to figure out if there's a way to "Find all references" (using the VS feature, as opposed to Control+F entire solution). when it comes to WCF Data and OperationContracts. In case that is unclear:

namespace WcfTestReferences
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello world");

            DoStuff();

            ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
            var results = client.GetData(42);

            Console.WriteLine(results);
        }

        static void DoStuff() { }
    }
}

namespace WcfTestReferences.WCFApp
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }

    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

Solution looks like this:

enter image description here

Now, if I look at DoStuff() with code lens, I can see that it in fact has a reference to it:

enter image description here

But the same does not hold true for the methods being called in the wcf service:

enter image description here enter image description here

In the above, the only references to the interface/method is the interface/method. I understand that the reference that I was hoping would be there (from the main method):

var results = client.GetData(42);

is not there, because the client is generated, and is not actually my Service1 implementation... but is there a way to change this?

In the real world, we have a WCF layer with thousands of methods, many of which are not used - but I cannot rely on Code Lens/Find all references to make this determination. Is there any way to change this behavior?


Solution

  • because the client is generated, and is not actually my Service1 implementation

    This is the root of the problem.

    You are correct - there is no way for your code analyser to determine that the GetData() call you are making from your client is semantically the same thing as the GetDate() service operation you have defined on your interface, because from a binary perspective they are defined in two completely different types.

    The root of this is that you're using a service reference. WCF provides service references as the default way of connecting to a service, but in my opinion service references are problematic and should be avoided.

    Luckily, WCF provides another way of consuming and calling a service via the user of ChannelFactory<T>. One of the many benefits you will get when using this instead of a service reference is that your client will have use of the service interface via a binary reference to the assembly containing your service definition.

    This will allow tools like code lens to resolve references to your interface methods directly to your consuming clients.