Relationship between SVC files and WCF projects?

36,544

Solution 1

.svc files are used when you host your WCF service in IIS.

See Microsoft's doc here and here.

There's a module within IIS that handles the .svc file. Actually, it is the ASPNET ISAPI Module, which hands off the request for the .svc file to one of the handler factory types that has been configured for ASPNET, in this case

System.ServiceModel.Activation.HttpHandler, System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089


If you are hosting your WCF service in something other than IIS, then you don't need the .svc file.

Solution 2

If you are using .net 4.0 or later, you can now "simulate" the .svc via config with the following:

<system.serviceModel>
   <!-- bindings, endpoints, behaviors -->
   <serviceHostingEnvironment >
      <serviceActivations>
         <add relativeAddress="MyService.svc" service="MyAssembly.MyService"/>
      </serviceActivations>
   </serviceHostingEnvironment>
</system.serviceModel>

Then you don't need a physical .svc file nor a global.asax

Solution 3

It is possible to create a WCF project and host it in IIS without using a .svc file.

Instead of implementing your DataContract in your svc code-behind, you implement it in a normal .cs file (i.e. no code behind.)

So, you would have a MyService.cs like this:

public class MyService: IMyService //IMyService defines the contract
{
    [WebGet(UriTemplate = "resource/{externalResourceId}")]
    public Resource GetResource(string externalResourceId)
    {
        int resourceId = 0;
        if (!Int32.TryParse(externalResourceId, out resourceId) || externalResourceId == 0) // No ID or 0 provided
        {
            WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.NotFound;
            return null;
        }
        var resource = GetResource(resourceId);
        return resource;
    }
}

Then comes the thing making this possible. Now you need to create a Global.asax with code-behind where you add an Application_Start event hook:

 public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        RegisterRoutes();
    }

    private void RegisterRoutes()
    {
        // Edit the base address of MyService by replacing the "MyService" string below
        RouteTable.Routes.Add(new ServiceRoute("MyService", new WebServiceHostFactory(), typeof(MyService)));
    }
}

One nice thing about this is that you don't have to handle the .svc in your resource URLs. One not so nice thing is that you now have a Global.asax file.

Share:
36,544
Craig Schwarze
Author by

Craig Schwarze

A software development manager and architect, with extensive experience in enterprise application development using a range of technologies, mostly in the Microsoft technical space. Find me on LinkedIn!

Updated on June 18, 2020

Comments

  • Craig Schwarze
    Craig Schwarze almost 4 years

    When creating a WCF project, the default member files are just ordinary csharp class files, rather than svc files. Are svc files required with a WCF project? When should they be used?

  • Craig Schwarze
    Craig Schwarze over 14 years
    What else can I host my service in?
  • Cheeso
    Cheeso over 14 years
    Three that come to mind are: A custom host that you write, a third-party provided host that you buy, or a Windows Service. msdn.microsoft.com/en-us/library/bb332338.aspx
  • Jim Aho
    Jim Aho about 8 years
    Can .svc and global.asax files in this matter be seen as "deprecated"?