Search code examples
c#asp.net-mvcweb-serviceswcfwcf-configuration

how to add wcf service to existing class library


I have a big MVC solution with multiple projects. I am planning to create a separate solution with WCF services and move some highly resource hungry projects. Idea is that the MVC application will communicate with WCF for any computational requirements.

The problem is I dont know how do I call the existing class and interfaces which already have interfaces to services. My class/interface:

public interface IHelloWorld
{
    string SayHello(string name);
}

public class HelloWorld : IHelloWorld
{
    public string SayHello(string name)
    {
        return string.Format("Hello, {0}", name);
    }
}

There are 100s of methods in the class. Not all will be exposed to WCF only a few of them.

Now I have to call this class in newly created WCF service. I am not sure:

  • Do I have modify the existing classes to convert to svc.cs (service) or I can create a separate service file and call the existing methods there?
  • The service class also needs an interface which will be defined in web.config (endpoint contract). Do I have to create a separate interface with only methods I need to expose OR I have to use the existing interface in the class library? If I use the class interface, then I have to mention the same in web.config.

I am bit confused what should be the best way to doing it. I dont want to modify the existing class but want to add a new service which I can just hook up and call from there. new svc.cs files are:

[ServiceContract]
public interface IHelloWorldService
{
    [OperationContract]
    string SayHello(string name);
}

public class HelloWorldService : IHelloWorldService
{
    private IHelloWorld helloWorld = new HelloWorld();
    public string SayHello(string name)
    {
        return helloWorld.SayHello(name);
    }
}

This current design seems I am repeating the existing class/interface. Not sure if this is the correct way. Please help.


Solution

  • Do I have modify the existing classes to convert to svc.cs (service) or I can create a separate service file and call the existing methods there?

    No, because you say that not all methods need to be exposed.

    This current design seems I am repeating the existing class/interface. Not sure if this is the correct way. Please help.

    Yes, it's repeating if you are doing like the code you show in the question. You can write the code of your service like this;

    [ServiceContract]
    public interface IHelloWorldService
    {
        [OperationContract]
        string SayHello(string name);
    }
    
    public class HelloWorldService : IHelloWorldService
    {
        private IHelloWorld helloWorld = new HelloWorld();
    
        public string SayHello(string name)
        {
            return helloWorld.SayHello(name);
        }
    }
    

    By using the IHelloWorld contract on your service, you are not duplicating logics and only needed method will be exposed through HelloWorldService contracts.