Benutzer:MovGP0/WCF/Hosting

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      

Using .svc file

[Bearbeiten | Quelltext bearbeiten]
.svc
<%@ ServiceHost Language="C#" Debug="true" CodeBehind="~/App_Code/MyService.cs" Service="MyService" %>
web.config
<system.serviceModel>
   <services>
      <service name="MyNamespace.MyService">
         <!-- service configuration -->
      </service>
      <!-- more services -->
   </services>
</system.serviceModel>

Using web.config

[Bearbeiten | Quelltext bearbeiten]
<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>
app.config
<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();
      }
   }
}

ServiceHost Class

[Bearbeiten | Quelltext bearbeiten]
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)
   {
   }
}
setting timeout in web.config
<system.serviceModel>
   <services>
      <service name="MyNamespace.MyService">
         <host>
            <timeouts closeTimeout="00:00:20" />
         </host>
      </service>
   </services>
</system.serviceModel>
  • Hosting-Engine that is used by modern versions of IIS
  • configuration with .svc like with IIS hosting
  • advantages over self-hosting
    • application pooling, recycling, idle time management, identity management, isolation
.svc file
<%@ ServiceHost Language="C#" Debug="true" CodeBehind=""~/App_Code/MyService.cs" Service="MyService" Factory="MyServiceFactory" %>
web.config
<serviceActivations>
   <add relativeAddress="MyService.svc" service="MyService" factory="MyServiceFactory" />
</serviceActivations>
Implementation
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;
   }
}

Windows Server/Azure AppFabric

[Bearbeiten | Quelltext bearbeiten]
  • Extension to WAS
  • additional configuration options for WCF and WF services
    • health monitoring, diagnostics, instrumentation, event tracking, troubleshooting
  • optimized for WF
    • persistence, state management correlation
  • plugin for IIS Management Console
  • stores events in SQL server database
  • autostart services without waiting for client request