WCF应用程序启动事件

时间:2009-04-11 00:48:28

标签: wcf

首次启动WCF服务时获得通知的最佳方式是什么?

ASP.NET应用程序的Global.asax中是否存在类似于Application_Start方法的内容?

8 个答案:

答案 0 :(得分:80)

因为它只是一个类,所以你可以使用一个静态构造函数,它将在第一次使用Type时调用。

public Service : IContract
{
    public Service(){ // regular constructor }
    static Service(){ // Only called first time it's used. }
}

答案 1 :(得分:22)

我使用过此链接,有多种解决方案: http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx

答案 2 :(得分:8)

您可以随时手动将global.asax文件添加到WCF服务应用程序,因为它在IIS上托管并与ASP.NET管道集成:

<%@ Application Codebehind="Global.asax.cs" Inherits="WcfApplication" Language="C#" %>

public class WcfApplication : HttpApplication
{
    protected void Application_Start()
    {
    }
}

答案 3 :(得分:5)

嗯,这可能有点棘手,因为调用WCF服务的首选方式是“按通话”,例如你真的没有任何“开始”的东西,然后只是徘徊,真的。

如果您在IIS或WAS中托管服务,它甚至是服务主机的“按需加载” - 当消息到达时,主机将被实例化并处理请求。

如果你是自托管的,你要么有一个控制台或Winforms应用程序 - 所以你可以在那里挂钩知道它们何时开始。如果您有一个Windows服务来托管您的服务主机,您很可能会覆盖ServiceBase类上的OnStart和OnStop方法 - &gt;挂进那里。

问题更多:你究竟想要完成什么?只是记录或类似的东西,或者你想在内存中建立一些东西来坚持下去?

马克

答案 4 :(得分:4)

如果你有一个自托管WCF服务,你可以在服务开头添加一个事件,在这个事件中你可以分配一个静态变量,就像这个帖子一样:

//Static Variables in a WCF Service
public class Post2331848
{
    [ServiceContract]
    public interface ITest
    {
        [OperationContract]    
        string GetString();
    }    

    public class Service : ITest
    {
        public static string TheString; 
        public string GetString()
        {
            return TheString;
        }
    }

    static void host_Opening(object sender, EventArgs e)
    {
        Service.TheString = "This is the original string";
    } 

    public static void Test() 
    {
        string baseAddress = "http://" + Environment.MachineName + ":8000/Service"; 
        ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress)); 
        ServiceEndpoint endpoint = host.AddServiceEndpoint(typeof(ITest), new BasicHttpBinding(), ""); 

        //This is the magic line!
        host.Opening += new EventHandler(host_Opening);

        host.Open();

        Console.WriteLine("Host opened"); 
        Console.ReadLine();
        host.Close();
    }
}

(最初来自http://www.eggheadcafe.com/community/aspnet/18/10162637/help-in-maintain-global-variable-in-wcf.aspx

祝你好运!

答案 5 :(得分:1)

Imports System.ServiceModel
Imports System.ServiceModel.Description

Public Class MyServiceHost
   Inherits Attribute
    Implements IServiceBehavior

    Public Sub AddBindingParameters(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase, endpoints As System.Collections.ObjectModel.Collection(Of System.ServiceModel.Description.ServiceEndpoint), bindingParameters As System.ServiceModel.Channels.BindingParameterCollection) Implements System.ServiceModel.Description.IServiceBehavior.AddBindingParameters

    End Sub

    Public Sub ApplyDispatchBehavior(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.ApplyDispatchBehavior
        AddHandler serviceHostBase.Opened, AddressOf serviceHostBase_Opened
        AddHandler serviceHostBase.Closed, AddressOf serviceHostBase_Closed
    End Sub

    Public Sub Validate(serviceDescription As System.ServiceModel.Description.ServiceDescription, serviceHostBase As System.ServiceModel.ServiceHostBase) Implements System.ServiceModel.Description.IServiceBehavior.Validate

    End Sub




#Region "Event Handlers"


    Private Sub serviceHostBase_Opened(ByVal sender As Object, ByVal e As EventArgs)



    End Sub

    Private Sub serviceHostBase_Closed(ByVal sender As Object, ByVal e As EventArgs)



    End Sub


#End Region

答案 6 :(得分:0)

Windows Communication Foundation(WCF)中托管服务的标准ServiceHost API是WCF体系结构中的可扩展点。用户可以从ServiceHost派生自己的主机类,通常在打开服务之前覆盖OnOpening以使用ServiceDescription强制添加默认端点或修改行为。

http://msdn.microsoft.com/en-us/library/aa702697%28v=vs.110%29.aspx

答案 7 :(得分:0)

我发现有一个名为WebActivator的nuget包对IIS托管有用。

https://www.nuget.org/packages/WebActivatorEx/

您可以向WCF项目添加一些程序集属性。

[assembly: WebActivatorEx.PreApplicationStartMethod
(
    typeof(MyActivator),
    "Start")
]

public static class MyActivator
{
    public static void Start()
    {
        // do stuff here
    }
}