Question

I recently started using WCF. I created a WCF service in a console program, and made that console program the service itself. It is being hosted as a Singleton as the program runs, in the following way:

var endPointUri = new Uri("http://127.0.0.1:13370/RuntimeService");
var host = new ServiceHost(this, endPointUri);
host.Open();

Notice how this is being pointed as the instance. This works great, and my console application implements my IRuntimeService just fine.

Now, here's the problem.

How do I add a service client for this service in another project in the solution? Right now, there's an issue in that I have to first start up the console application (for the service to be hosted), and THEN somehow add it in Visual Studio (or else it wouldn't be visible, right?). Same problem arises when I want to update the service.

Right now, as a workaround, I open up the compiled .EXE file of the console application. Then, in my project, I update / add the service client while the .EXE is running. However, that's not very smart in the long run, since I have several developers on this project, and I would like it to be somewhat automated (also because I have a build server that runs some unit tests and deploys the server as needed).

Is there any way that I can programmatically (maybe through commandline) update my WCF service client? Or even better, just update it while the service itself is not running?

Was it helpful?

Solution

I've found that the easiest way to implement this is to define your service client class programatically and pass the URI of the service in to the constructor. So your client class would look like this:

public class RuntimeServiceClient 
{
    public IRuntimeService service;

    public RuntimeServiceClient(string uri)
    {
        // Setup the address of your service
        EndpointAddress address = new EndpointAddress(uri);

        //Setup your binding - HttpBinding, NetTcpBinding, etc.
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
        //Setup your binding parameters

        ChannelFactory<IRuntimeService> factory = new ChannelFactory<IRuntimeService>(binding, address);
        service = factory.CreateChannel();
    }
}

Then, when you want to access your service on the client, you can call it like such:

string uri = "http://myservice.com/RuntimeService";
RuntimeServiceClient client = new RuntimeServiceClient(uri);
client.service.MyRuntimeServiceFunction();

Using this method, you never have to worry about keeping your client in sync with your service. As long as you maintain a reference to your IRuntimeService interface, you can call any function exposed by the service.

OTHER TIPS

you may have to use svcutil.exe for generating the proxy classes manually.

refer to the below SO question for more details on how to create proxy classes.

how to generate client proxy class of a wcf service that will work in the .net 2.0

after creating that, you may need to manually copy those .cs files along with the web config file on to your client application

MSDN

Happy Coding :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top