WCF Web服务无法访问Web Config

时间:2017-09-16 10:30:24

标签: c# asp.net wcf web service

这是我的第一个网络服务,我正在尝试按照有关为Web服务创建自定义身份验证的教程,因为我收到此错误而停滞不前:

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

网络配置:

      <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");
    }
}

}

我已经尝试过我在网上找到的大部分内容,服务名称的变体等,但无法找到答案。

提前致谢。

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

以下是导致我的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中的以下两项对使此功能通过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>

因此,也许可以摆脱一些话题,但这也许会对某人有所帮助。