学习网址:https://docs.microsoft.com/zh-cn/dotnet/framework/wcf/how-to-create-a-wcf-client
本地资源配置与IIS资源配置我是根据微软官网学习的,有兴趣的可以跟着敲一下,我这里只说一下我遇到的问题

IIS

  1. 再IIS中配置服务时,根据教程上创建虚拟目录,发现如果我如果再默认站点创建服务将会提示InvalidOperationException: 找不到类型“Microsoft.ServiceModel.Samples.CalculatorService,发现我再默认站点已经托管了一个服务导致的问题。应该是今天感冒有点迟钝
    新建站点
    1.1. 在这里插入图片描述
    将我们的路径端口及名称输入即可,和创建Web站点一个步骤。
  2. 创建文件APP_Codeservice.svcWeb.config,其中Web.config文件按照官方给的案例后面将会出现
    无法打开安全通道,因为与远程终结点的安全协商已失败。这可能是由于用于创建通道的 EndpointAddress 中不存在 EndpointIdentity 或错误指定了 EndpointIdentity。请确认由 EndpointAddress 指定或暗示的 EndpointIdentity 正确标识了远程终结点。,我们需要再服务端添加字段
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>

全文为:

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>

service.svc的文件内容

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

App_Code文件中根据官方只放置了一个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
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. 完成这些之后我们打开浏览器输入http://localhost:8081/service.svc,检测服务端是否配置完成
    在这里插入图片描述

客户端

使用管理员模式打开VisualStuodio 2019,这里根据文档中写的进行操作,不使用管理员身份启动将会出现报错问题,比如:无法添加服务引用

  1. 新建一个基于.NetFramwork 的控制台应用程序
  2. 添加引用System.serviceModel
    在这里插入图片描述
  3. 添加引用服务
    在这里插入图片描述
    点击转到

在这里插入图片描述

可以看到我们发布的WCF服务,点击确定之后,进入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();
}
}
}

在这里插入图片描述
控制台将会根据我们的计算服务计算结果。
在IIS上设置并引用就算结束了。不过VS现在提供了工具 -> WCF配置管理器可以快速的配置文件,如果和我一样是刚开始使用的菜鸟还是手动配置一遍尝试一下比较好:)

搭建数据库WCF

参考连接:https://blog.csdn.net/tianlelu/article/details/81905916

  1. VS中创建 WCF应用程序
  2. 删除其中的Iservise和service两个文件,右键项目->添加->WCF服务,命名为MySqlInfo

IMysqlInfo.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
namespace WcfMySqlService
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IMysqlInfo”。
[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
{
// 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码、svc 和配置文件中的类名“MysqlInfo”。
// 注意: 为了启动 WCF 测试客户端以测试此服务,请在解决方案资源管理器中选择 MysqlInfo.svc 或 MysqlInfo.svc.cs,然后开始调试。
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();
}
}
}
}
}

右键项目->调试->启动新项目 查看WCF项目是否生成成功

  1. 右键点击项目文件浏览器到文件位置,选择bin文件及.svc、Web.config文件放置到相应的IIS目录位置,在网页输入网址查看是否部署成功
    在这里插入图片描述
  2. 另外新建一个WPF项目,拖入一个DataGrid显示数据库数据。
  3. 按照上面搭建的方式输入网址搭建连接服务,并引用System.serviceModel.dll
  4. 代码:
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>
/// 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];
}
}
}

在这里插入图片描述
可以看到数据库里面的值能够成功获取,这样就完成了一个Mysql数据库WCF项目的简单搭建