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
- 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 - New Site:
1.1.
Input our path port and name, same step as creating Web site. - Create files
APP_Code,service.svc,Web.config, among whichWeb.configfile following official case will appear laterCould 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
<defaultPorts>
<add scheme="http" port="8081" />
<add scheme="https" port="8081" />
</defaultPorts>
<bindings>
<wsHttpBinding>
<binding>
<security mode="None"/>
</binding>
</wsHttpBinding>
</bindings>
Full text is:
<?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
<%@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
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;
}
}
}
- After completing these we open browser input
http://localhost:8081/service.svc, detect if server configuration is complete
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
- Create a new Console Application based on .NetFramework
- Add reference
System.serviceModel
- Add reference service
Click Go To

Can see our published WCF service, after clicking OK, enter Program.cs
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();
}
}
}
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
- Create WCF application in VS
- Delete Iservice and service two files inside, right click project->Add->WCF Service, name it
MySqlInfo
IMysqlInfo.cs
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
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
- Right click project file explorer to file location, select
binfile and .svc, Web.config files place to corresponding IIS directory location, input URL in webpage to check if deployment successful
- Additionally create a WPF project, drag in a DataGrid to display database data.
- Input URL to build connection service according to way built above, and reference
System.serviceModel.dll - Code:
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];
}
}
}
Can see values in database can be successfully retrieved, this completes simple construction of a Mysql database WCF project