4

如果在 IIS7 或 WPAS 中托管我的 WCF 服务,是否可以将两个或多个服务加载到同一个 AppDomain 中,以便它们可以共享静态变量?

4

3 回答 3

5

当然,您可以在Web 应用程序中公开任意数量的端点(即使是不同的 WCF 服务)。这不应仅限于 IIS 或 WPAS。

这样做将使您能够访问任何类型的共享数据。尽管我通常建议不要使用静态变量来共享信息(但我当然不知道您的要求)。

于 2010-04-24T19:46:56.720 回答
2

当然。在 Visual Studio 中,只需添加另一个 WCF 服务项。IIS 将在同一个 AppDomain 中运行这两个服务。在这个例子中,我首先创建了一个只有以下接口定义的库:

namespace ServiceInterface
{
    [ServiceContract]
    public interface IClass
    {
        [OperationContract]
        string GetMessage();
    }
}

然后我在 VS 中创建了一个 Web 应用程序并添加了两个服务:MyService它们Service2都实现了IClass. 这是我的 web.config 文件部分serviceModel

  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="WebService1.MyServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
        <behavior name="WebService1.Service2Behavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="WebService1.MyServiceBehavior"
       name="WebService1.MyService">
        <endpoint address="" binding="wsHttpBinding" contract="ServiceInterface.IClass">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
      <service behaviorConfiguration="WebService1.Service2Behavior"
       name="WebService1.Service2">
        <endpoint address="" binding="wsHttpBinding" contract="ServiceInterface.IClass">
          <identity>
            <dns value="localhost" />
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
      </service>
    </services>
  </system.serviceModel>

在客户端应用程序中,您的配置信息可能如下所示:

<client>
    <endpoint address="http://mymachinename.local/MyService.svc"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IClass"
        contract="ServiceReference1.IClass" name="WSHttpBinding_IClass">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
    <endpoint address="http://mymachinename.local/Service2.svc"
        binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IClass1"
        contract="ServiceReference2.IClass" name="WSHttpBinding_IClass1">
        <identity>
            <dns value="localhost" />
        </identity>
    </endpoint>
</client>
于 2010-04-24T20:56:10.353 回答
0

是的,您可以在 IIS 和 WPAS 中执行此操作。但这样做的唯一方法是在同一个程序集 AFAIK 中编译这两个服务。

于 2010-04-25T01:52:10.123 回答