简体   繁体   English

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

[英]How to expose SOAP web services in C# through WCF

For my project at school I must create some SOAP web services with WCF in a C# app, in order to make it interact with a Java EE app. 对于我在学校的项目,我必须在C#应用程序中使用WCF创建一些SOAP Web服务,以使其与Java EE应用程序交互。

I can't find any tutorial telling me how to do this with WCF. 我找不到任何教程可以告诉我如何使用WCF进行操作。 What should I do? 我该怎么办?

REST / SOAP endpoints for a WCF service WCF服务的REST / SOAP端点

You can expose the service in two different endpoints. 您可以在两个不同的端点中公开该服务。 the SOAP one can use the binding that support SOAP eg basicHttpBinding, the RESTful one can use the webHttpBinding. SOAP可以使用支持SOAP的绑定,例如basicHttpBinding,RESTful可以使用webHttpBinding。 I assume your REST service will be in JSON, in that case, you need to configure the two endpoints with the following behaviour configuration 我假设您的REST服务将使用JSON,在这种情况下,您需要使用以下行为配置来配置两个端点

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

An example of endpoint configuration in your scenario is 您的方案中的端点配置示例是

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

so, the service will be available at 因此,该服务将在
http://www.example.com/soap http://www.example.com/json http://www.example.com/soap http://www.example.com/json
Apply [WebGet] to the operation contract to make it RESTful. 将[WebGet]应用于操作合同以使其为RESTful。 eg 例如

public interface ITestService { [OperationContract] [WebGet] string HelloWorld(string text) } 公共接口ITestService {[OperationContract] [WebGet]字符串HelloWorld(字符串文本)}

Note, if the REST service is not in JSON, parameters of the operations can not contain complex type. 请注意,如果REST服务不在JSON中,则操作的参数不能包含复杂类型。
For plain old XML as return format, this is an example that would work both for SOAP and XML. 对于普通的旧XML作为返回格式,这是一个适用于SOAP和XML的示例。

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

POX behavior for REST Plain Old XML REST Plain Old XML的POX行为

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

Endpoints 端点

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

Service will be available at 服务将在

http://www.example.com/soap http://www.example.com/xml http://www.example.com/soap http://www.example.com/xml
REST request try it in browser, REST请求在浏览器中尝试一下,
http://www.example.com/xml/accounts/A123 http://www.example.com/xml/accounts/A123

SOAP request client endpoint configuration for SOAP service after adding the service reference, 添加服务参考后,SOAP请求针对SOAP服务的客户端端点配置,

<client>
    <endpoint address="http://www.example.com/soap" binding="basicHttpBinding"
  contract="ITestService" name="BasicHttpBinding_ITestService" />
</client>

in C# 在C#中

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

Another way of doing it is to expose two different service contract and each one with specific configuration. 做到这一点的另一种方法是公开两个不同的服务合同,每个合同都有特定的配置。 This may generate some duplicates at code level, however at the end of the day, you want to make it working. 这可能会在代码级别生成一些重复项,但是到了最后,您要使其正常运行。

Just create a WCF project in Visual Studio, and write all the code in C#. 只需在Visual Studio中创建一个WCF项目,然后用C#编写所有代码。 The actual problem you're going to have is making SOAP calls from Java EE. 您将要遇到的实际问题是从Java EE进行SOAP调用。

The WCF service will be hosted in IIS not a Windows Service hosting WCF. WCF服务将托管在IIS中,而不是Windows服务托管WCF。

Tutorials on how to get started with WCF: 有关如何开始使用WCF的教程:

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

Enjoy! 请享用!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM