繁体   English   中英

WCF服务元数据发布错误

[英]WCF Service metadata publishing error

我是WCF的新手并试图让它运行起来。 我甚至无法得到一个没有任何错误的空白版本。

web.config中:

 <system.serviceModel>
    <behaviors>
      <endpointBehaviors>
        <behavior name="ServiceAspNetAjaxBehavior">
          <enableWebScript />
        </behavior>

        <!--wcf-->     
        <behavior name="recordsWCF">
          <webHttp/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="recordsSWCF" >
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    <!--end wcf-->      

    </behaviors> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />   
    <services>


      <!--wcf-->
      <service name="records.wcf" behaviorConfiguration="recordsSWCF" >
        <!--<endpoint address="" binding="webHttpBinding" contract="IService" behaviorConfiguration="recordsWCF"/>-->
        <endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />
      </service>
      <!--end wcf-->         

      <service name="Service">
        <endpoint address="" behaviorConfiguration="ServiceAspNetAjaxBehavior" binding="webHttpBinding" contract="Service" />
      </service>
    </services>
  </system.serviceModel>

至于我的wcf代码,还有很多工作要做。 我只是想学习如何设置web.config没有任何错误。 这是我的wcf定义:

[ServiceContract(Namespace = "records")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class wcf
{

 public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }      
    [OperationContract]
    [WebGet]
    public string GetData()
    {
        Person p = new Person();
        List<Person> test = new List<Person>();
        p.Name = "Tom";
        p.Age = 28;   
        test.Add(p);   
        p = new Person();
        p.Name = "Dan";
        p.Age = 22;
        test.Add(p);
        JavaScriptSerializer js = new JavaScriptSerializer();   
        string jsonString = js.Serialize(test);    

        return jsonString;
    }    
}

虽然它在web.config的records.wcf服务部分中被注释掉,但是您正在公开(或将要公开)的端点看起来像是一个webHttpBinding端点,它是一个REST绑定。 WCF元数据交换端点仅用于SOAP端点(例如basicHttpBinding或wsHttpBinding)。 没有办法从这样的WCF REST服务中获取元数据。

如果您需要元数据,您有两种选择:

  1. 将您的服务重构为SOAP服务
  2. 而不是WCF,使用ASP.Net Web API。 这是专为REST而构建的,它为您提供了一个自动生成的帮助页面,该页面在概念上与WCF中的元数据端点相同

从你的问题来看,元数据并不是真正的目标,它只是一种检查你的服务是否正常的方法。 如果是这种情况,您需要

  1. 从recordWCF服务行为中删除serviceMetadata元素
  2. 删除IMetadatExchange端点
  3. 添加服务端点

您的web.config应如下所示:

  <system.serviceModel>
    <behaviors>
      <endpointBehaviors>

        <!--wcf-->
        <behavior name="recordsWCF">
          <webHttp/>
          <enableWebScript />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="recordsSWCF" >
          <!--<serviceMetadata httpGetEnabled="true" />-->
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
      <!--end wcf-->

    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
    <services>


      <!--wcf-->
      <service name="records.Service" behaviorConfiguration="recordsSWCF" >
        <endpoint address="" binding="webHttpBinding" contract="records.Service" behaviorConfiguration="recordsWCF"/>
        <!--<endpoint contract="IMetadataExchange" binding="mexHttpBinding" address="mex" />-->
      </service>
      <!--end wcf-->
    </services>
  </system.serviceModel>

实现服务的类应该如下所示

using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web.Script.Serialization;

namespace records
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    [ServiceContract]
    public class Service
    {
        public class Person
        {
            public string Name { get; set; }
            public int Age { get; set; }
        }

        [WebGet]
        public string GetData()
        {
            Person p = new Person();
            List<Person> test = new List<Person>();
            p.Name = "Tom";
            p.Age = 28;
            test.Add(p);
            p = new Person();
            p.Name = "Dan";
            p.Age = 22;
            test.Add(p);
            JavaScriptSerializer js = new JavaScriptSerializer();
            string jsonString = js.Serialize(test);

            return jsonString;
        }
    }
}

你的.svc的标记看起来应该是这样的

<%@ ServiceHost Language="C#" Debug="true" Service="records.Service" CodeBehind="Service.svc.cs" %>

使用该配置,当我浏览到http://localhost:56157/Service.svc/GetData我得到了

[{"Name":"Tom","Age":28},{"Name":"Dan","Age":22}]

你必须在Chrome中这样做,而不是IE。 如果您在IE中尝试它,它会尝试将JSON作为文件打开。

请注意,在实现服务的同一个类上定义服务协定有点不寻常。 更常见的是,服务契约在接口(例如IService)上定义并在类中实现(例如公共类服务:IService)。

此外,除非有特殊原因使用WCF(例如,您想要执行SOAP服务),我强烈建议使用ASP.Net Web API进行REST服务。 我知道了很多关于WCF比ASP.Net一般和我还是觉得它容易做 REST与ASP.Net的Web API。

暂无
暂无

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

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