Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Kind of an old questions, but for Googlers..</p> <p>Actually, it is possible to create a WCF project and host it in IIS without using a .svc file.</p> <p>Instead of implementing your DataContract in your svc code-behind, you implement it in a normal .cs file (i.e. no code behind.)</p> <p>So, you would have a MyService.cs like this:</p> <pre><code>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; } } </code></pre> <p>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:</p> <pre><code> 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))); } } </code></pre> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload