views:

521

answers:

3

If host my WCF services in IIS7 or WPAS, is it possible to load up two or more services into the same AppDomain so that they can share static variables?

+3  A: 

Sure you can expose as many Endpoints within a Web Application as you want (even of different WCF Services). This should not be limited to either IIS or WPAS.

Doing so will enable you to access any kind of shared data. Even though I would normally advise against using static variables to share information (But of course I don't know your requirements).

ntziolis
+1  A: 

Sure. In Visual Studio, simply add another WCF Service item. IIS will run both services in the same AppDomain. In this example, I first created a library with only the following interface definition:

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

I then created a web application in VS and added two services: MyService and Service2 which both implement IClass. This is my web.config file section for 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>

In the client application, your configuration information might look like:

<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>
Thomas
A: 

Yes you can do it in both IIS and WPAS. But the only way of doing that is compiling both services in the same assembly, AFAIK.

Jader Dias