I'm trying to implement a UoW like shown here:
https://blog.iannelson.uk/wcf-global-exception-handling/
But I can't for the life of me figure out how to wire it up with Autofac. I have absolutely no idea where to start.
I've got WCF working fine with Autofac from using http://autofac.readthedocs.org/en/latest/integration/wcf.html
But to inject or add the IEndpointBehavior? No idea...
If there's a better way to implement a UoW I would like to hear.
Edit:
For now I've just done:
builder.RegisterType(typeof (UnitOfWork))
.As(typeof (IUnitOfWork))
.InstancePerLifetimeScope()
.OnRelease(x =>
{
Trace.WriteLine("Comitted of UoW");
((IUnitOfWork) x).Commit();
// OnRelease inhibits the default Autofac Auto-Dispose behavior so explicitly chain to it
x.Dispose();
});
Though I don't know if this is an acceptable way of doing it, seems like a hack :(
Edit2:
Doesn't seem like it's possible to run a UoW in WCF :/
Edit 3:
I've posted my solution here: http://www.philliphaydon.com/2011/11/06/unit-of-work-with-wcf-and-autofac/
I have found a solution to this problem, where the unit of work only will be committed if no errors is thrown.
Register the unit of work as InstancePerLifetimeScope in Autofac
builder.RegisterType(typeof (UnitOfWork))
.As(typeof (IUnitOfWork)).InstancePerLifetimeScope();
Then i have created a combined EndpointBehavior and a ErrorHandler.
public class UnitOfWorkEndpointBehavior : BehaviorExtensionElement, IEndpointBehavior
{
public void Validate(ServiceEndpoint endpoint)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
var unitOfWorkInstanceHandler = new UnitOfWorkInstanceHandler();
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(unitOfWorkInstanceHandler);
endpointDispatcher.DispatchRuntime.InstanceContextInitializers.Add(unitOfWorkInstanceHandler);
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
protected override object CreateBehavior()
{
return new UnitOfWorkEndpointBehavior();
}
public override Type BehaviorType
{
get { return typeof (UnitOfWorkEndpointBehavior); }
}
}
public class UnitOfWorkInstanceHandler : IInstanceContextInitializer, IErrorHandler
{
private bool _doCommit = true;
public void Initialize(InstanceContext instanceContext, Message message)
{
instanceContext.Closing += CommitUnitOfWork;
}
void CommitUnitOfWork(object sender, EventArgs e)
{
//Only commit if no error has occured
if (_doCommit)
{
//Resolve the UnitOfWork form scope in Autofac
OperationContext.Current.InstanceContext.Extensions.Find<AutofacInstanceContext>().Resolve<IUnitOfWork>().Commit();
}
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
_doCommit = false;
}
public bool HandleError(Exception error)
{
_doCommit = false;
return false;
}
}
The registration of the Endpoint Behavior in web.config
<system.serviceModel>
...
<extensions>
<behaviorExtensions>
<add name="UnitOfWork" type="Namespace.UnitOfWorkBehavior, Namespace"/>
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior name="">
<UnitOfWork/>
</behavior>
</endpointBehaviors>
...
</behaviors>
...
</system.serviceModel>
Related
I've currently rigged up Ninject into a WCF application (using ninject.extensions.wcf), and the basics are working fine. However, I've added a custom IDispatchMessageInspector and IServiceBehavior in order to authenticate each service call (by check the database against some credentials), but I'm not entirely sure how to get Ninject working within the IDispatchMessageInspector.
The code I have so far is:
public class MyServiceInspector: IDispatchMessageInspector
{
#region Methods
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
// TODO: Get credentials from request here, and hit database.
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
// Do nothing
}
#endregion
}
The IServiceBehavior which initiates this is as follows:
public class MyServiceBehaviour : Attribute, IServiceBehavior
{
#region Methods
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
// Do nothing
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
// Loop through channels and endpoints
foreach (ChannelDispatcher cDispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher eDispatcher in cDispatcher.Endpoints)
{
// Add credentials inspector
eDispatcher.DispatchRuntime.MessageInspectors.Add(new MyServiceInspector());
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
// Do nothing
}
#endregion
}
I've scoured the internet looking for examples, but I can't seem to find anything. Is this even possible?
I know this answer is very late, but hopefully it helps other people coming across this issue.
I had the same problem today.. I needed a message inspector with injected dependencies. They added a NinjectBehaviorExtensionElement class to Ninjext.Extensions.WCF from version 3.2. So here's how i injected dependencies in my Message Inspector:
The Message Inspector:
public class MyMessageInspector : IDispatchMessageInspector
{
public MyMessageInspector(IInjectedDependency injectedDependency)
{
}
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}
}
The Behavior:
public class MyMessageInspectionBehavior : IEndpointBehavior
{
private readonly IDispatchMessageInspector _messageInspector;
public MyMessageInspectionBehavior(IDispatchMessageInspector messageInspector)
{
_messageInspector = messageInspector;
}
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(_messageInspector);
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
The Ninject Binding:
this.Bind<IDispatchMessageInspector>()
.To<MyMessageInspector>()
.WhenInjectedInto<IEndpointBehavior>();
The last step is the Configuration:
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="MyCustomMessageInspector"
type="Ninject.Extensions.Wcf.BaseNinjectBehaviorExtensionElement+NinjectBehaviorExtensionElement`1[[MyNamespace.MyMessageInspectionBehavior, MyAssemblyName]], Ninject.Extensions.Wcf" />
</behaviorExtensions>
</extensions>
<behaviors>
<endpointBehaviors>
<behavior>
<MyCustomMessageInspector />
</behavior>
</endpointBehaviors>
I have parked this for now, as there doesn't seem to be any solution. I am simply connecting to my context directly with a view to improving it in the future.
I am implementing IErrorHandler in order to centralize all of the error handling for my WCF service in one place. This works fairly well:
public class ServiceErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
// ..Log..
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
// ..Provide fault..
}
}
Now, we're using Ninject to inject dependencies in the rest of the service, and I'd like to do the same here. Since WCF is constructing the objects based on my configuration, and I don't think I have any hooks into this process, I need to use property injection:
[Inject]
public ILoggingService Logger { get; set; }
However, this never seems to get injected. I tried using Ninject's MVC extensions to set ServiceErrorHandler to allow injection like a filter, but that didn't seem to do the trick. Is there a way to make this happen?
Late answer, but you can inject dependencies into IErrorHandler by creating your custom ServiceHost, let's say TestServiceHost.
In your TestServiceHost you need to do:
Implement constructor with IErrorHandler parameter.
Inside, create a private nested class named ErrorHandlerBehaviour*, which needs to implement both IServiceBehavior and IErrorHandler. It also must have constructor with IErrorHandler parameter.
Override OnStarting() method, where you will add ErrorHandlerBehaviour to service behaviours. All behaviours must be added before base.OnStarting().
*the idea came from Juval Lowy's example in book - "Programming WCF Services". More information about Faults and Error extensions you can find there.
Here is the working host console application. I don't use IoC there, just Pure DI, but you can easily resolve logger with any IoC you want:
using System;
using System.Collections.ObjectModel;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
namespace ConsoleHost
{
class Program
{
static void Main(string[] args)
{
var logger = new DummyLogger();
var errorHandler = new TestErrorHandler(logger);
ServiceHost host = new TestServiceHost(errorHandler, typeof(TestService), new Uri("net.tcp://localhost:8002"));
host.Open();
Console.WriteLine("Press enter to exit");
Console.ReadKey();
}
}
[ServiceContract]
public interface ITestService
{
[OperationContract]
string Test(int input);
}
public class TestService : ITestService
{
public string Test(int input)
{
throw new Exception("Test exception!");
}
}
public class TestErrorHandler : IErrorHandler
{
private ILogger Logger { get; }
public TestErrorHandler(ILogger logger)
{
Logger = logger;
}
public bool HandleError(Exception error)
{
Logger.Log(error.Message);
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
FaultException fe = new FaultException();
MessageFault message = fe.CreateMessageFault();
fault = Message.CreateMessage(version, message, null);
}
}
public class TestServiceHost : ServiceHost
{
private readonly IErrorHandler errorHandler;
public TestServiceHost(IErrorHandler errorHandler, Type serviceType, params Uri[] baseAddresses)
: base(serviceType, baseAddresses)
{
this.errorHandler = errorHandler;
}
protected override void OnOpening()
{
Description.Behaviors.Add(new ErrorHandlerBehaviour(errorHandler));
base.OnOpening();
}
class ErrorHandlerBehaviour : IServiceBehavior, IErrorHandler
{
private readonly IErrorHandler errorHandler;
public ErrorHandlerBehaviour(IErrorHandler errorHandler)
{
this.errorHandler = errorHandler;
}
bool IErrorHandler.HandleError(Exception error)
{
return errorHandler.HandleError(error);
}
void IErrorHandler.ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
errorHandler.ProvideFault(error, version, ref fault);
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
channelDispatcher.ErrorHandlers.Add(this);
}
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
}
// Dummy logger
public interface ILogger
{
void Log(string input);
}
public class DummyLogger : ILogger
{
public void Log(string input) => Console.WriteLine(input);
}
}
And configuration:
<system.serviceModel>
<services>
<service name="ConsoleHost.TestService">
<endpoint address="net.tcp://localhost:8002/TestService"
binding="netTcpBinding"
contract="ConsoleHost.ITestService" />
</service>
</services>
</system.serviceModel>
Btw. Make sure you added System.Runtime.Serialization to your references
I need to play with request header in a REST, Json, WCF web service.
I create my IDispatchMessageInspector
public class HeaderInspector : IDispatchMessageInspector
{
public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
{
int ind = request.Headers.FindHeader("xxxxx", "");
return null;
}
public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
}
}
Then a endpointbehavio to attach the inspector to endpoints :
public class HeaderInspectorBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
HeaderInspector headerinsp = new HeaderInspector();
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new HeaderInspector());
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
And finally a BehaviorExtensionElement :
public class MyExtension : BehaviorExtensionElement
{
public override Type BehaviorType
{
get { return typeof(HeaderInspectorBehavior); }
}
protected override object CreateBehavior()
{
return new HeaderInspectorBehavior();
}
}
Those classes being in the same file / namespace PDM.WebService
My config is :
<behaviors>
<endpointBehaviors>
<behavior name="RestBehavior">
<HeaderInspectorBehavior/>
<webHttp helpEnabled="true" defaultOutgoingResponseFormat="Json" faultExceptionEnabled="true" automaticFormatSelectionEnabled="false" />
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="HeaderInspectorBehavior" type="PDM.WebService.MyExtension, PDM.WebService, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
Everything compile well, at execution i can catch the execution of method "public override Type BehaviorType",
but no other methods of the code are fired after that (i sent request and i got response, the service respond well).
i set debug point in every methods nothing else execute ! (especially ApplyDispatchBehavior).
Can somebody point out what i'm missing ?
Refer CreateBehavior() is not invoked for a similar issue.
Ensure that name of your service element is corresponding to mynamespace.myservicename
The service will be providing correct response even if you don't have the correct service name; but the CreateBehavior() will be called only when you have the correct name for the service element.
Example
<service
name="WcfServiceApp001.Service1"
behaviorConfiguration="InternalPayrollBehavior">
<endpoint address="" binding="basicHttpBinding"
behaviorConfiguration="EndpointBehavior"
contract="WcfServiceApp001.IService1"
/>
</service>
I'm trying to find a way to handle exception in WCF router, it means when for any reason router can not send message to primary endpoint (here, there is EndpointNotFoundException, ServerTooBusyException, or CommunicationObjectFaultedException) and goes and select an endpoint from backup list and send this message to backup endpoint. how can I get this internal exception in router service? because at this time I want to change the router configuration dynamically in memory and change the backup endpoint with primary endpoint. is it possible to do this with IErrorHandler? or is it possible to do this Custom Filter ? How I can change the backup behavior with Custom Filter?
this is a full working example to implement IErrorHandler fo wcf service. we can implement this for WCF Router and get the internal exception in router level and then make decision how we can change the configuration in the runtime.
[ServiceContract]
public interface IService1
{
[OperationContract]
[FaultContract(typeof(MyFault))]
string GetData(int value);
}
[DataContract]
public class MyFault
{
}
public class Service1 : IService1
{
public string GetData(int value)
{
throw new Exception("error");
}
}
public class MyErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message msg)
{
var vfc = new MyFault();
var fe = new FaultException<MyFault>(vfc);
var fault = fe.CreateMessageFault();
msg = Message.CreateMessage(version, fault, "http://ns");
}
}
public class ErrorHandlerExtension : BehaviorExtensionElement, IServiceBehavior
{
public override Type BehaviorType
{
get { return GetType(); }
}
protected override object CreateBehavior()
{
return this;
}
private IErrorHandler GetInstance()
{
return new MyErrorHandler();
}
void IServiceBehavior.AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
void IServiceBehavior.ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
IErrorHandler errorHandlerInstance = GetInstance();
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
dispatcher.ErrorHandlers.Add(errorHandlerInstance);
}
}
void IServiceBehavior.Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
if (endpoint.Contract.Name.Equals("IMetadataExchange") && endpoint.Contract.Namespace.Equals("http://schemas.microsoft.com/2006/04/mex"))
continue;
foreach (OperationDescription description in endpoint.Contract.Operations)
{
if (description.Faults.Count == 0)
{
throw new InvalidOperationException("FaultContractAttribute not found on this method");
}
}
}
}
}
web.config:
<system.serviceModel>
<services>
<service name="ToDD.Service1">
<endpoint address="" binding="basicHttpBinding" contract="ToDD.IService1" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
<errorHandler />
</behavior>
</serviceBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="errorHandler" type="ToDD.ErrorHandlerExtension, ToDD, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</behaviorExtensions>
</extensions>
</system.serviceModel>
so in HandleError method we can handle this exception in proper way.
I am trying to develop a WCF rest servie which needs authorization using custom attribute.
I want to send the response as 401 status code if the authorization key is not valid in custom attribute which implements IOperationBehavior and IParameterInspector.
can any one tell me how to send the 401 status code as response from the custom attribute.
Here is the implementation
public class AuthorizationAttribute : Attribute,IOperationBehavior,
IParameterInspector
{
#region IOperationBehavior Members
public void ApplyDispatchBehavior(OperationDescription operationDescription,
System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}
#endregion
#region IParameterInspector Members
public object BeforeCall(string operationName, object[] inputs)
{
string publicKey =
WebOperationContext.Current.IncomingRequest.Header["Authorization"];
if (publicKey == "592F2D7E-5E9C-400D-B0AE-1C2603C34137")
{
}
else
{
// Here i would like to send the response back to client
with the status code
}
}
return null;
}
#endregion
}
[Authorization]
public bool signin(string username, string password)
{
}
Throw a WebFormatException
throw new WebFaultException(HttpStatusCode.Unauthorized);
If you're using WCF Rest Starter kit you can also do:
throw new Microsoft.ServiceModel.Web.WebProtocolException(System.Net.HttpStatusCode.Unauthorized, "message", ex);
That method has some more overloads if you need.
public string CreateError(int code ,string description)
{
Context.Response.StatusCode = code;
Context.Response.StatusDescription = description;
Context.ApplicationInstance.CompleteRequest();
return null;
}
Then from your web service methods just use this to return errors example:
return CreateError(403, "Request denied by server");
If aspnetcompatibilityMode cannot be enabled in your WCF services, then you can do as below.
You have to intercept the message and set the status code in HttpResponseMessageProperty in the wcf message. I use a CustomErrorHandler for doing that and it works fine.
public class CustomErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
return true;
}
public void ProvideFault(Exception error, MessageVersion version, ref Message fault)
{
fault.Properties[HttpResponseMessageProperty.Name] = new HttpResponseMessageProperty()
{
StatusCode = statusCode
};
}
}
Below code is for injecting CustomErrorHandler into the ServiceBehavior.
public class CustomServiceBehaviour : IServiceBehavior
{
... other IServiceBehavior methods
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, System.ServiceModel.ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher channelDispatcher in serviceHostBase.ChannelDispatchers)
{
channelDispatcher.ErrorHandlers.Add(new CustomErrorHandler());
}
}
}
Then in web.config use the serviceBehavior
<system.serviceModel>
<extensions>
<behaviorExtensions>
<add name="CustomServiceBehavior" type="MyNamespace.CustomServiceBehavior, MyAssembly" />
</behaviorExtensions>
</extensions>
<behaviors>
<serviceBehaviors>
<behavior name="CustomBehavior">
<CustomServiceBehavior />
</behavior>
</serviceBehaviors>
</behaviors>
<service behaviorConfiguration="CustomBehavior" name="SomeService">
<endpoint ..../>
</service>
</system.serviceModel>