Throw new WebFaultException used in IOperationBehavior does not work - c#

I am throwing WebFaultException when I extend Attribute, IOperationBehavior, IParameterInspector adn it does not work.
public class ApplicationNotSupportedAttribute : Attribute, IOperationBehavior, IParameterInspector
{
private readonly ApplicationNotSupportedBehaviour behaviour;
public ApplicationNotSupportedAttribute()
{
Logging<string>.Error("ApplicationNotSupportedAttribute");
behaviour = new ApplicationNotSupportedBehaviour();
}
public void AddBindingParameters(OperationDescription operationDescription, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
behaviour.AddBindingParameters(operationDescription, bindingParameters);
}
public void ApplyClientBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.ClientOperation clientOperation)
{
behaviour.ApplyClientBehavior(operationDescription, clientOperation);
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, System.ServiceModel.Dispatcher.DispatchOperation dispatchOperation)
{
behaviour.ApplyDispatchBehavior(operationDescription, dispatchOperation);
}
public void Validate(OperationDescription operationDescription)
{
behaviour.Validate(operationDescription);
}
public object BeforeCall(string operationName, object[] inputs)
{
throw new WebFaultException(HttpStatusCode.Forbidden);
}
public void AfterCall(string operationName, object[] outputs,
object returnValue, object correlationState)
{
}
Then I have the in the service:
[WebInvoke(Method = "POST", UriTemplate = "myMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[OperationContract]
[ApplicationNotSupportedAttribute]
myMethodResult myMethod(MyMethodRequest myMethodRequest);
After I run the code from above I get 200 Ok. If I add
throw new WebFaultException(HttpStatusCode.Forbidden);
in myMethod I get 403 Forbidden. As in the bellow code:
public myMethodResult myMethod(MyMethodRequest myMethodRequest)
{
throw new WebFaultException<myObject>(myObj, HttpStatusCode.Forbidden);
}
It is somehow possible to get the 403 before we actually get inside the myMethod?

I've found the issue in class ApplicationNotSupportedAttribute all the overwritten methods could have been empty except BeforeCall and ApplyDispatchBehavior which should look like:
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(this);
}
public object BeforeCall(string operationName, object[] inputs)
{
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.Forbidden;
throw new WebFaultException<string>("Unauthorized", HttpStatusCode.Forbidden);
}
Seems I've missed the dispatchOperation.ParameterInspectors.Add(this). This solved my problem.

Related

c# func parameter not being used

I have a legacy app that logs the input / output of services. Currently, every method has the same lines to log the request and response objects. I would like to use AOP, but without adding any extra tool (Postsharp, Castle, etc), or wrap every service class into another class (ServiceWrapper).
In order to do that, I'm trying to create a Generic class that knows that it should log the request and response objects. Here's what I'm trying:
using System;
namespace ProxyTest
{
class Program
{
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(r => fooService.DoFoo(request), "abc");
Console.Read();
}
}
class ServiceProxy
{
public static void Invoke(Func<object, object> service, object request)
{
Console.WriteLine("input:" + request);
var response = service(request);
Console.WriteLine("output:" + response);
}
}
class FooService
{
public string DoFoo(object a)
{
return a + ": returning: Do Foo";
}
}
}
Although it's working, the "abc" string is just to compile the application, but it's not being used as the request parameter. If I remove that, the code does not compile. Am I missing something?
UPDATE
Changing to the following did the trick:
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(r => fooService.DoFoo(r), request);
Console.Read();
}
You should call it like this:
class Program
{
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(fooService.DoFoo, "abc"); // lose the DoFoo parameter.
Console.Read();
}
}
You should pass the DoFoo as Func, instead of calling it. Also you should change the method signature to:
class FooService
{
public object DoFoo(object a)
{
return a + ": returning: Do Foo";
}
}
For this task you can just add logging behavior on dispatcher.
First, you create ServiceBehavior with such content:
public class ServiceLoggingBehavior : Attribute, IServiceBehavior
{
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ServiceEndpoint endpoint in serviceDescription.Endpoints)
{
foreach (OperationDescription operation in endpoint.Contract.Operations)
{
IOperationBehavior behavior = new LoggingOperationBehavior();
operation.Behaviors.Add(behavior);
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
Then you need to create operation behavior:
internal class LoggingOperationBehavior : IOperationBehavior
{
public void Validate(OperationDescription operationDescription)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.Invoker = new LoggingOperationInvoker(dispatchOperation.Invoker, dispatchOperation);
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
}
And finally create invoker for all methods on server side:
internal class LoggingOperationInvoker : IOperationInvoker
{
private readonly IOperationInvoker _baseInvoker;
private readonly string _operationName;
public LoggingOperationInvoker(IOperationInvoker baseInvoker, DispatchOperation operation)
{
_baseInvoker = baseInvoker;
_operationName = operation.Name;
}
public bool IsSynchronous
{
get { return _baseInvoker.IsSynchronous; }
}
public object[] AllocateInputs()
{
return _baseInvoker.AllocateInputs();
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
var sw = new Stopwatch();
try
{
LogBegin();
sw.Start();
var response = _baseInvoker.Invoke(instance, inputs, out outputs);
return response;
}
finally
{
sw.Stop();
LogEnd(sw.Elapsed);
}
}
private void LogBegin()
{
//you can log begin here.
}
private void LogEnd(TimeSpan elapsed)
{
//you can log end here.
}
public IAsyncResult InvokeBegin(object instance, object[] inputs, AsyncCallback callback, object state)
{
return _baseInvoker.InvokeBegin(instance, inputs, callback, state);
}
public object InvokeEnd(object instance, out object[] outputs, IAsyncResult result)
{
return _baseInvoker.InvokeEnd(instance, out outputs, result);
}
}
If you want to log request, you can just serialize and log inputs variable in Invoke method. For response - just serialize and log response variable.
And finaly, most enjoyable part, just attach it like attribute:
[ServiceLoggingBehavior]
public MyService : IMyServiceContract
{
...
}
Your Invoke-method clearly asks for a Func- and an object-parameter, so you have to provide both. No idea what exactly you´re expecting when you omit one of the params. I assume you want to make the Func to return the response created by a specific request-object. Furthermore it might be good idea to make your request- and response-arguments generic:
class Program
{
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(r => fooService.DoFoo(r), request);
Console.Read();
}
}
class ServiceProxy
{
public static void Invoke<TRequest, TResponse>(Func<TRequest, TResponse> service, TRequest request)
{
Console.WriteLine("input:" + request.ToString());
var response = service(request);
Console.WriteLine("output:" + response.ToString());
}
}
The Invoke-call can further be simplyfied to ServiceProxy.Invoke(fooService.DoFoo, request);
Thanks for all the responses. I was able to achieve what I was looking for using:
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(fooService.DoFoo, request);
Console.Read();
}
or
static void Main(string[] args)
{
var request = "request";
var fooService = new FooService();
ServiceProxy.Invoke(r => fooService.DoFoo(r), request);
Console.Read();
}

How to pass username and password to the SOAP header of a Web Service Call from C#?

I want to write a C# code which calls a (remote) web service in another machine. For this I have to pass username and password in the SOAP header of the call.
I would like to know an example of code to make this in C#.
the produced XML should be like :
<env:Header>
<ns1:Security>
<ns1:UsernameToken>
<ns1:Username>XXXXXXXXXXXXXXXX</ns1:Username>
<ns1:Password>YYYYYYYYYYYYYYYY</ns1:Password>
</ns1:UsernameToken>
</ns1:Security>
</env:Header>
Thanks in advance
J.
Are many ways to do that. The CustomBinding is more flexible because it allow more controll, for that i propose you with that. Pasing header to endpoint is a simple way:
// binding
var binding = new CustomBinding();
binding.Elements.Clear();
binding.Elements.Add(new TextMessageEncodingBindingElement{MessageVersion = MessageVersion.Soap12});
binding.Elements.Add(new HttpTransportBindingElement{MaxReceivedMessageSize = 20000000,});
// endpoint
var endpoint = new EndpointAddress(new Uri(listeningUri), new MySecurityHeader())
var client = new Client(binding, endpoint);
client.SomeMethod();
where MySecurityHeader is an AddressHeader, for example:
public class MySecurityHeader : AddressHeader
{
public override string Name
{
get { return "Security"; }
}
public override string Namespace
{
get { return "<provide the appropiate namespace>"; }
}
protected override void OnWriteAddressHeaderContents(System.Xml.XmlDictionaryWriter writer)
{
// here you do what you want
writer.WriteRaw(String.Format(#"
<UsernameToken xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<Username>user</Username>
<Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"">pass</Password>
</UsernameToken>").Trim());
}
}
this is an example using an IEndpointAdress
var customBinding = new CustomBinding();
customBinding.Elements.Add(new TextMessageEncodingBindingElement { MessageVersion = MessageVersion.Soap12, });
customBinding.Elements.Add(new HttpTransportBindingElement { MaxReceivedMessageSize = 20000000, });
var endpointAddres = new EndpointAddress(listeningUri);
var client = new Client(customBinding, endpointAddres);
// add my own IEndpointBehavior
client.ChannelFactory.Endpoint.Behaviors.Add(new CustomBehavior());
client.SomeMethod();
and this is the CustomBehavior definition
public class CustomBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{}
public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
{
var inspector = new CustomMessageInspector();
clientRuntime.MessageInspectors.Add(inspector);
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
{}
public void Validate(ServiceEndpoint endpoint)
{}
}
public class CustomMessageInspector : IClientMessageInspector
{
public void AfterReceiveReply(ref Message reply, object correlationState)
{}
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
request.Headers.Add(new MyMessageHeader());
return null;
}
}
public class MyMessageHeader : MessageHeader
{
protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
{
writer.WriteRaw(String.Format(#"
<UsernameToken xmlns=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"">
<Username>user</Username>
<Password Type=""http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordDigest"">pass</Password>
</UsernameToken>").Trim());
}
public override string Name
{
get { return "MyHeaderName"; }
}
public override string Namespace
{
get { return "MyHeaderNamespace"; }
}
}
Note you have control before send the request and after receive your reply.
I hope this resolve your issue, if you have some problems with this yust ask me.

How to handle exceptions thrown in IParameterInspector?

I've been looking all over the place and I can't seem to find the answer. I have an extension endpoint behaviour which uses IParameterInspector. How can I handle an exception when it's thrown in BeforeCall method?
I've tried adding try-catch to IEndPointBehavior and BehaviorExtensionElement neither of which handles it. Here is some code:
BehaviorExtensionElement:
public class ExtensionService : BehaviorExtensionElement
{
protected override object CreateBehavior()
{
//try-catch doesn't work here
return new ExtensionBehavior();
}
public override Type BehaviorType
{
get { return typeof(ExtensionBehavior); }
}
}
IEndpointBehavior:
public class ExtensionBehavior : IEndpointBehavior
{
public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
//throw new NotImplementedException();
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
foreach (ClientOperation clientOperation in clientRuntime.ClientOperations)
{
//try-catch here doesn't work
clientOperation.ClientParameterInspectors.Add(new ParamInspector());
}
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (DispatchOperation dispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
{
//try-catch here doesn't work
dispatchOperation.ParameterInspectors.Add(new ParamInspector());
}
}
public void Validate(ServiceEndpoint endpoint)
{
//throw new NotImplementedException();
}
}
IParameterInspector
public class ParamInspector : IParameterInspector
{
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
}
public object BeforeCall(string operationName, object[] inputs)
{
///an exception is thrown here
return null;
}
}
I did finally manage to solve it. I had to implement IErrorHandler like so:
public class CustomErrorHandler : IErrorHandler
{
public bool HandleError(Exception error)
{
//the procedure for handling the errors.
//False is returned because every time we have an exception we want to abort the session.
return false;
}
public void ProvideFault(Exception error, System.ServiceModel.Channels.MessageVersion version, ref System.ServiceModel.Channels.Message fault)
{
}
}
and then add this IErrorHandler to ApplyDispatchBehavior
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
foreach (DispatchOperation dispatchOperation in endpointDispatcher.DispatchRuntime.Operations)
{
dispatchOperation.ParameterInspectors.Add(new ParamInspector(this.Class));
}
endpointDispatcher.ChannelDispatcher.ErrorHandlers.Add(new CustomErrorHandler());
}

Action Filters in WCF Services

Is there something Like ActionFilterAttribute (From ASP.NET MVC) in WCF Services (Or anything like that). basically what I want to do is to log what is coming and goint to and from my services, and I don't want to write the logging code in every single ServiceContracts. yes the question is very general but you understand the idea what I want to do.
Yes there is it called as MessageInspectors/ParameterInspectors , here you can read about them
http://msdn.microsoft.com/en-us/library/aa717047%28v=vs.110%29.aspx
This is exactly what you are looking for , WCF custom behavior log logging
http://www.codeproject.com/Articles/243352/LoggingBehavior-How-to-Connect-Log-prints-with-the
Only confusing thing is you can have message inspector on WCF service and WCF proxy as well , in your case you need only for service side
I had to read a lot to find this out, I'm not an expert in WCF but given this information is a little scarce I'm sharing what have worked for me.
My Solution consists of using an OperationBehavior and a DispatcherMessageInspector
OperationBehavior
Allows you to change the binding information, validate de operation
description, and apply dispatcher behaviors.
DispatcherMessageInspector
Allows you to inspect and change the messages that are sent for your
service.
Dispatcher
Gets the messages from the communication channels and sends to the
right operation, and get the result back to the caller.
Service Operation
are your service methods
CODE SOLUTION
MESSAGE INSPECTOR
public class MyMessageInspector : IDispatchMessageInspector
{
List<string> targetOperations = new List<string>();
public MyMessageInspector(OperationDescription operation)
{
this.AddOperation(operation);
}
public void AddOperation(OperationDescription operation)
{
this.targetOperations.Add(operation.Messages[0].Action);
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
if (TargetOperationMatchesRequest(request))
{
request = ChangeMessage(request);
return true;
}else
{
return false;
}
}
public bool TargetOperationMatchesRequest(Message request)
{
string requestAction = request.Headers.To.AbsolutePath;
requestAction = requestAction.Substring(requestAction.LastIndexOf("/"));
string targetOperation = "";
foreach (string targetOperationPath in targetOperations)
{
targetOperation = targetOperationPath.Substring(targetOperationPath.LastIndexOf("/"));
if (targetOperation.Equals(requestAction))
{
return true;
}
}
return false;
}
public Message ChangeMessage(Message oldMessage)
{
Message newMessage = request.CreateBufferedCopy(Int32.MaxValue).CreateMessage();
//Change your message
return newMessage;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
}
OPERATION
public class MyOperation : Attribute, IOperationBehavior
{
public void AddBindingParameters(OperationDescription operationDescription, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
MyMessageInspector inspector = dispatchOperation.Parent.MessageInspectors
.Where(x => x is MyMessageInspector)
.FirstOrDefault() as MyMessageInspector;
if (inspector != null)
{
inspector.AddOperation(operationDescription);
}
else
{
inspector = new MessageInspectors(operationDescription);
dispatchOperation.Parent.MessageInspectors.Add(inspector);
}
}
public void Validate(OperationDescription operationDescription)
{
}
}
CONTRACT
[ServiceContract]
public interface IService
{
[OperationContract]
[MyOperation]
OutputData MyMethod(InputData inputData);
}
SERVICE
public class Service : IService
{
[WebInvoke(Method = "POST", UriTemplate = "/json/MyMethod", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
public OutputData MyMethod(InputData inputData)
{
//Implementation
return new OutputData();
}
}

Add behaviorattribute to a WorkflowServiceHost

Hi all i have a problem while adding a custom behavior to a WorkflowServiceHost.
Here is my WorflowServiceHostFactory:
public class ScoringWorkflowServiceHostFactory : WorkflowServiceHostFactory, IServiceHost<IKernel>
{
private static IKernel _InjectionInstance;
public IKernel InjectionInstance
{
get { return _InjectionInstance ?? (_InjectionInstance = new StandardKernel(new ScoringWorkflowServicesNinjectModule(Scope))); }
}
public object Scope
{
get { return Guid.NewGuid(); }
}
public override ServiceHostBase CreateServiceHost(string constructorString, Uri[] baseAddresses)
{
String fullFilePath = Path.Combine(HostingEnvironment.ApplicationPhysicalPath, constructorString);
WorkflowService wf = CSharpExpressionCompiler.Compile(fullFilePath);
System.ServiceModel.Activities.WorkflowServiceHost host = base.CreateWorkflowServiceHost(wf, baseAddresses);
NinjectBehaviorAttributeWF behavior = new NinjectBehaviorAttributeWF(wf);
host.Description.Behaviors.Add(behavior);
host.AddNinjectResolverExtension(InjectionInstance, Scope);
TypeAdapterFactory.SetCurrent(new SvcMapperAdapterFactory());
LoggerFactory.SetCurrent(new EntLibLoggerFactory());
return host;
}
}
Here is my behavior:
public class NinjectBehaviorAttributeWF : Attribute, IServiceBehavior
{
private System.ServiceModel.Activities.WorkflowService host;
public NinjectBehaviorAttributeWF(System.ServiceModel.Activities.WorkflowService host)
{
this.host = host;
}
public void AddBindingParameters(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase, Collection<ServiceEndpoint> endpoints, BindingParameterCollection bindingParameters)
{
}
public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
foreach (ChannelDispatcher dispatcher in serviceHostBase.ChannelDispatchers)
{
foreach (EndpointDispatcher endpointDispatcher in dispatcher.Endpoints)
{
DispatchRuntime dispatchRuntime = endpointDispatcher.DispatchRuntime;
dispatchRuntime.InstanceContextProvider = new PerCallInstanceContextProvider(dispatchRuntime);
}
}
}
public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
{
}
}
In this way, i have an error while loading my service(xamlx): The service type provided could not be loaded as a service because it does not have a default (parameter-less) constructor. To fix the problem, add a default constructor to the type, or pass an instance of the type to the host.
I don't know neither it's possible, nor how can create the default constructor for a workflowservice, (because the real implementation is the xamlx and not a simple class)
So, I've tried with a custom Provider:
dispatchRuntime.InstanceProvider = new CustomInstanceProvider(host.Body);
where CustomInstanceProvider is:
public class CustomInstanceProvider : IInstanceProvider
{
string message;
private System.Activities.Activity activity;
public CustomInstanceProvider(string msg)
{
Console.WriteLine("The non-default constructor has been called.");
this.message = msg;
}
public CustomInstanceProvider(System.Activities.Activity activity)
{
this.activity = activity;
}
public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
{
Console.WriteLine("GetInstance is called:");
return this.activity;
}
public object GetInstance(InstanceContext instanceContext)
{
Console.WriteLine("GetInstance is called:");
return this.activity;
}
public void ReleaseInstance(InstanceContext instanceContext, object instance)
{
Console.WriteLine("ReleaseInstance is called");
}
}
But i have this error:
System.InvalidCastException: Unable to cast object of type 'System.ServiceModel.Activities.WorkflowService' to type 'IHttpGetMetadata'.
How can I resolve my problem? Thanks a lot

Categories