I have a wcf rest service on IIS 7.5. When someone visits a part of the endpoint that doesn't exist (i.e. http://localhost/rest.svc/DOESNOTEXIST vs http://localhost/EXISTS) they are presented with a Generic WCF gray and blue error page with status code 404. However, I would like to return something like the following:
<service-response>
<error>The url requested does not exist</error>
</service-response>
I tried configuring the custom errors in IIS, but they only work if requesting a page outside of the rest service (i.e. http://localhost/DOESNOTEXIST).
Does anyone know how to do this?
Edit
After the answer below I was able to figure out I needed to create a WebHttpExceptionBehaviorElement class that implements BehaviorExtensionElement.
public class WebHttpExceptionBehaviorElement : BehaviorExtensionElement
{
///
/// Get the type of behavior to attach to the endpoint
///
public override Type BehaviorType
{
get
{
return typeof(WebHttpExceptionBehavior);
}
}
///
/// Create the custom behavior
///
protected override object CreateBehavior()
{
return new WebHttpExceptionBehavior();
}
}
I was then able to reference it in my web.config file via:
<extensions>
<behaviorExtensions>
<add name="customError" type="Service.WebHttpExceptionBehaviorElement, Service"/>
</behaviorExtensions>
</extensions>
And then adding
<customError />
to my default endpoint behaviors.
Thanks,
Jeffrey Kevin Pry
First, create a custom behavior which subclasses WebHttpBehavior - here you will remove the default Unhandled Dispatch Operation handler, and attach your own:
public class WebHttpBehaviorEx : WebHttpBehavior
{
public override void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
base.ApplyDispatchBehavior(endpoint, endpointDispatcher);
endpointDispatcher.DispatchRuntime.Operations.Remove(endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation);
endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation = new DispatchOperation(endpointDispatcher.DispatchRuntime, "*", "*", "*");
endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.DeserializeRequest = false;
endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.SerializeReply = false;
endpointDispatcher.DispatchRuntime.UnhandledDispatchOperation.Invoker = new UnknownOperationInvoker();
}
}
Then. make your unknown operation handler. This class will handle the unknown operation request and generate a "Message" which is the response. I've shown how to create a plain text message. Modifying it for your purposes should be fairly straight-forward:
internal class UnknownOperationInvoker : IOperationInvoker
{
public object[] AllocateInputs()
{
return new object[1];
}
private Message CreateTextMessage(string message)
{
Message result = Message.CreateMessage(MessageVersion.None, null, new HelpPageGenerator.TextBodyWriter(message));
result.Properties["WebBodyFormatMessageProperty"] = new WebBodyFormatMessageProperty(WebContentFormat.Raw);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
return result;
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
// Code HERE
StringBuilder builder = new System.Text.StringBuilder();
builder.Append("...");
Message result = CreateTextMessage(builder.ToString());
return result;
}
public System.IAsyncResult InvokeBegin(object instance, object[] inputs, System.AsyncCallback callback, object state)
{
throw new System.NotImplementedException();
}
public object InvokeEnd(object instance, out object[] outputs, System.IAsyncResult result)
{
throw new System.NotImplementedException();
}
public bool IsSynchronous
{
get { return true; }
}
}
At this point you have to associate the new behavior with your service.
There are several ways to do that, so just ask if you don't already know, and i'll happily elaborate further.
I had a similar problem, and the other answer did lead to my eventual success, it was not the clearest of answers. Below is the way I solved this issue.
My projects setup is a WCF service hosted as a svc hosted in IIS. I could not go with the configuration route for adding the behavior, because my assembly versions change every checkin due to continuous integration.
to overcome this obsticle, I created a custom ServiceHostFactory:
using System.ServiceModel;
using System.ServiceModel.Activation;
namespace your.namespace.here
{
public class CustomServiceHostFactory : WebServiceHostFactory
{
protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
{
ServiceHost host = base.CreateServiceHost(serviceType, baseAddresses);
//note: these endpoints will not exist yet, if you are relying on the svc system to generate your endpoints for you
// calling host.AddDefaultEndpoints provides you the endpoints you need to add the behavior we need.
var endpoints = host.AddDefaultEndpoints();
foreach (var endpoint in endpoints)
{
endpoint.Behaviors.Add(new WcfUnkownUriBehavior());
}
return host;
}
}
}
As you can see above, we are adding a new behavior: WcfUnknownUriBehavior. This new custom behavior's soul duty is to replace the UnknownDispatcher. below is that implementation:
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Channels;
using System.ServiceModel.Web;
namespace your.namespace.here
{
public class UnknownUriDispatcher : IOperationInvoker
{
public object[] AllocateInputs()
{
//no inputs are really going to come in,
//but we want to provide an array anyways
return new object[1];
}
public object Invoke(object instance, object[] inputs, out object[] outputs)
{
var responeObject = new YourResponseObject()
{
Message = "Invalid Uri",
Code = "Error",
};
Message result = Message.CreateMessage(MessageVersion.None, null, responeObject);
WebOperationContext.Current.OutgoingResponse.ContentType = "text/html";
outputs = new object[1]{responeObject};
return result;
}
public System.IAsyncResult InvokeBegin(object instance, object[] inputs, System.AsyncCallback callback, object state)
{
throw new System.NotImplementedException();
}
public object InvokeEnd(object instance, out object[] outputs, System.IAsyncResult result)
{
throw new System.NotImplementedException();
}
public bool IsSynchronous
{
get { return true; }
}
}
}
Once you have these objects specified, you can now use the new factory within your svc's "markup":
<%# ServiceHost Language="C#" Debug="true" Service="your.service.namespace.here" CodeBehind="myservice.svc.cs"
Factory="your.namespace.here.CustomServiceHostFactory" %>
And that should be it. as long as your object "YourResponseObject" can be serialized, it's serialized representation will be sent back to the client.
Related
Following is the exact scenario in my application.
I have used ServiceStack 3.9.48 and AutoFac 4.6.0 to develop a REST service.
Following is the code of AppHost which is inherited from AppHostBase
public AppHost()
:base("My Service Host", typeof(NotificationService).Assembly)
{
var builder = new ContainerBuilder();
builder.RegisterType<ConfigurationProvider>().As<IConfigurationProvider>();
builder.RegisterType<Logging>().As<ILogging>();
IoCContainer = builder.Build();
}
public override void Configure(Container container)
{
using (var scope = IoCContainer.BeginLifetimeScope())
{
var _logging = scope.Resolve<ILogging>();
JsConfig.EmitCamelCaseNames = true;
base.RequestFilters.Add(delegate (IHttpRequest req, IHttpResponse res, object dto)
{
HandleUncaughtExceptionDelegate uncaughtExceptionDelegate = null;
if (DateTime.Now.Year <= 2019)
{
if (uncaughtExceptionDelegate == null)
{
uncaughtExceptionDelegate = delegate (IHttpRequest request, IHttpResponse response, string operationName, Exception ex)
{
res.StatusCode = 0x191;
res.Write("Error: This service is unavailable till 2019: " + operationName);
};
}
base.ExceptionHandler = uncaughtExceptionDelegate;
HttpResponse originalResponse = res.OriginalResponse as HttpResponse;
originalResponse.SuppressFormsAuthenticationRedirect = false;
res.End();
}
});
base.ServiceExceptionHandler = delegate (object request, Exception exception)
{
_logging.Log(exception);
return DtoUtils.HandleException(this, request, exception);
};
}
}
I can see this code working fine, and logging the exception if the condition is not satisfied.
However, there is an issue when I try to make a call to the API endpoint which invokes following:
public class NotificationService: Service
{
private IConfigurationProvider _configurationProvider;
public NotificationService(IConfigurationProvider _configurationProvider)
{
_configurationProvider = configurationProvider;
}
public object Post(SendEventNotification request)
{
return new SendEventNotificationResponse { SentStatus = SendNotification(_configurationProvider.GetValue("EncId")) };
}
}
It gives me an error saying -
Required dependency of type IConfigurationProvider could not be
resolved.
Can anyone please suggest what could be the reason here? I believe the instances which were initialized during AppHost have not been persisted.
I am sure, something is missing, but unable to figure it out.
Any help on this will be much appreciated.
Thanks and Regards,
Nirman
I figured it out an issue of ServiceStack only. There was no need to use Autofac as Servicestack itself provides DI resolution. Also, I had to use "RegisterAutoWiredAs" method of ServiceStack's Container object.
I'm using Wcf data service(V3). From IOS App they will send Signature through URL. Problem is sometimes user enters long signature in that situation it is giving an error like "Url is too long". how can i fix this issue on wcf data services.
Advance Thanks.
If the message client want to give to service is large, it is recommended to use POST.
You can find the guide for Actions in WCF Data Service V3 here:
http://blogs.msdn.com/b/odatateam/archive/2011/10/17/actions-in-wcf-data-services.aspx
And here is quick demo for setting up a WCF DS service with Action support:
public class Service : DataService<Context>, IServiceProvider
{
public static void InitializeService(DataServiceConfiguration config)
{
config.SetEntitySetAccessRule("*", EntitySetRights.All);
config.SetServiceActionAccessRule("*", ServiceActionRights.Invoke);
config.DataServiceBehavior.MaxProtocolVersion = DataServiceProtocolVersion.V3;
}
public object GetService(Type serviceType)
{
return typeof(IDataServiceActionProvider) == serviceType ? new ActionProvider() : null;
}
}
public class ActionProvider : IDataServiceActionProvider, IDataServiceActionResolver
{
private static List<ServiceAction> actions;
static ActionProvider()
{
ServiceAction movieRateAction = new ServiceAction(
"Action1", // name of the action
ResourceType.GetPrimitiveResourceType(typeof(string)), // no return type i.e. void
null, // no return type means we don’t need to know the ResourceSet so use null.
OperationParameterBindingKind.Never,
new ServiceActionParameter[] {
new ServiceActionParameter("val", ResourceType.GetPrimitiveResourceType(typeof(string)))
}
);
movieRateAction.SetReadOnly();
actions = new List<ServiceAction>() { movieRateAction };
}
public IEnumerable<ServiceAction> GetServiceActions(DataServiceOperationContext operationContext)
{
return actions;
}
public bool TryResolveServiceAction(DataServiceOperationContext operationContext, string serviceActionName,
out ServiceAction serviceAction)
{
serviceAction = null;
return false;
}
public IEnumerable<ServiceAction> GetServiceActionsByBindingParameterType(DataServiceOperationContext operationContext,
ResourceType bindingParameterType)
{
return Enumerable.Empty<ServiceAction>();
}
public IDataServiceInvokable CreateInvokable(DataServiceOperationContext operationContext, ServiceAction serviceAction,
object[] parameterTokens)
{
return new DataServiceInvokable(parameterTokens);
}
public bool AdvertiseServiceAction(DataServiceOperationContext operationContext, ServiceAction serviceAction, object resourceInstance, bool resourceInstanceInFeed, ref ODataAction actionToSerialize)
{
actionToSerialize = null;
return false;
}
public bool TryResolveServiceAction(DataServiceOperationContext operationContext, ServiceActionResolverArgs resolverArgs, out ServiceAction serviceAction)
{
serviceAction = actions[0];
return true;
}
}
public class DataServiceInvokable : IDataServiceInvokable
{
private readonly object[] parameters;
private string result;
public DataServiceInvokable(object[] parameters)
{
this.parameters = parameters;
}
public object GetResult()
{
return result;
}
public void Invoke()
{
result = parameters[0] as string;
}
}
Then you could send a POST request to http://example.org/service.svc/Action1
Header:
Content-Type: Application/json
Request Body:
{"val":"MessageToPostHere..."}
If you are using .Net 4.0 or above, you could experiment with your web.config settings file, with this:
<system.web>
...
<httpRuntime maxUrlLength="500" />
....
</system.web>
In line with the ServiceStack documentation, we have a global service exception handler. The docs say that this handler should log the exception then call DtoUtils.HandleException, like this:
private object LogServiceException(object request, Exception exception)
{
var message = string.Format("Here we make a custom message...");
_logger.Error(message, exception);
return DtoUtils.HandleException(this, request, exception);
}
This results in the error being logged twice, since DTOUtils.HandleException also logs it, in a less customised format. Yes, I much prefer this to the DTOUtils logging and don't want to just use that.
How do we turn off DTOUtils logging while retaining the rest of the functionality? Nobody likes getting twice as many error emails as they should.
I hope the following code solves your problem.
based on the documentation New API, Custom Hooks, ServiceRunner
and Fine grain error handling using the New API's ServiceRunner
in AppHost.Configure
LogManager.LogFactory = new ServiceStack.Logging.Support.Logging.ConsoleLogFactory();
then in AppHost class
public override IServiceRunner<TRequest> CreateServiceRunner<TRequest>(ActionContext actionContext)
{
return new MyServiceRunner<TRequest>(this, actionContext);
}
in the ServiceRunner class
public class MyServiceRunner<T> : ServiceRunner<T>
{
public override object HandleException(IRequestContext requestContext, T request, Exception ex)
{
if ( isYourCondition )
{
ResponseStatus rs = new ResponseStatus("error1", "your_message");
// optionally you can add custom response errors
rs.Errors = new List<ResponseError>();
rs.Errors.Add(new ResponseError());
rs.Errors[0].ErrorCode = "more details 2";
// create an ErrorResponse with the ResponseStatus as parameter
var errorResponse = DtoUtils.CreateErrorResponse(request, ex, rs);
// log the error
Log.Error("your_message", ex);
return errorResponse;
}
else
return base.HandleException(requestContext, request, ex);
}
}
if you return the base.HandleException, it calls internally the DtoUtils.HandleException.
You will see in console, one log error only.
In client, if you handle the WebServiceException for custom errors.
catch (WebServiceException err)
{
if ( err.ResponseStatus.Errors != null)
{ // do something with err.ResponseStatus.Errors[0].ErrorCode;
}
}
Do not call DtoUtils.HandleException as it logs the error. Don't call ServiceRunner.HandleException either, it calls DtoUtils.HandleException.
Call DtoUtils.CreateErrorResponse to make the response (it is used by DtoUtils.HandleException). The ToResponseStatus helper is also in DtoUtils
My AppServiceRunner is now like this:
public class AppServiceRunner<T> : ServiceRunner<T>
{
public AppServiceRunner(AppHost appHost, ActionContext actionContext)
: base(appHost, actionContext)
{
}
public override object HandleException(IRequestContext requestContext,
T request, Exception ex)
{
LogException(requestContext, request, ex);
var responseStatus = ex.ToResponseStatus();
return DtoUtils.CreateErrorResponse(request, ex, responseStatus);
}
private void LogException(IRequestContext requestContext, T request, Exception ex)
{
// since AppHost.CreateServiceRunner can be called before AppHost.Configure
// don't get the logger in the constructor, only make it when it is needed
var logger = MakeLogger();
var requestType = typeof(T);
var message = string.Format("Exception at URI:'{0}' on service {1} : {2}",
requestContext.AbsoluteUri, requestType.Name, request.ToJson());
logger.Error(message, ex);
}
private static ILog MakeLogger()
{
return LogManager.GetLogger(typeof(AppServiceRunner<T>));
}
}
Now the only service errors that I get are those generated by this code.
I have implemented Proto-buf.net over WCF and my WCF services are now using this as their serialiser, all good so far.
However, I need to add some information from the message into the HTTP headers so that these packets can be tracked around the network.
I have implemented a Message Inspector that allows me to add Header information, however at this point the message has already been ran through the proto-buf serialiser and is no longer readable.
Is it possible to intercept the message before serialisation and still have access to the HttpRequestMessage Headers? If not can I put some information about the request somewhere that will be accessible from the message inspector?
Many thanks
Yarons comment got me moving in the right direction
A ParameterInspector pulls out the value and places it onto the operationContext using a cusom operationContext extension
public class TestParameterInspector : IParameterInspector
{
public object BeforeCall(string operationName, object[] inputs)
{
return null;
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
OperationContext.Current.Extensions.Add(new ContextSessionExtension() {SomeData = "testme"} );
}
}
public class ContextSessionExtension : IExtension<OperationContext>
{
public void Attach(OperationContext owner)
{
}
public void Detach(OperationContext owner)
{
}
public string SomeData { get; set; }
}
The value is then pulled out and placed into the HTTP headers using a MessageInspector
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
return null;
}
public void AfterReceiveReply(ref Message reply, object correlationState)
{
}
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
var test = OperationContext.Current.Extensions.Find<ContextSessionExtension>().SomeData;
object httpRequestMessageObject;
HttpRequestMessageProperty httpRequestMessage;
if (reply.Properties.TryGetValue(HttpRequestMessageProperty.Name, out httpRequestMessageObject))
{
httpRequestMessage = httpRequestMessageObject as HttpRequestMessageProperty;
if (string.IsNullOrEmpty(httpRequestMessage.Headers["MYTEST"]))
{
httpRequestMessage.Headers["MYTEST"] = test;
}
}
else
{
httpRequestMessage = new HttpRequestMessageProperty();
httpRequestMessage.Headers.Add("MYTEST", test);
reply.Properties.Add(HttpRequestMessageProperty.Name, httpRequestMessage);
}
}
I have an api which will be publicly exposed and have a sandbox. I've written some code in my ResourceFactory so api.sandbox.whatever/whatever will work and also sandbox=true in the arguments will work but this feels like a giant hack. Any better ways to do it?
Here is my code:
public class NinjectResourceFactory : IResourceFactory
{
private readonly IKernel _productionKernel;
private readonly IKernel _sandboxKernel;
public NinjectResourceFactory()
{
_productionKernel = new StandardKernel(new QueryMasterModule());
_sandboxKernel = new StandardKernel(new QueryMasterModule(true));
}
public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
{
string uri = request.RequestUri.ToString();
if (uri.Contains(".sandbox."))
{
return _sandboxKernel.Get(serviceType);
}
else if (uri.Contains("sandbox=true"))
{
request.RequestUri = new Uri(uri.Replace("sandbox=true", ""));
return _sandboxKernel.Get(serviceType);
}
else
{
return _productionKernel.Get(serviceType);
}
}
public void ReleaseInstance(InstanceContext instanceContext, object service)
{
// todo do I need to implement this?
}
}
If it's supposed to be a true sandbox then you don't want the two sites to run in the same process. I would deploy two web sites and let IIS decide which one based on host name. That way the sandbox will be isolated from production, which is the purpose of a sandbox.