【问题标题】:Cannot set service name attribute in web.config for WCF web service无法在 web.config 中为 WCF Web 服务设置服务名称属性
【发布时间】:2014-02-12 15:47:25
【问题描述】:

我编写了一个 WCF Web 服务,它工作正常,然后我从另一个应用程序复制了 Web 服务的内容并创建了一个新的 WCF 文件,该文件在 web.config 中创建了一个新文件,但 name 属性说命名空间不能找到了。

这是我的 WCF 前几行的示例:

namespace Testing.ws.mainGrid
{
    [WebService(Namespace = "")]
    [ScriptService]
    [ToolboxItem(false)]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Test : WebService
    {
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
        [WebMethod]
        public void GetRecords()
        {
            ((NameValueCollection)new WebClient().Headers).Add("accept", "text/xml");
            string str1 = "1";
            string str2 = "1";
            string str3 = "1";

这是我的 web.config:

  <system.serviceModel>
        <services>
            <service name="Testing.ws.getStaffTree">
                <endpoint address="" behaviorConfiguration="Testing.ws.getStaffTreeAspNetAjaxBehavior"
                    binding="webHttpBinding" contract="Testing.ws.getStaffTree" />
            </service>
            <service name="Testing.ws.mainGrid.Test">
                <endpoint address="" behaviorConfiguration="Testing.ws.mainGridAspNetAjaxBehavior"
                    binding="webHttpBinding" contract="Testing.ws.mainGrid" />
            </service>
        </services>

基本上name="Testing.ws.mainGrid.Test"不起作用,也找不到。

我不确定我做错了什么,但我已经在这上面花了很长时间!这第一个工作正常,但它的第二个问题。

实际错误是:

“name”属性无效 - 根据其数据类型“serviceNameType”,值 Testing.ws.mainGrid.Test 无效 - 枚举约束失败

如果您让智能感知完成它的工作,那么它会显示第一个 Web 服务,但不会显示我的新服务!

【问题讨论】:

  • 看起来您正在将 ASMX(您的类实现)与 WCF(您的 web.config)混合。
  • 另外,您正在尝试将 webHttpBinding 用于 SOAP 服务 - webHttpBinding 用于 REST。

标签: c# wcf


【解决方案1】:

我认为您的第二个服务应该如下所示。注意合约名称的变化:

<service name="Testing.ws.mainGrid.Test">
    <endpoint address="" behaviorConfiguration="Testing.ws.mainGridAspNetAjaxBehavior"
        binding="webHttpBinding" contract="Testing.ws.mainGrid.Test" />
</service>

在它指向命名空间之前,而不是类类型。

【讨论】:

    【解决方案2】:

    虽然我们遇到了同样的问题,但我们的修复略有不同,因为我们的开发环境对某些服务使用 Windows 身份验证,同时通过 web.config 专门禁用它。

    需要注释掉/删除 web.config 的以下部分

    <system.webServer>
        <!--<security>
               <authentication>
                   <windowsAuthentication enabled="false" />
               </authentication>
            </security>-->
    </system.webServer>
    

    这需要取消注释才能发布到生产是唯一的。

    【讨论】:

      猜你喜欢
      • 2018-02-25
      • 2011-06-24
      • 1970-01-01
      • 2016-05-28
      • 2021-05-09
      • 2012-02-02
      • 1970-01-01
      • 1970-01-01
      • 2022-01-16
      相关资源
      最近更新 更多