开发者

Relationship between SVC files and WCF projects?

开发者 https://www.devze.com 2022-12-17 07:58 出处:网络
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 us开发者_如何转开发ed?

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 us开发者_如何转开发ed?


.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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消