|
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.ServiceModel.Channels;
using System.ServiceModel.Dispatcher;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
namespace Microsoft.Jsonp
{
public class JSONPBehavior : Attribute, IOperationBehavior
{
public string callback;
#region IOperationBehavior Members
public void AddBindingParameters(
OperationDescription operationDescription, BindingParameterCollection bindingParameters
)
{ return; }
public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
{
clientOperation.ParameterInspectors.Add(new Inspector(callback));
}
public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
{
dispatchOperation.ParameterInspectors.Add(new Inspector(callback));
}
public void Validate(OperationDescription operationDescription) { return; }
#endregion
//Parameter inspector
class Inspector : IParameterInspector
{
string callback;
public Inspector(string callback)
{
this.callback = callback;
}
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
}
public object BeforeCall(string operationName, object[] inputs)
{
string methodName = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters[callback];
if(methodName !=null)
{
//System.ServiceModel.OperationContext.Current.OutgoingMessageProperties["wrapper"] = inputs[0];
JSONPMessageProperty property = new JSONPMessageProperty()
{
MethodName = methodName
};
OperationContext.Current.OutgoingMessageProperties.Add(JSONPMessageProperty.Name, property);
}
return null;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Kirill Balandin is a professional software developer, software architect and an accomplished leader of small teams of software developers. He lives in the Greater Los Angeles area, CA. He is an unofficial Microsoft technology evangelist.