如何通过WCF在C#中公开SOAP Web服务

时间:2012-12-04 16:43:55

标签: c# wcf web-services soap

对于我在学校的项目,我必须在C#应用程序中使用WCF创建一些SOAP Web服务,以使其与Java EE应用程序进行交互。

我找不到任何教程告诉我如何使用WCF执行此操作。我该怎么办?

3 个答案:

答案 0 :(得分:3)

WCF服务的REST / SOAP端点

您可以在两个不同的端点中公开该服务。 SOAP可以使用支持SOAP的绑定,例如basicHttpBinding,RESTful可以使用webHttpBinding。我假设您的REST服务将使用JSON,在这种情况下,您需要使用以下行为配置配置两个端点

<endpointBehaviors>
    <behavior name="jsonBehavior">
        <enableWebScript/>
    </behavior>
</endpointBehaviors>

您的方案中的端点配置示例是

<services>
    <service name="TestService">
        <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
        <endpoint address="json" binding="webHttpBinding"  behaviorConfiguration="jsonBehavior" contract="ITestService"/>
    </service>
</services>

所以,该服务将在
http://www.example.com/soap http://www.example.com/json
将[WebGet]应用于操作合同以使其成为RESTful。 e.g。

公共接口ITestService {    [OperationContract的]    [WebGet]    字符串HelloWorld(字符串文本) }

注意,如果REST服务不是JSON,则操作的参数不能包含复杂类型 对于普通的旧XML作为返回格式,这是一个既适用于SOAP又适用于XML的示例。

[ServiceContract(Namespace = "http://test")]
public interface ITestService
{
[OperationContract]
[WebGet(UriTemplate = "accounts/{id}")]
Account[] GetAccount(string id);
}

REST Plain Old XML的POX行为

<behavior name="poxBehavior">
    <webHttp/>
</behavior>

端点

<services>
    <service name="TestService">
        <endpoint address="soap" binding="basicHttpBinding" contract="ITestService"/>
        <endpoint address="xml" binding="webHttpBinding"  behaviorConfiguration="poxBehavior" contract="ITestService"/>
    </service>
</services>

服务将在

提供

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" />
</client>

在C#

TestServiceClient client = new TestServiceClient();
client.GetAccount("A123");

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

答案 1 :(得分:2)

只需在Visual Studio中创建一个WCF项目,然后用C#编写所有代码。 您将遇到的实际问题是从Java EE进行SOAP调用。

WCF服务将托管在IIS中,而不是托管WCF的Windows服务。

如何开始使用WCF的教程:

http://msdn.microsoft.com/en-us/library/dd936243.aspx

享受!

答案 2 :(得分:0)