WCF构建基于SOAP的服务

ahe*_*ang 14 wcf soap web-services

我试图找到一些很好的教程,告诉我使用WCF创建一个简单的基于SOAP的服务并进行部署.我一直在谷歌搜索过去2小时似乎找不到任何好的资源..任何人都可以帮助我吗?

mar*_*c_s 12

至于资源:MSDN WCF开发人员中心提供从初学者教程到文章和示例代码的所有内容.

另外,请查看MSDN上屏幕强制转换库,了解一些非常有用的10-15分钟信息块,其中包含与您可能感兴趣的WCF相关的任何主题.

MSDN杂志中关于WCF各个方面的服务站文章也非常好- 有些比较基本,如WCF中的序列化深度WCF绑定,有些更高级和深奥 - 但总是值得一看!

更新:用于学习WCF和SOAP,请查看例如

还有更多 - 在使用SOAP绑定的WCF 上有大量的教程和学习材料 - 不仅仅是REST的东西!


Shi*_*abh 5

WCF 服务的 REST/SOAP 端点

您可以在两个不同的端点中公开服务。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)

另一种方法是公开两个不同的服务契约,每个契约都有特定的配置。这可能会在代码级别生成一些重复项,但是在一天结束时,您希望使其工作。