【问题标题】:WCF Web Service can't access service name in Web ConfigWCF Web 服务无法访问 Web Config 中的服务名称
【发布时间】:2018-02-25 10:21:36
【问题描述】:

这是我的第一个 Web 服务,我正在尝试按照有关为 Web 服务创建自定义身份验证的教程进行操作,但遇到此错误时卡住了:

根据其数据类型“serviceNameType”,值“WcfOrderingService.Services.AuthenticationTokenService”无效 - 枚举约束失败。

网络配置:

      <service name ="WcfOrderingService.Services.AuthenticationTokenService"
           behaviorConfiguration="ServiceBehaviourHttp">
    <endpoint address="" behaviorConfiguration="WcfOrderingService.AuthenticationTokenServiceAspNetAjaxBehavior"
      binding="webHttpBinding" contract="WcfOrderingService.Services.AuthenticationTokenService.Authenticate" />
  </service>

.svc.cs -

namespace WcfOrderingService.Services
{
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class AuthenticationTokenService
{
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json,
    ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Bare)]
    [OperationContract]
    public string Authenticate(Credentials creds)
    {
        ICredentialsValidator validator = new CredentialsValidator();
        if (validator.IsValid(creds))
            return new TokenBuilder().Build(creds);
        throw new InvalidCredentialException("Invalid credentials");
    }
}

}

我已经尝试了我在网上找到的大多数东西、服务名称的变体等,但找不到答案。

提前致谢。

【问题讨论】:

    标签: c# asp.net wcf web service


    【解决方案1】:

    可能对服务的引用不再正常工作。这可能有几个原因。

    解决方案是首先删除服务引用,然后再次添加。

    【讨论】:

    • 谢谢,但如果我什至启动一个全新的 WCF 服务应用程序项目,然后添加一个 WCF 服务(启用 Ajax)服务 - 它会给我同样的错误。
    • 啊,我认为配置不正确。将端点的contract属性改成 WcfOrderingService.Services.AuthenticationTokenService 是否有效?
    • 它不起作用,智能感知也不会拾取它,但默认的 service1 会。
    【解决方案2】:

    以下导致我的 Web.config 出现错误

    <service name="case_info">
        <endpoint address="" behaviorConfiguration="case_infoAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="ICase_Info" />
    </service>
    

    我似乎通过将我的服务和合同(接口)移动到命名空间中解决了这个问题。

    <service name="mynamesp.case_info">
        <endpoint address="" behaviorConfiguration="case_infoAspNetAjaxBehavior" binding="webHttpBinding" bindingConfiguration="webBinding" contract="mynamesp.ICase_Info" />
    </service>
    

    事实证明,只要您在运行项目时保持 Web.config 处于打开状态,那么奇怪的是,一切都会运行良好。我最终将服务从命名空间中取出,根本没有实现接口。

    <ServiceContract(Namespace:="http://edwardo.com/services/")>
    <AspNetCompatibilityRequirements(RequirementsMode:=AspNetCompatibilityRequirementsMode.Allowed)>
    Public Class case_info
    
        <WebGet(ResponseFormat:=WebMessageFormat.Json)>
        <OperationContract()>
        Public Function GetCasePopup(CaseID As Int32) As CasePopupInfo
            Return CasePopup.GetCasePopup(CaseID)
        End Function
    
    End Class
    

    Web.config 中的以下 2 项对于通过 https 运行至关重要。

    <system.serviceModel>
        <bindings>
          <webHttpBinding>
            <binding name="webBinding">
                <security mode="Transport" />
            </binding>
          </webHttpBinding>
        </bindings>
    </system.serviceModel>
    
    <system.webServer>
        <handlers>
            <add name=".svc" verb="*" path="*.svc" type="System.ServiceModel.Activation.ServiceHttpHandlerFactory, System.ServiceModel.Activation, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
        </handlers>
    </system.webServer>
    

    所以,也许有点跑题了,但也许这会对某人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-07-20
      • 2011-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-06
      • 1970-01-01
      相关资源
      最近更新 更多