ahe*_*ang 14 wcf soap web-services
我试图找到一些很好的教程,告诉我使用WCF创建一个简单的基于SOAP的服务并进行部署.我一直在谷歌搜索过去2小时似乎找不到任何好的资源..任何人都可以帮助我吗?
您可以在两个不同的端点中公开服务。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)
因此,该服务将在
http://www.example.com/soap http://www.example.com/json 将[WebGet]应用到操作合约中,使其成为RESTful。例如
公共接口 ITestService { [OperationContract] [WebGet] string HelloWorld(string text) }
注意,如果 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 纯旧 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)
服务将在
http://www.example.com/soap
http://www.example.com/xml
REST 请求在浏览器中尝试,
http://www.example.com/xml/accounts/A123
添加服务引用后,SOAP 服务的 SOAP 请求客户端端点配置,
<client>
<endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
contract="ITestService" name="BasicHttpBinding_ITestService" />
Run Code Online (Sandbox Code Playgroud)
在 C# 中
TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");
Run Code Online (Sandbox Code Playgroud)
另一种方法是公开两个不同的服务契约,每个契约都有特定的配置。这可能会在代码级别生成一些重复项,但是在一天结束时,您希望使其工作。
归档时间: |
|
查看次数: |
26438 次 |
最近记录: |