如何修复“枚举约束失败”?

时间:2019-07-30 18:55:19

标签: c# wcf service

我一直在关注Microsoft tutorial。设置启用AJAX的WCF服务并通过客户端访问它们。但是,尽管严格按照本教程进行操作,但由于错误原因,结果将不会显示。具体来说,错误指出Enumeration约束失败,导致“名称”和“合同”属性无效。

该错误似乎源自我的Web.config文件,因为错误列表仅显示该文件中的问题。下面,我包括了文件中的代码以及我尝试访问的服务中的代码。

//The service model segment of the configuration file.  
<system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="SandwichServices.Service1AspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
        <behavior name="SandwichServices.CostServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
    <services>
      <service name="SandwichServices.CostService">
        <endpoint address="" behaviorConfiguration="SandwichServices.CostServiceAspNetAjaxBehavior"
          binding="webHttpBinding" contract="SandwichServices.CostService" />
      </service>
    </services>
  </system.serviceModel>
//The .svc.cs file for my service
using System.ServiceModel;
using System.ServiceModel.Activation;

namespace SandwichServices
{
    [ServiceContract]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class CostService
    {
        [OperationContract]
        public double CostOfSandwiches(int quantity)
        {
            return 1.25 * quantity;
        }
    }
}

以下错误消息出现在Web.config文件中:

  

  “合同”属性无效-该值   根据其数据类型,“ SandwichServices.CostService”无效   'serviceContractType'-枚举约束失败。“   “名称”属性无效-值“ SandwichServices.CostService”   根据其数据类型'serviceNameType'是无效的-   枚举约束失败。

1 个答案:

答案 0 :(得分:0)

这是警告而不是错误,因此Asp.net应用程序可以成功构建。对于WCF服务(启用了ajax),这是一个兼容的解决方案,因为WCF对Service部分有严格的规定(一个用于接口,一个用于服务实现类),如下所示。

var dc = await _dialogs.CreateContextAsync(turnContext);

                if (turnContext.Activity.Type == ActivityTypes.Message)
                {
                    // Ensure that message is a postBack (like a submission from Adaptive Cards)
                    var channelData = JObject.Parse(dc.Context.Activity.ChannelData.ToString());
                    if (channelData.ContainsKey("postback"))
                    {
                        var postbackActivity = dc.Context.Activity;
                        // Convert the user's Adaptive Card input into the input of a Text Prompt
                        // Must be sent as a string
                        postbackActivity.Text = postbackActivity.Value.ToString();
                        await dc.Context.SendActivityAsync(postbackActivity);
                    }
                }

                if (dc.ActiveDialog != null)
                {
                    var result = await dc.ContinueDialogAsync();
                }
                else
                {
                    await dc.BeginDialogAsync(typeof(T).Name);
                }

总而言之,我们无需更多注意此警告,该服务仍可以正常运行。