开发工具: VS2017 .net framework4.0

服务方:我们自己建一个WebService接口服务,等待调研。

请求方:Soap UI软件作客户端来调用WS接口。

 

一、用VS2017建一个WCF的DLL:

搭建一个WebService接口环境(SAP PO 开发 二)

新建一个WCF项目,什么都不改动它,用它自带的代码事例就好:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、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;
        }
    }
}

自带的事例接收一个整型变量为参数,返回一个字符串结果。

搭建一个WebService接口环境(SAP PO 开发 二)

生成这个WCF的DLL,在bin目录里面:

搭建一个WebService接口环境(SAP PO 开发 二)

 

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

(不想这么恶心?DLL动态链接库不能直接运行,要么放到IIS的web属主容器中,要么放到应用程序的进程容器中)

建一个WINFORM程序:

搭建一个WebService接口环境(SAP PO 开发 二)

添加有关于WEBSERVICE  的引用:

搭建一个WebService接口环境(SAP PO 开发 二)

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

搭建一个WebService接口环境(SAP PO 开发 二)

加几行代码,启动WCF寄生:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Web.Services;
using System.Windows.Forms;

using System.ServiceModel;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        private ServiceHost host = null;

        public Form1()
        {          

            InitializeComponent();
            try
            {
                host = new ServiceHost(typeof(WcfService1.Service1));//WcfDemo.Service1 为引用的dll中的服务 
                host.Open();//启动服务     
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}

如果WINFORM没有配置文件app.config,需要添加一个:

搭建一个WebService接口环境(SAP PO 开发 二)

打开app.config文件,把配置信息拷贝进去,注意改上自己的IP地址

(<add baseAddress="http://10.18.33.181:8000/"/>)

<?xml version="1.0" encoding="utf-8"?>
<configuration>


  <runtime>
    <legacyUnhandledExceptionPolicy enabled="true"/>
  </runtime>

  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
  </startup>


  <system.serviceModel>
    <services>
      <!--添加服务-->
      <service name="WcfService1.Service1" behaviorConfiguration="CalculatorServiceBehavior">
        <!--name 必须与代码中的host实例初始化的服务一样 behaviorConfiguration 行为配置 -->
        <host>
          <baseAddresses>
            <!--添加调用服务地址-->
            <add baseAddress="http://10.18.33.181:8000/"/>
          </baseAddresses>

        </host>
        <!--添加契约接口 contract="WcfDemo.IService1" WcfDemo.IService1为契约接口 binding="wsHttpBinding" wsHttpBinding为通过Http调用-->
        <endpoint address="" binding="basicHttpBinding" contract="WcfService1.IService1"></endpoint>
      </service>

    </services>
    <!--定义CalculatorServiceBehavior的行为-->
    <behaviors>
      <serviceBehaviors>
        <behavior name="CalculatorServiceBehavior">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>

        </behavior>
      </serviceBehaviors>

    </behaviors>
  </system.serviceModel>

</configuration>

OK,编译项目,产生exe文件,然后用管理员的权限去执行这个执行程序,8000端口的webservice服务就准备好了。

浏览器访问一下这个接口,http://10.18.33.181:8000/,出来微软为我们做好的接口说明。

搭建一个WebService接口环境(SAP PO 开发 二)

用这个: http://10.18.33.181:8000/?wsdl      地址去在SOAPUI上建一个测试demo.

在SOAPUI中双机参数,填上一个值,测试:

搭建一个WebService接口环境(SAP PO 开发 二)

我们的环境准备完成,下一步,我们要把SAP PO摆在中间,看看SAP PO怎么去配置。

 

相关文章:

  • 2022-12-23
  • 2021-09-28
  • 2022-01-02
  • 2021-06-20
  • 2021-10-15
  • 2021-11-17
  • 2021-10-05
  • 2021-07-27
猜你喜欢
  • 2021-10-07
  • 2021-07-21
  • 2021-10-25
  • 2021-09-25
  • 2021-12-14
  • 2021-04-07
相关资源
相似解决方案