programing

WCF 응용 프로그램 시작 이벤트

minecode 2021. 1. 18. 07:50
반응형

WCF 응용 프로그램 시작 이벤트


WCF 서비스가 처음 시작될 때 알림을받는 가장 좋은 방법은 무엇입니까?

ASP.NET 응용 프로그램에 대한 Global.asax의 Application_Start 메서드와 유사한 것이 있습니까?


음, WCF 서비스를 호출하는 선호하는 방법이 "호출 당"기반이기 때문에 약간 까다로울 수 있습니다. 예를 들어 실제로 "시작된"항목이 없어서 그냥 멈춰 있습니다.

IIS 또는 WAS에서 서비스를 호스팅하는 경우 서비스 호스트의 "주문형로드"일 수도 있습니다. 메시지가 도착하면 호스트가 인스턴스화되고 요청을 처리합니다.

자체 호스팅하는 경우 콘솔 또는 Winforms 앱이 있으므로 시작 시간을 알 수 있습니다. 서비스 호스트를 호스팅 할 Windows 서비스가있는 경우 ServiceBase 클래스의 OnStart 및 OnStop 메서드를 재정 의하여 여기에 연결합니다.

문제는 더 있습니다 : 정확히 무엇을 성취하려고합니까? 로깅이나 그와 비슷한 것, 아니면 기억에 남을 무언가를 만들고 싶습니까 ??

마크


클래스 일 뿐이므로 Type이 처음 사용될 때 호출되는 정적 생성자를 사용할 수 있습니다.

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

이 링크를 사용했으며 여러 솔루션이 있습니다. http://blogs.msdn.com/b/wenlong/archive/2006/01/11/511514.aspx


IIS에서 호스팅되고 ASP.NET 파이프 라인과 통합되므로 언제든지 global.asax 파일을 WCF 서비스 응용 프로그램에 수동으로 추가 할 수 있습니다.

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

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

셀프 호스팅 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 )

행운을 빕니다!


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

WCF (Windows Communication Foundation)에서 서비스를 호스팅하기위한 표준 ServiceHost API는 WCF 아키텍처의 확장 지점입니다. 사용자는 ServiceHost에서 자신의 호스트 클래스를 파생시킬 수 있습니다. 일반적으로 OnOpening을 재정 의하여 ServiceDescription을 사용하여 서비스를 열기 전에 기본 끝점을 명령 적으로 추가하거나 동작을 수정합니다.

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


IIS 호스팅에 유용하다고 생각되는 WebActivator라는 너겟 패키지가 있습니다.

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

You add some assembly attributes to your WCF project.

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

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

ReferenceURL : https://stackoverflow.com/questions/739268/wcf-application-start-event

반응형