【问题标题】:is it possible to use Validation Application Block with WCF REST service?是否可以将验证应用程序块与 WCF REST 服务一起使用?
【发布时间】:2011-09-26 20:36:57
【问题描述】:

我正在使用带有 API 密钥模板的 WCF REST 服务,并尝试使用验证应用程序块属性验证来强制验证。这是我的服务:

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
[ValidationBehavior]
public class Service1
{
    [FaultContract(typeof(ValidationFault))]
    [WebGet(UriTemplate = "ValidateStuff?text={text}")]
    public void ValidateStuff(
        [NotNullValidator]
        string text)
    {
    }

以及模板中的 global.asax:

public class Global : HttpApplication
    {
        void Application_Start(object sender, EventArgs e)
        {
            RegisterRoutes();
        }

        private void RegisterRoutes()
        {
            // Edit the base address of Service1 by replacing the "Service1" string below
            RouteTable.Routes.Add(new ServiceRoute("Service1", new WebServiceHostFactory(), typeof(Service1)));
        }
    }

然后我有一个客户端发送一个 GET 请求:

HttpWebRequest invokeRequest = WebRequest.Create(String.Concat(baseUrl, "/", uri, queryString)) as HttpWebRequest;
invokeRequest.Method = Enum.GetName(typeof(Method), method); 
WebResponse response = invokeRequest.GetResponse())

现在的问题是我每次都收到 HTTP/1.1 500 Internal Server Error。

如果我删除 [ValidationBehavior] [FaultContract(typeof(ValidationFault))] 和 [NotNullValidator] 属性,那么一切正常。我检查了服务跟踪,没有看到任何可以帮助我的东西。

【问题讨论】:

    标签: wcf validation rest block


    【解决方案1】:

    答案是确实有可能! 我发现了问题所在。我缺少对以下内容的引用:

    Microsoft.Practices.ServiceLocation.dll
    Microsoft.Practices.Unity.dll
    Microsoft.Practices.Unity.Interception.dll
    

    奇怪的是,我在跟踪日志和调试模式中都没有看到任何迹象。我发现的唯一方法是当我尝试在操作实现本身中手动进行验证时,如下所示:

    //at this point I got the exception saying that I'm missing the above references.
    var validationResult = Validation.Validate<T>(TInstance); 
    

    希望对某人有所帮助

    【讨论】:

      猜你喜欢
      • 2010-12-02
      • 2011-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多