Ray*_* Lu 581
您可以在两个不同的端点中公开该服务.SOAP可以使用支持SOAP的绑定,例如basicHttpBinding,RESTful可以使用webHttpBinding.我假设您的REST服务将使用JSON,在这种情况下,您需要使用以下行为配置来配置两个端点
<endpointBehaviors>
<behavior name="jsonBehavior">
<enableWebScript/>
</behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)
您的方案中的端点配置示例如下
<services>
<service name="TestService">
<endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
<endpoint address="json" binding="webHttpBinding" behaviorConfiguration="jsonBehavior" contract="ITestService"/>
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
所以,该服务将在
将[WebGet]应用于操作合同以使其成为RESTful.例如
public interface ITestService
{
[OperationContract]
[WebGet]
string HelloWorld(string text)
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果REST服务不在JSON中,则操作的参数不能包含复杂类型.
对于普通的旧XML作为返回格式,这是一个既适用于SOAP又适用于XML的示例.
[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
[OperationContract]
[WebGet(UriTemplate = "accounts/{id}")]
Account[] GetAccount(string id);
}
Run Code Online (Sandbox Code Playgroud)
REST Plain Old XML的POX行为
<behavior name="poxBehavior">
<webHttp/>
</behavior>
Run Code Online (Sandbox Code Playgroud)
端点
<services>
<service name="TestService">
<endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
<endpoint address="xml" binding="webHttpBinding" behaviorConfiguration="poxBehavior" contract="ITestService"/>
</service>
</services>
Run Code Online (Sandbox Code Playgroud)
服务将在
REST请求 在浏览器中尝试
添加服务引用后SOAP服务的SOAP请求客户端端点配置,
<client>
<endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
contract="ITestService" name="BasicHttpBinding_ITestService" />
</client>
Run Code Online (Sandbox Code Playgroud)
在C#中
TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");
Run Code Online (Sandbox Code Playgroud)
另一种方法是公开两个不同的服务合同,每个合同都有特定的配置.这可能会在代码级别生成一些重复项,但是在一天结束时,您希望使其正常工作.
Tuo*_*nen 38
这个帖子已经有一个很好的答案通过"社区维基",我也建议看里克施特拉尔的网络博客,大约有WCF休息多好职位像这样.
我用两者来获得这种MyService服务......然后我可以使用jQuery的REST接口或Java的SOAP.
这是来自我的Web.Config:
<system.serviceModel>
<services>
<service name="MyService" behaviorConfiguration="MyServiceBehavior">
<endpoint name="rest" address="" binding="webHttpBinding" contract="MyService" behaviorConfiguration="restBehavior"/>
<endpoint name="mex" address="mex" binding="mexHttpBinding" contract="MyService"/>
<endpoint name="soap" address="soap" binding="basicHttpBinding" contract="MyService"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="restBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)
这是我的服务类(.svc-codebehind,不需要接口):
/// <summary> MyService documentation here ;) </summary>
[ServiceContract(Name = "MyService", Namespace = "http://myservice/", SessionMode = SessionMode.NotAllowed)]
//[ServiceKnownType(typeof (IList<MyDataContractTypes>))]
[ServiceBehavior(Name = "MyService", Namespace = "http://myservice/")]
public class MyService
{
[OperationContract(Name = "MyResource1")]
[WebGet(ResponseFormat = WebMessageFormat.Xml, UriTemplate = "MyXmlResource/{key}")]
public string MyResource1(string key)
{
return "Test: " + key;
}
[OperationContract(Name = "MyResource2")]
[WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource/{key}")]
public string MyResource2(string key)
{
return "Test: " + key;
}
}
Run Code Online (Sandbox Code Playgroud)
实际上我只使用Json或Xml,但这些都是出于演示目的.这些是获取数据的GET请求.要插入数据,我会使用带有属性的方法:
[OperationContract(Name = "MyResourceSave")]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, UriTemplate = "MyJsonResource")]
public string MyResourceSave(string thing){
//...
Run Code Online (Sandbox Code Playgroud)
myt*_*thz 25
如果您只想开发单个Web服务并将其托管在许多不同的端点上(即SOAP + REST,使用XML,JSON,CSV,HTML输出).您还应该考虑使用我为此目的构建的ServiceStack,您开发的每个服务都可以在开箱即用的SOAP和REST端点上自动使用,而无需任何配置.
在的Hello World例子说明如何创建与服务的简单只(无需配置):
public class Hello {
public string Name { get; set; }
}
public class HelloResponse {
public string Result { get; set; }
}
public class HelloService : IService
{
public object Any(Hello request)
{
return new HelloResponse { Result = "Hello, " + request.Name };
}
}
Run Code Online (Sandbox Code Playgroud)
无需其他配置,此服务可立即在REST中使用:
它还内置了友好的HTML输出(当使用具有Accept:text/html的HTTP客户端调用时,例如浏览器),因此您可以更好地可视化服务的输出.
处理不同的REST动词也很简单,这里是一个完整的REST服务CRUD应用程序,在1页的C#中(少于配置WCF所需的);
MSDN现在似乎有一篇文章:
https://msdn.microsoft.com/en-us/library/bb412196(v=vs.110).aspx
介绍:
默认情况下,Windows Communication Foundation(WCF)使端点仅对SOAP客户端可用.在如何:创建基本WCF Web HTTP服务中,端点可供非SOAP客户端使用.有时您可能希望以相同的方式提供相同的合同,如Web端点和SOAP端点.本主题显示了如何执行此操作的示例.
归档时间: |
|
查看次数: |
259046 次 |
最近记录: |