开发者

Configuring two WCF services (not just endpoints) to be hosted in IIS with different contracts implemented by the same class?

I have a single class that implements a set of operations that I want to be able to expose as two different WCF services. Note that I need these to be two different services rather than two different endpoints within the same service. The reason for this is that I have a behavior on one of the services to require user name authentication. That same behavior cannot be applied to the other service. There are two separate contracts, both of which have been implemented by the class that will be acting as both services.

The problem that I've run into is that when hosting the services in IIS, the name of the service must match the type. Duplicate names are not allowed on the two different services. And therefore, I cannot have two services implemented by the same class (AFAIK).

The workaround that I have in place right now is that I have two empty wrapper classes, which simply inherit from the common class that implements the actual functionality and add nothing to the base class. This allows me to create the configuration for the two services with different type names, even though the types do nothing more than alias the base class.

Here is a sample from my configuration file (with the workaround in place) to help explain...

  <service behaviorConfiguration="ServicesBehavior"
           name="Company.Services.ApplicationServiceWrapper">
    <endpoint address=""
              binding="basicHttpBinding"
              bindingConfiguration="basicHttpBinding"
              contract="Company.Contracts.IApplicationService" />
  </service>
  <service behaviorConfiguration="SecuredServiceBehavior"
           name="Company.Services.SecuredApplicationServiceWrapper">
    <endpoint address="secure"
              binding="wsHttpBinding"
              bindingConfiguration="secureAuthorizedBinding"
              contract="Company.Contracts.ISecuredApplicationService" />
  </service>

And the basic implementation of the contracts and wrappers...

  开发者_JS百科  /// <summary>
    /// These two classes are in place only so that WCF can differentiate between
    /// two types, apply a different contract to each and host them in IIS.
    /// </summary>
    public class ApplicationServiceWrapper : ApplicationService { }
    public class SecuredApplicationServiceWrapper : ApplicationService { }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall
        , ConcurrencyMode = ConcurrencyMode.Multiple)]
    public class ApplicationService : IApplicationService
        , ISecuredApplicationService
    {
        // Implementation.
    }

Any Ideas?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜