<%@ ServiceHost Language="C#" Debug="true" CodeBehind="~/App_Code/MyService.cs" Service="MyService" %>
<system.serviceModel>
<services>
<service name="MyNamespace.MyService">
<!-- service configuration -->
</service>
<!-- more services -->
</services>
</system.serviceModel>
<system.serviceModel>
<serviceHostingEnvironment>
<serviceActivations>
<add relativeAddress="MyService.svc" service="MyNamespace.MyService" />
<!-- more services -->
<serviceActivations>
<services>
<service name="MyNamespace.MyService">
<!-- service config -->
</service>
<!-- more services -->
</services>
</system.serviceModel>
<system.serviceModel>
<services>
<service name="MyNamespace.MyService">
<!-- service config -->
</service>
</services>
</system.serviceModel>
static void Main()
{
using(var host = new ServiceHost(typeof(MyService)))
{
host.CloseTimeout = TimeSpan.FromSeconds(20);
host.Open();
try
{
Application.Run(new MainForm());
}
finally
{
host.Close();
}
}
}
public interface ICommunicationObject
{
void Open();
void Close();
void Abort();
event EventHandler Closed;
event EventHandler Closing;
event EventHandler Faulted;
event EventHandler Opened;
event EventHandler Opening;
IAsyncResult BeginClose(AsyncCallback callback, object state);
void EndClose(IAsyncResult result);
IAsyncResult BeginOpen(AsyncCallback callback, object state);
void EndOpen(IAsyncResult result);
CommunicationState State { get; } // Created, Opening, Closed, Closing, Closed, Faulted
// ...
}
public abstract class CommunicationObject : ICommunicationObject { ... }
public abstract class ServiceHostBase : CommunicationObject, IDisposable
{
// used to set a timeout
public TimeSpan CloseTimeout { get; set; }
}
public class ServiceHost : ServiceHostBase
{
public ServiceHost(Type serviceType, params Uri[] baseAddresses);
// ...
}
// CUSTOM
public class ServiceHost<T> : ServiceHost
{
public ServiceHost() : base(typeof(T))
{
}
public ServiceHost(params string[] baseAddresses)
: base(typeof(T), baseAddresses.Select(address => new Uri(address)).ToArray())
{
}
public ServiceHost(params Uri[] baseAddresses) : base(typeof(T), baseAddresses)
{
}
}
<system.serviceModel>
<services>
<service name="MyNamespace.MyService">
<host>
<timeouts closeTimeout="00:00:20" />
</host>
</service>
</services>
</system.serviceModel>
<%@ ServiceHost Language="C#" Debug="true" CodeBehind=""~/App_Code/MyService.cs" Service="MyService" Factory="MyServiceFactory" %>
<serviceActivations>
<add relativeAddress="MyService.svc" service="MyService" factory="MyServiceFactory" />
</serviceActivations>
public MyServiceFactory : ServiceHostFactory
{
// must be implemented!
// used like Main() by IIS/WAS
protected override ServiceHost CreateServiceHost(Type serviceType Uri[] baseAddresses)
{
var host = new ServiceHost(serviceType, baseAddresses);
// ...
return host;
}
}