我有一个WCF Rest服务,在调用服务方法时,我们需要验证许可证密钥,该密钥作为每个请求的一部分消息(请求和响应消息将是JSON格式)。要验证许可证密钥,我们需要为每个请求调用单独的方法,并且此方法将包含逻辑,如果输入请求包含无效的许可证密钥,那么我们需要发送错误消息,例如"无效的许可证密钥&# 34;以JSON格式发送给客户端。 为此,我们在WCF Rest中是否有任何方法可以自动为每个请求调用此方法,而不是为每个请求显式调用?
注意: - 许可证密钥将在Service Web.Config文件中提供。
答案 0 :(得分:1)
您可以使用消息检查器,每次发送到服务的请求都会调用该消息检查器。下面的代码显示了这种检查员的一种实现方式。
public class StackOverflow_25380450
{
[ServiceContract]
public class Service
{
[WebGet]
public int Add(int x, int y)
{
return x + y;
}
}
public class MyInspector : IDispatchMessageInspector, IEndpointBehavior
{
public const string LicenseHeaderName = "X-License";
public const string ExpectedLicense = "abcdef";
public object AfterReceiveRequest(ref Message request, IClientChannel channel, InstanceContext instanceContext)
{
HttpRequestMessageProperty reqProp = (HttpRequestMessageProperty)request.Properties[HttpRequestMessageProperty.Name];
var license = reqProp.Headers[LicenseHeaderName];
if (license != ExpectedLicense)
{
throw new WebFaultException<string>("License required", HttpStatusCode.Forbidden);
}
return null;
}
public void BeforeSendReply(ref Message reply, object correlationState)
{
}
public void AddBindingParameters(ServiceEndpoint endpoint, BindingParameterCollection bindingParameters)
{
}
public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
{
}
public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.MessageInspectors.Add(this);
}
public void Validate(ServiceEndpoint endpoint)
{
}
}
public static string SendGet(string uri, Dictionary<string, string> headers)
{
string responseBody = null;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
req.Method = "GET";
if (headers != null)
{
foreach (string headerName in headers.Keys)
{
switch (headerName)
{
case "Accept":
req.Accept = headers[headerName];
break;
default:
req.Headers[headerName] = headers[headerName];
break;
}
}
}
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
}
catch (WebException e)
{
resp = (HttpWebResponse)e.Response;
}
if (resp == null)
{
responseBody = null;
Console.WriteLine("Response is null");
}
else
{
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
foreach (string headerName in resp.Headers.AllKeys)
{
Console.WriteLine("{0}: {1}", headerName, resp.Headers[headerName]);
}
Console.WriteLine();
Stream respStream = resp.GetResponseStream();
if (respStream != null)
{
responseBody = new StreamReader(respStream).ReadToEnd();
Console.WriteLine(responseBody);
}
else
{
Console.WriteLine("HttpWebResponse.GetResponseStream returned null");
}
}
Console.WriteLine();
Console.WriteLine(" *-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* ");
Console.WriteLine();
return responseBody;
}
public static void Test()
{
string baseAddress = "http://" + Environment.MachineName + ":8000/Service";
ServiceHost host = new ServiceHost(typeof(Service), new Uri(baseAddress));
var endpoint = host.AddServiceEndpoint(typeof(Service), new WebHttpBinding(), "");
endpoint.Behaviors.Add(new WebHttpBehavior { AutomaticFormatSelectionEnabled = true });
endpoint.Behaviors.Add(new MyInspector());
host.Open();
Console.WriteLine("Host opened");
Console.WriteLine("No license:");
Dictionary<string, string> headers = new Dictionary<string, string>
{
{ "Accept", "application/json" }
};
SendGet(baseAddress + "/Add?x=6&y=8", headers);
Console.WriteLine("Incorrect license:");
headers.Add(MyInspector.LicenseHeaderName, "incorrect");
SendGet(baseAddress + "/Add?x=6&y=8", headers);
headers[MyInspector.LicenseHeaderName] = MyInspector.ExpectedLicense;
SendGet(baseAddress + "/Add?x=6&y=8", headers);
Console.Write("Press ENTER to close the host");
Console.ReadLine();
host.Close();
}
}
您可以在http://blogs.msdn.com/b/carlosfigueira/archive/2011/04/19/wcf-extensibility-message-inspectors.aspx找到有关检查员的更多信息。