Reputation: 342
I have a WCF service that is setup to be hosted within a unity container. I was intending to use this container to perform method interception. The problem is I cannot get my interceptor to fire...
First here the definition of my interceptor attribute and handler:
[AttributeUsage(AttributeTargets.Method)]
public class PCSecurityAttribute : HandlerAttribute
{
public PCSecurityAttribute(Modules module, int modulePermission)
{
SecurityModule = module;
SecurityModulePermission = modulePermission;
}
public Modules SecurityModule
{
get;
set;
}
public int SecurityModulePermission
{
get;
set;
}
public override ICallHandler CreateHandler(IUnityContainer container)
{
return new PCSecurityCallHandler(0, SecurityModule,
SecurityModulePermission);
}
}
using System.ServiceModel.Security;
using MHC.PracticeConnect.Contract.Data.Security;
using Microsoft.Practices.Unity.InterceptionExtension;
namespace MHC.PracticeConnect.Service
{
public class PCSecurityCallHandler : ICallHandler
{
private Modules securityModule;
private int securityModulePermission;
public PCSecurityCallHandler(Modules module, int modulePermission)
{
securityModule = module;
securityModulePermission = modulePermission;
Order = 0;
}
public PCSecurityCallHandler(int order, Modules module,
int modulePermission)
{
securityModule = module;
securityModulePermission = modulePermission;
Order = order;
}
public IMethodReturn Invoke(IMethodInvocation input,
GetNextHandlerDelegate getNext)
{
bool validPermission = false;
// check security permission
IMethodReturn result;
if (validPermission)
result = getNext().Invoke(input, getNext);
else
throw new SecurityAccessDeniedException(
"The current user does not have security " +
"permissions to call this module.");
return result;
}
public int Order
{
get;
set;
}
}
}
In my host I've tried to configure it to use interception to no avail... HELP!!!!
public class DocumentTemplateServiceHostFactory : ServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType,
Uri[] baseAddresses)
{
UnityServiceHost host =
new UnityServiceHost(serviceType, baseAddresses);
UnityContainer unity = new UnityContainer();
host.Container = unity;
host.Container.AddNewExtension<Interception>(); ;
host.Container.RegisterType<IDocumentTemplateService,
DocumentTemplateService>().Configure<Interception>().
SetInterceptorFor<IDocumentTemplateService>(
new TransparentProxyInterceptor());
return host;
}
}
What am I doing wrong here?
Upvotes: 1
Views: 4568
Reputation: 3406
I've created something that does exactly what you're looking for. I've placed it on CodePlex:
http://wcfaop.codeplex.com/
Basically, you have to create your own InstanceProvider which will then create the WCF service object from a Unity container and allow you to do interception.
Upvotes: 0
Reputation: 829
You might want to check out using WCF behaviors. Here's links that might be useful.
http://msdn.microsoft.com/en-us/magazine/cc136759.aspx
AND
http://www.global-webnet.net/blogengine/post/2009/01/03/Integrating-IIS-WCF-and-Unity.aspx
-Bryan
Upvotes: 1