tong
tong

Reputation: 269

how to host custom wcf applicaion(server) on IIS7.5?

I've used custom wcf applicaion as wcf server by using:

ServiceHost<AlertService> alertServiceHost = new ServiceHost<AlertService>();
configuredEndpoints = alertServiceHost.Description.Endpoints;
alertServiceHost.Open();

Now, I have a problem with deployment on production which is IIS7.5.

I have no idea how to deploy on IIS. Because I only know that I have to create svc file for hosting on IIS. Now, I only have console application working as wcf service.

How can I transform it to deploy on IIS?

Thank you.

Upvotes: 1

Views: 851

Answers (2)

marc_s
marc_s

Reputation: 754468

If you want to use a custom service host in a IIS hosting scenario, you need to supply a custom ServiceHostFactory that will return that type of service host, and configure that service host factory in your SVC file.

Basically, your custom service host factory must descend from ServiceHostFactory and override one method that returns an instance of your custom service host - something along the lines of:

public class MyOwnServiceHostFactory : ServiceHostFactory 
{ 
    protected override ServiceHost CreateServiceHost(Type t, Uri[] baseAddresses) 
    { 
        return new MyOwnCustomServiceHost(t, baseAddresses); 
    }

    public override ServiceHostBase CreateServiceHost(string service, Uri[] baseAddresses)
    {
         // The service parameter is ignored here because we know our service.
         ServiceHost serviceHost = new ServiceHost(typeof(HelloService), baseAddresses);
         return serviceHost;
    }
}

And in your SVC file, you now need to have:

<%@ ServiceHost Language="C#" Debug="true" 
        Service="YourNamespace.YourService" 
        Factory="YourNamespace.MyOwnServiceHostFactory" %> 

Read more about:

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364269

When hosting in IIS you don't create ServiceHost by yourselves. It is reponsibility of IIS and reason why .svc file exists. Svc file instructs worker process how and which service should be hosted.

Svc file consists of simple directive (markup):

<%@ ServiceHost Language="C#" Debug="true" Service="MyNamespace.MyService" CodeBehind="MyService.svc.cs" %> 

This is example of .svc file hosting locally create service but you can omit CodeBehind attribute and use full type description in Service attribute to host service class from another assembly.

Also check How to: Host WCF service in IIS.

Upvotes: 1

Related Questions