Creation and deployment of SOA
SOA是面向服务的体系架构,它更多的是停留于抽象的层面的概念,如果要实现对它的制作和调用,我们要借助于一个载体,我这里用的是Web Service,它也是实现SOA的途径之一。下面就来详细介绍SOA的制作和调用。
1. New project
1. Create a new project and select "Console Application (.Net Framework)", as shown in the figure below.
2. Configure a new project, as shown in the figure below.
Second, the production of SOA
1. Right-click "Solution "Client"" and select "Add | New Project", as shown in the figure below.
2. Add a new project and select "ASP.NET Web Application (.Net Framework)", as shown in the figure below.
3. Configure the new project, as shown in the figure below.
4. Create a new ASP.NET Web application, as shown in the figure below.
5. In this way, a new website is created, as shown in the figure below.
6. Right-click "WebSite" and select "Add | New Item", as shown in the figure below.
7. Add Web service (ASMX), the steps are shown in the figure below.
8. Make a multiplier and write a method function for multiplication. The code is as follows.
/// <summary>
/// 自定义添加一个乘法行为
/// </summary>
/// <param name="_numberA"></param>
/// <param name="_numberB"></param>
/// <returns></returns>
[WebMethod]
public int MUL(int _numberA, int _numberB)
{
return _numberA * _numberB;
}
9. Right-click WebService1.asmx and select "View in Browser", as shown in the figure below.
10. Enter the values of _numberA and _numberB in the box, click "Call", the operation steps and call results are shown in the figure below.
At this point, the creation of the Web service is complete. The next thing to do is to call it.
Three, SOA calls
Method 1: console mode
1. Right-click "Solution "Client"" and select "Add | New Project", as shown in the figure below.
2. Add a new project and select "Console Application (.Net Framework)", as shown in the figure below.
3. Configure the new project, as shown in the figure below.
4. "Add service reference", as shown in the figure below.
5. Copy the URL https://localhost:44355/WebService1.asmx in the second picture of "2. SOA Production 9", as shown in the figure below.
6. Add code and start the console, as shown in the figure below.
namespace ConsoleClient
{
class Program
{
static void Main(string[] args)
{
ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();//调用Web Service
int result = client.MUL(8, 9);//触发乘法器的方法
Console.WriteLine(result);
Console.ReadKey(true);
}
}
}
7. The running result is shown in the figure below.
The console calls the Web Service and it ends.
Method 2: Form Mode
1. Right-click "Solution "Client"" and click "Add | New Project", as shown in the figure below.
2. Add a new project and select "Windows Form Application (.Net Framework)", as shown in the figure below.
3. Configure the new project, as shown in the figure below.
4. "Add service reference", as shown in the figure below.
5. Copy the URL https://localhost:44355/WebService1.asmx in the second picture of "2. SOA Production 9", as shown in the figure below.
6. Add some controls in the Form1 dialog box, as shown in the figure below.
7. Double-click the "=" button to program it, the code is as follows.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsClient
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int numberA = int.Parse(textBox1.Text.ToString());//强转
int numberB = int.Parse(textBox2.Text.ToString());
ServiceReference1.WebService1SoapClient client = new ServiceReference1.WebService1SoapClient();//调用Web Service
int result = client.MUL(numberA, numberB);
MessageBox.Show(result.ToString());
}
}
}
8. Start the form mode, and the running effect is shown in the figure below.
The form calls the Web Service and it ends.
At this point, the creation and invocation of Web Service is all over.