Learning URL: https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/how-to-create-a-wcf-client
Local resource configuration and IIS resource configuration I learned according to Microsoft official website, interested can follow to type, I only calculate problems I encountered here

IIS

  1. When configuring service in IIS, creating virtual directory according to tutorial, found if I create service in default site will prompt InvalidOperationException: Cannot find type “Microsoft.ServiceModel.Samples.CalculatorService”, found I already hosted a service in default site causing problem. Should be because of having a cold today a bit slow
  2. New Site:
    1.1. Image Description
    Input our path port and name, same step as creating Web site.
  3. Create files APP_Code, service.svc, Web.config, among which Web.config file following official case will appear later
    Could not establish secure channel for SSL/TLS with authority with authority 'endpoint'. This might be because the server certificate is not configured properly with HTTP.SYS in the HTTPS case. This could also be caused by a mismatch of the security binding between the client and the server.
    We need to add field in server
1
2
3
4
<defaultPorts>
<add scheme="http" port="8081" />
<add scheme="https" port="8081" />
</defaultPorts>
1
2
3
4
5
6
7
<bindings>
<wsHttpBinding>
<binding>
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings>

Full text is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="Microsoft.ServiceModel.Samples.CalculatorService" behaviorConfiguration="CalculatorServiceBehaviors">

<!-- This endpoint is exposed at the base address provided by host: http://localhost/servicemodelsamples/service.svc -->
<endpoint address=""
binding="wsHttpBinding"
contract="Microsoft.ServiceModel.Samples.ICalculator" />

<!-- The mex endpoint is exposed at http://localhost/servicemodelsamples/service.svc/mex -->
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding>
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<serviceBehaviors>
<behavior name="CalculatorServiceBehaviors">
<!-- Add the following element to your service behavior configuration. -->
<serviceMetadata httpGetEnabled="true" />
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="http" port="8081" />
<add scheme="https" port="8081" />
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>

</configuration>

File content of service.svc

1
<%@ServiceHost language=c# Debug="true" Service="Microsoft.ServiceModel.Samples.CalculatorService" CodeBehind="CalculatorService.svc.cs" %>

Only placed one cs file in App_Code file according to official

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
using System;  
using System.ServiceModel;

namespace Microsoft.ServiceModel.Samples
{

[ServiceContract]
public interface ICalculator
{
[OperationContract]
double Add(double n1, double n2);
[OperationContract]
double Subtract(double n1, double n2);
[OperationContract]
double Multiply(double n1, double n2);
[OperationContract]
double Divide(double n1, double n2);
}

public class CalculatorService: ICalculator
{
public double Add(double n1, double n2)
{
return n1 + n2;
}
public double Subtract(double n1, double n2)
{
return n1 - n2;
}
public double Multiply(double n1, double n2)
{
return n1 * n2;
}
public double Divide(double n1, double n2)
{
return n1 / n2;
}
}

}
  1. After completing these we open browser input http://localhost:8081/service.svc, detect if server configuration is complete
    Image Description

Client

Use administrator mode to open VisualStudio 2019, here operate according to written in document, starting without administrator identity will appear error reporting problem, e.g.: Unable to add service reference

  1. Create a new Console Application based on .NetFramework
  2. Add reference System.serviceModel
    Image Description
  3. Add reference service
    Image Description
    Click Go To

Image Description

Can see our published WCF service, after clicking OK, enter Program.cs

4.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IISCalculator.ServiceReference1;

namespace IISCalculator
{
class Program
{
static void Main(string[] args)
{
CalculatorClient client = new CalculatorClient();
double value1 = 100.00D;
double value2 = 39.9D;
double reuslt = client.Add(value1, value2);
Console.WriteLine("value1:{0} add value2:{1} is : {2}",value1,value2,reuslt);
Console.Read();
}
}
}

Image Description
Console will calculate result according to our calculation service.
Setting and referencing on IIS is finished. However VS now provides Tool -> WCF Configuration Manager can quickly configure file. If newbie starting just like me better configure manually once to try :)

Build Database WCF

Reference Link:https://blog.csdn.net/tianlelu/article/details/81905916

  1. Create WCF application in VS
  2. Delete Iservice and service two files inside, right click project->Add->WCF Service, name it MySqlInfo

IMysqlInfo.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace WcfMySqlService
{
// Note: Use "Rename" command on "Refactor" menu to change interface name "IMysqlInfo" in both code and config file simultaneously.
[ServiceContract]
public interface IMysqlInfo
{
[OperationContract]
void DoWork();

[OperationContract]
DataSet GetDataSet();
}
}

MysqlInfo.svc.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
namespace WcfMySqlService
{
// Note: Use "Rename" command on "Refactor" menu to change class name "MysqlInfo" in code, svc and config file simultaneously.
// Note: In order to launch WCF Test Client for testing this service, please select MysqlInfo.svc or MysqlInfo.svc.cs in Solution Explorer and start debugging.
public class MysqlInfo : IMysqlInfo
{
public void DoWork()
{
}

public DataSet GetDataSet()
{
string connector = "server=localhost;port=3306;user=user;password=password;database=recheck_model";
using (MySqlConnection connection = new MySqlConnection(connector))
{
try
{
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter("select * from problem_model", connection);
DataSet set = new DataSet();
adapter.Fill(set, "item_info");
return set;
}
catch (Exception e)
{
Debug.Print(e.Message);
return null;
}
finally
{
connection.Close();
}
}
}
}
}

Right click project->Debug->Start new instance Check if WCF project generated successfully

  1. Right click project file explorer to file location, select bin file and .svc, Web.config files place to corresponding IIS directory location, input URL in webpage to check if deployment successful
    Image Description
  2. Additionally create a WPF project, drag in a DataGrid to display database data.
  3. Input URL to build connection service according to way built above, and reference System.serviceModel.dll
  4. Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WcfMysqlClient.ServiceReference1;

namespace WcfMysqlClient
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataGrid.ItemsSource = GetTable().DefaultView;
}

DataTable GetTable()
{
MysqlInfoClient client = new MysqlInfoClient();
var table = client.GetDataSet();
return table.Tables[0];
}
}
}

Image Description
Can see values in database can be successfully retrieved, this completes simple construction of a Mysql database WCF project