简体   繁体   English

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

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

This is my first web service and I am trying to follow a tutorial on creating a custom authentication for a web service, am stuck as I'm getting this error:这是我的第一个 Web 服务,我正在尝试按照有关为 Web 服务创建自定义身份验证的教程进行操作,但遇到此错误时卡住了:

The value 'WcfOrderingService.Services.AuthenticationTokenService' is invalid according to its datatype 'serviceNameType' - The Enumeration constraint failed.根据其数据类型“serviceNameType”,值“WcfOrderingService.Services.AuthenticationTokenService”无效 - 枚举约束失败。

web config:网络配置:

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

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

} }

I have tried most things I have found online, variations of the service name etc but cant find an answer.我已经尝试了我在网上找到的大多数东西,服务名称的变体等,但找不到答案。

Thanks in advance.提前致谢。

Probably the reference to the service isn't working properly anymore.可能对服务的引用不再正常工作。 There can be several reason's for this.这可能有几个原因。

The solution will be to first remove the servicereference and then add it again.解决方案是首先删除服务引用,然后再次添加它。

The following was causing errors in my Web.config以下在我的 Web.config 中导致错误

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

I appeared to have solved the problem by moving my service and contract (interface) into a namespace.我似乎已经通过将我的服务和合同(接口)移动到一个命名空间中来解决这个问题。

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

It turned out that as long as you leave the Web.config open when running the project then, strangely, all would run fine.事实证明,只要您在运行项目时让 Web.config 保持打开状态,那么奇怪的是,一切都会运行良好。 I ended up taking the service out of the namespace and not implementing an interface at all.我最终将服务从命名空间中取出,根本没有实现接口。

<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

The following 2 items in Web.config were vital to getting this working over https. 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>

So, maybe getting a bit off subject but perhaps this will help someone.所以,也许有点偏离主题,但也许这会对某人有所帮助。

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

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