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 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
<!-- 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>
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 :)
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();
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 bin file 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
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