ServiceHostFactory から派生したクラスを利用すれば、構成ファイルに system.serviceModel の構成を記述をしなくても、IIS に WCF サービスをホストさせることができます。
SampleService.svc
<%@ ServiceHost Language="C#" Debug="true" Factory="ServiceHostFactorySample.SampleServiceHostFactory" %> using System; using System.Collections.ObjectModel; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Activation; using System.ServiceModel.Channels; using System.ServiceModel.Description; using System.ServiceModel.Dispatcher; using System.Text; namespace ServiceHostFactorySample { public class SampleServiceHostFactory : ServiceHostFactory { public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses) { ServiceHost host = new ServiceHost(typeof(SampleService), baseAddresses); // エンドポイントを追加 host.AddServiceEndpoint(typeof(SampleService), new BasicHttpBinding(), ""); // サービスのメタ情報を取得できるようにする ServiceMetadataBehavior smb = new ServiceMetadataBehavior(); smb.HttpGetEnabled = true; host.Description.Behaviors.Add(smb); return host; } } [ServiceContract] public class SampleService { [OperationContract] public string Hello(string name) { return string.Format("Hello, {0}!", name); } } }
先頭の行で、Service ではなく Factory を指定しています。
これで IIS にホストさせたときに、Factory で指定したカスタム ServiceHostFactory を使ってサービスのインスタンスを生成するようになります。
Web.config には system.serviceModel 以外の構成を記述すれば OK です。