本文已参加「新人创造礼」活动,一起开启创造之路
一、服务方运用C#完成
1.新建wcf服务应用程序
2.不用修正任何代码,坚持默许即可
自带的事例接收一个整型变量为参数,回来一个字符串结果。
service1.svc代码
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfServiceSoap1
{
// 留意: 运用“重构”菜单上的“重命名”命令,可以一起更改代码、svc 和装备文件中的类名“Service1”。
// 留意: 为了发动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 Service1.svc 或 Service1.svc.cs,然后开端调试。
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
}
}
IServer1.cs代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
namespace WcfServiceSoap1
{
// 留意: 运用“重构”菜单上的“重命名”命令,可以一起更改代码和装备文件中的接口名“IService1”。
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
[OperationContract]
CompositeType GetDataUsingDataContract(CompositeType composite);
// TODO: 在此增加您的服务操作
}
// 运用下面示例中说明的数据约定将复合类型增加到服务操作。
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
}
3.生成解决方案
4.作一个winform桌面应用程序为属主,让这个WCF的DLL作为寄生虫在上面作业
4.1增加有关于WEBSERVICE 的引证:
4.2在把第一步我们作的WcfService1.dll也引证进来,留意选“仿制”:
4.3program由以下代码替换:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.ServiceModel;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsSoapServiceApp
{
public partial class Form2 : Form
{
private ServiceHost host = null;
private Container components;
public Form2()
{
InitializeComponent();
try
{
host = new ServiceHost(typeof(WcfServiceSoap1.Service1));//WcfDemo.Service1 为引证的dll中的服务
host.Open();//发动服务
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form2";
}
}
internal static class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
/// Application.Run(new Form1());
Application.Run(new Form2());
}
}
}
4.4假如WINFORM没有装备文件app.config,需要增加一个:
代码:(留意改上自己的IP地址)
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<legacyUnhandledExceptionPolicy enabled="true"/>
</runtime>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<system.serviceModel>
<services>
<!--增加服务-->
<service name="WcfServiceSoap1.Service1" behaviorConfiguration="CalculatorServiceBehavior">
<!--name 有必要与代码中的host实例初始化的服务相同 behaviorConfiguration 行为装备 -->
<host>
<baseAddresses>
<!--增加调用服务地址-->
<add baseAddress="http://IP:8000/"/>
</baseAddresses>
</host>
<!--增加契约接口 contract="WcfDemo.IService1" WcfDemo.IService1为契约接口 binding="wsHttpBinding" wsHttpBinding为经过Http调用-->
<endpoint address="" binding="basicHttpBinding" contract="WcfServiceSoap1.IService1"></endpoint>
</service>
</services>
<!--定义CalculatorServiceBehavior的行为-->
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
4.5编译项目,产生exe文件,然后用管理员的权限去履行这个履行程序,8000端口的webservice服务就准备好了。
改为release
拜访服务地址:http://localhost:8000/
wsdl地址:http://localhost:8000/?wsdl
4.6SoapUI测试:
二、PO/PI装备
假如结构不杂乱直接引入wsdl到ESR中,装备时需留意对应联系,假如允许为空的字段需设置为0-unbound,再铺开PO/PI服务器拜访外部服务(服务器)对应端口的权限,就ok了