为什么WCF定制ServiceHostFactory每次都被调用?

时间:2015-04-23 01:05:41

标签: wcf wif

我有一个WCF服务(在IIS 7.5中托管),它使用以下

来激活SVC文件
<serviceHostingEnvironment> 
   <serviceActivations>
      <add factory="String"
           service="String"/>
   </serviceActivations>
</serviceHostingEnvironment>

我使用的是自定义主机工厂而非默认主机工厂。

该服务是WSTrustServiceContract。现在,如果你看到这个类的声明

[ServiceBehaviorAttribute(Name = "SecurityTokenService", Namespace = "http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice", 
    InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)]
[AspNetCompatibilityRequirementsAttribute(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class WSTrustServiceContract : IWSTrustFeb2005SyncContract, 
    IWSTrust13SyncContract, IWSTrustFeb2005AsyncContract, IWSTrust13AsyncContract, IWsdlExportExtension, IContractBehavior

所以我们可以推断出

  1. 将调用自定义主机工厂以创建服务实例。
  2. 创建服务实例后,它将存在所有请求(InstanceContextMode.Single)
  3. 我的问题是

    1. 为什么每次调用都会创建自定义主机工厂的实例(我在这个工厂的构造函数中有一个断点。我通过附加到w3wc实例进行调试)?
    2. 为什么每次通话都会创建我的服务实例?

1 个答案:

答案 0 :(得分:0)

正如我所说,我创建了简单的应用程序,它工作正常。所以试着比较一下:

<强>的ServiceHost

Public Class MyServiceHost
    Inherits ServiceHost

    Public Sub New(serviceType As Type, baseAddresses As Uri())
        MyBase.New(serviceType, baseAddresses)
    End Sub

End Class

Public Class MyServiceHostFactory
    Inherits ServiceHostFactory

    Public Sub New()
        Trace.TraceInformation("MyServiceHost instance created.")
    End Sub

    Protected Overrides Function CreateServiceHost(serviceType As Type, baseAddresses As Uri()) As ServiceHost
        Return New MyServiceHost(serviceType, baseAddresses)
    End Function

End Class

<强>服务

<ServiceContract>
Public Interface IService

    <OperationContract()>
    Function SayHello(name As String) As String

End Interface

<ServiceBehavior(InstanceContextMode:=InstanceContextMode.Single, ConcurrencyMode:=ConcurrencyMode.Multiple)>
<AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
Public Class Service
    Implements IService

    Public Sub New()
        Trace.TraceInformation("Service instance created.")
    End Sub

    Public Function SayHello(name As String) As String Implements IService.SayHello
        Trace.TraceInformation("Calling Service.SayHello({0}).", name)
        Return "Hello " & name
    End Function

End Class

Public Class Global_asax
    Inherits HttpApplication

    Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Trace.TraceInformation("Application started")
    End Sub

End Class

<强>的Web.config

<system.serviceModel>
    <behaviors>
        <serviceBehaviors>
            <behavior>
                <serviceMetadata httpGetEnabled="true" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true">
        <serviceActivations>
            <add factory="FileLessActivationWithCustomServiceHost.MyServiceHostFactory"
                 service="FileLessActivationWithCustomServiceHost.Service" 
                 relativeAddress="Service.svc"/>
        </serviceActivations>
    </serviceHostingEnvironment>
</system.serviceModel>

<system.diagnostics>
    <trace autoflush="true" indentsize="4">
        <listeners>
            <remove name="Default" />
            <add name="Default"
                 type="System.Diagnostics.XmlWriterTraceListener"
                 initializeData="log.svclog" />
        </listeners>
    </trace>
</system.diagnostics>

我启动了Web应用程序,对SayHello函数进行了几次调用,如下所示,它的行为正确: enter image description here