繁体   English   中英

WCF Web Service 无法访问 Web Config 中的服务名称

[英]WCF Web Service can't access service name in Web Config

这是我的第一个 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");
    }
}

}

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

提前致谢。

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

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

以下在我的 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>

所以,也许有点偏离主题,但也许这会对某人有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM