用C#生成服务并通过PO/PI调用

941 阅读2分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路


一、服务方使用C#实现

1.新建wcf服务应用程序

image.png

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.生成解决方案

image.png

4.作一个winform桌面应用程序为属主,让这个WCF的DLL作为寄生虫在上面工作

image.png

4.1添加有关于WEBSERVICE 的引用:

image.png

4.2在把第一步我们作的WcfService1.dll也引用进来,注意选“复制”:

image.png

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,需要添加一个:

image.png

代码:(注意改上自己的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

image.png 访问服务地址:http://localhost:8000/

wsdl地址:http://localhost:8000/?wsdl

4.6SoapUI测试:

image.png

image.png

二、PO/PI配置

如果结构不复杂直接引入wsdl到ESR中,配置时需注意对应关系,如果允许为空的字段需设置为0-unbound,再放开PO/PI服务器访问外部服务(服务器)对应端口的权限,就ok了