I'm following this MSDN article on how to implement Username authentication for WCF services.
in step 5, an attribute: [AspNetCompatibilityRequirements] is set on the service to configure the WCF service for ASP.NET compatibility mode. This is required because authentication is done by the HTTP module and you must be able to get the principal and the identity from the HTTP context in order to authorize users either imperatively or declaratively in WCF.
When I run the service I get this error message:
The service cannot be activated because it does not support ASP.NET compatibility. ASP.NET compatibility is enabled for this application. Turn off ASP.NET compatibility mode in the web.config or add the AspNetCompatibilityRequirements attribute to the service type with RequirementsMode setting as 'Allowed' or 'Required'.
It seems like even though I explicitly declared the attribute it's ignored. What can be the reason for this? Even if I change the value to AspNetCompatibilityRequirementsMode.Allowed it does'nt work. Same error message which is weired because then IIS has no reason to complain!
Service:
namespace MyNamespace.IISServiceHost
{
[ServiceBehavior(MaxItemsInObjectGraph = int.MaxValue)]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Required)]
public class CompanyNameAPIService
{
public CompanyNameAPIService()
{
}
}
}
Interface
namespace MyNamespace
{
[ServiceContract]
public interface ICompanyAPI
{
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role="WSIuser")]
[ServiceKnownType(typeof(Supplier))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void AddUpdateSuppliers(int companyId, Supplier[] sups);
[OperationContract]
[PrincipalPermission(SecurityAction.Demand, Role = "WSIuser")]
[ServiceKnownType(typeof(Dimension))]
[WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Xml, ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
void AddUpdateDimension(int companyId, Dimension dims);
...
}
}
I also set the equivalent in Web.config.
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="True" multipleSiteBindingsEnabled="true" />
<service behaviorConfiguration="ServiceBehavior" name="MyNamespace.IISServiceHost.CompanyAPIService">
<endpoint address="" behaviorConfiguration="largeDataBehavior"
binding="basicHttpBinding" bindingConfiguration="BasicBindingConfiguration"
name="BasicEndpoint" contract="MyNamespace.ICompanyAPI" />
<endpoint address="help" behaviorConfiguration="helpPageBehavior"
binding="mexHttpsBinding" bindingConfiguration="" name="MexEndpoint"
contract="MyNamespace.ICompanyAPI"
kind="mexEndpoint" endpointConfiguration="" />
</service>
<bindings>
<basicHttpBinding>
<binding name="BasicBindingConfiguration"><security mode="Transport" /></binding>
</basicHttpBinding>
<webHttpBinding>
<binding name="webBinding">
<security mode="Transport"><transport clientCredentialType="None" /></security>
</binding>
</webHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="largeDataBehavior">
<dataContractSerializer maxItemsInObjectGraph="2147483647" />
<enableWebScript />
</behavior>
<behavior name="helpPageBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpsGetEnabled="true" httpsGetUrl="https://localhost/WSI/service.svc/wsdl" />
<serviceDebug httpsHelpPageEnabled="true" includeExceptionDetailInFaults="true" />
<serviceAuthorization principalPermissionMode="UseAspNetRoles" roleProviderName="WSIRoleProvider">
<authorizationPolicies>
<add policyType="myNamespace.AuthorizationPolicy.HttpContextPrincipalPolicy, AuthorizationPolicy, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</authorizationPolicies>
</serviceAuthorization>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
public class CompanyNameAPIService:ICompanyAPI- AlbertoMyOwnNameSpace.IISServiceHost.CompanyNameAPIService(based on the information you have in your post). However, in config you define the name asMyNamespace.CompanyAPI. Thenameattribute of the<service>element in web.config must match the fully-qualified name of the service class. - carlosfigueiracontract="MyNamespace.ICompanyAPI". Do you think thats the reason? - David