wcf restful service config error
本文关键字:error config service restful wcf | 更新日期: 2023-09-27 18:31:06
我正在创建一个 restful 服务的骨架算法,展示如何处理帖子和获取请求。 从我的例子来看,Get 工作正常,但 Post 不能。 我想我应该向 web.config 添加东西,但我不知道是什么以及为什么。 提前谢谢,佐利。
[ServiceContract]
public interface IRestfulService
{
[OperationContract]
[WebGet(UriTemplate = "/GetAStudent")]
Student GetExistingStudent();
[OperationContract]
[WebInvoke(UriTemplate = "/GetTheGivenStudent/{studentName}", Method = "POST")]
Student GetGivenStudent(string studentName);
}
public class RestfulService : IRestfulService
{
public Student GetExistingStudent()
{
Student stdObj = new Student
{
StudentName = "Foo",
Age = 29,
Mark = 95
};
return stdObj;
}
public Student GetGivenStudent(string studentName)
{
Student stdObj = new Student
{
StudentName = studentName,
Age = 29,
Mark = 95
};
return stdObj;
}
}
[DataContract]
public class Student
{
[DataMember]
public string StudentName { get; set; }
[DataMember]
public int Age { get; set; }
[DataMember]
public double Mark { get; set; }
}
web.config:
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<protocolMapping>
<add scheme="http" binding="webHttpBinding"/>
</protocolMapping>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
<serviceMetadata httpGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior >
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
无需公开 REST 服务的 mex 终结点。你的web.config应该看起来像这样:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
<services>
<service name="BookService">
<!-- Expose an XML endpoint: -->
<endpoint name="xml"
address="xml"
binding="webHttpBinding"
contract="BookStore.Contracts.IBookService"
behaviorConfiguration="poxBehavior" />
<!-- Expose a JSON endpoint: -->
<endpoint name="json"
address="json"
binding="webHttpBinding"
contract="BookStore.Contracts.IBookService"
behaviorConfiguration="jsonBehavior" />
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="poxBehavior">
<webHttp />
</behavior>
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
上面将公开两个终结点,一个使用 XML 数据,另一个使用 JSON。当然,像这样公开两个终结点是完全可选的;这只是您可以做什么的一个例子。
我也喜欢将路由用于 REST 服务;就像在你的 Global.asax.cs 中这样:
protected void Application_Start(object sender, EventArgs e)
{
RouteTable.Routes.Add(
new System.ServiceModel.Activation.ServiceRoute("books",
new System.ServiceModel.Activation.WebServiceHostFactory(),
typeof(BookStore.Services.BookService)
)
);
}
在示例 web.config 中使用上述终结点,这将允许像这样访问服务:
http://yourdomain.com/books/xml
如果选择使用或添加 JSON 终结点,如下所示:
http://yourdomain.com/books/json