【问题标题】:WCF application start eventWCF 应用程序启动事件
【发布时间】:2010-10-18 20:44:35
【问题描述】:

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

对于 ASP.NET 应用程序,是否有类似于 Global.asax 中的 Application_Start 方法的东西?

【问题讨论】:

  • Job Vermeulen 的回答链接了一个可行的解决方案并为您提供了选择。可以办到。还可以在此处找到提供有效解决方案的相关问题 - stackoverflow.com/questions/2453353/…

标签: wcf


【解决方案1】:

由于它只是一个类,因此您可以使用静态构造函数,该构造函数将在第一次使用该类型时被调用。

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

【讨论】:

  • 我希望有更多特定于主机的东西,即托管服务的应用程序中的东西。
  • 保罗回答对我有用。静态构造函数是救星!!
  • 完美。而今天我第一次了解到static构造函数。
  • 另一种选择是使用自定义服务主机工厂并在那里放置一个静态构造函数。这适用于使用自定义工厂的多个服务和 IoC 容器。说如果你使用Autofac.Integration.Wcf 会成功:public class MyHostFactory : AutofacServiceHostFactory { static MyHostFactory() { /* init code goes here */ } }
【解决方案2】:

嗯,这可能有点棘手,因为调用 WCF 服务的首选方式是基于“每次调用”,例如你真的没有任何东西“开始”然后只是闲逛,真的。

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

如果您是自托管的,您可以拥有一个控制台或 Winforms 应用程序 - 这样您就可以连接到那里以了解它们何时启动。如果您有一个 Windows 服务来托管您的服务主机,您很可能会覆盖 ServiceBase 类上的 OnStart 和 OnStop 方法 --> 挂接到那里。

问题更多:您到底想完成什么?只是记录日志或类似的东西,还是你想在内存中建立一些东西来保留??

马克

【讨论】:

  • (-1) 我真的不明白这个“答案”如何帮助任何人。你基本上回问他,没有回答他的问题。
  • 我试图解释在 WCF 中没有什么与 Application_Start 完全一样,但试图提供一些想法,让他可以挂钩以检测他需要检测的内容......对不起,你有这种感觉 - 我试过了我最好,但请随时提供更好的答案!
  • Paul Alexander 提供了一个更好的答案,实际上对我帮助很大。
  • 这回答了问题 - 应用程序没有启动事件,它们是按需提供的。大概必须有一种方法可以绑定到 AppDomain.AssemblyLoaded 但那时为时已晚
  • 我猜 Application_Start 可能是合适的,如果你想做一些特定于 HTTP 绑定的事情,比如初始化 SimpleMembership,以便 Web 服务可以在内部使用角色/成员功能。
【解决方案3】:

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

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

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

【讨论】:

  • 您能否进一步扩展您的答案?你到底继承了什么,这是如何工作的?我认为您的回答可能对我个人有所帮助
  • 这就像你从默认模板创建了一个新的 web api 项目。
  • 在这种情况下调用 wcf 服务不应调用 Application_Start。至少通过 TCP 协议。这是个问题。
【解决方案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】:
    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】:

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

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

      【讨论】:

        【解决方案7】:

        我发现有一个名为 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
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-03-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多