Search code examples
c#wcfweb-serviceswebservicehost

Convert WebServiceHost to WCF Service Application


I have a working console application where all the adresses and configuration are in a config file. But when I try to move this code to a WCF Service Application i get all kinds of errors.

class Program
{
    static void Main(string[] args)
    {

        WebServiceHost host = new WebServiceHost(typeof(ImageService));
        host.Open();

        Console.WriteLine("Service up");
        Console.ReadLine();

        host.Close();
    }
}

The problem is that the WCF service application starts automatically and has no main method, like the console app. How do I define the WCF service to start a WebServiceHost when no main method exists?


Solution

  • I found a very easy solution to this problem. In order to tell WCF to use webservice instead, all that is needed is to add this to the .svc file

    Factory="System.ServiceModel.Activation.WebServiceHostFactory"
    

    Somehow I couldent open this file in Visual Studio, so I navigated to the project folder and opened it using notepad. My service .svc-file looks like this:

    <%@ ServiceHost Language="C#" Debug="true" Service="MyNamespace.Service1" CodeBehind="Service1.svc.cs" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>