我是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;
}
}
答案 0 :(得分:2)
虽然它在web.config的records.wcf服务部分中已注释掉,但看起来您正在公开(或将要公开)的端点是一个webHttpBinding端点,它是一个REST绑定。 WCF元数据交换端点仅用于SOAP端点(例如basicHttpBinding或wsHttpBinding)。没有办法从这样的WCF REST服务中获取元数据。
如果您需要元数据,您有两种选择:
从您的问题来看,元数据并不是真正的目标,它只是一种检查您的服务是否正常运行的方式。如果是这种情况,则需要
您的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服务。我通常比ASP.Net更了解WCF,仍然我发现很多更容易使用ASP.Net Web API进行REST。