gpt4 book ai didi

c# - wcf restful服务配置错误

转载 作者:行者123 更新时间:2023-11-30 18:39:21 25 4
gpt4 key购买 nike

我正在创建一个 Restful 服务的框架算法,展示如何处理发布和获取请求。从我的示例中 get 工作正常,但 post 没有。我想我应该向 web.config 添加内容,但我不知道是什么以及为什么。提前致谢,Zoli。

 [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; }
}

网络配置:

<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

关于c# - wcf restful服务配置错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10125883/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com