In previous WCF articles, successfully created local deployment and server deployment. But when I wanted to implement reading database information at Revit end, created a project to test effect.
Reference cases:
Revit Secondary Development Advanced Application (1) —— Using wcf for simple family library management
Drive Revit through a WCF Service

Code

Server

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;

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(string tableName , string command);

[OperationContract]
MySqlDataReader GetReader(string tableName, string command);

[OperationContract]
MySqlConnection GetConnection(string tableName);

[OperationContract]
bool SetDataToMySql(string tableName,string command);

}
}

using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using MySql.Data.MySqlClient;

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 tableName, string command)
{
string connector = $"server=localhost;port=3306;user=user;password=pwd;database={tableName}";
//using (MySqlConnection connection = new MySqlConnection(connector))
//{
try
{
MySqlConnection connection = new MySqlConnection(connector);
connection.Open();
MySqlDataAdapter adapter = new MySqlDataAdapter(command, connection);
DataSet set = new DataSet();
adapter.Fill(set, "item_info");
return set;
}
catch (Exception e)
{
Debug.Print(e.Message);
return null;
}
//finally
//{
// connection.Close();
//}
//}
}

public MySqlDataReader GetReader(string tableName, string command)
{
string connector = $"server=localhost;port=3306;user=user;password=pwd;database={tableName}";
using (MySqlConnection connection = new MySqlConnection(connector))
{
try
{
MySqlCommand cmd = new MySqlCommand()
{
Connection = connection,
CommandText = command
};
connection.Open();
MySqlDataReader reader = cmd.ExecuteReader();
cmd.Dispose();
return reader;
}
catch (Exception e)
{
Debug.Print(e.Message);
return null;
}
finally
{
connection.Close();
}
}
}

public MySqlConnection GetConnection(string tableName)
{
try
{
string connector = $"server=localhost;port=3306;user=user;password=pwd;database={tableName}";
MySqlConnection connection = new MySqlConnection(connector);
return connection;
}
catch (Exception e)
{
Debug.Print(e.Message);
return null;
}

}

public bool SetDataToMySql(string tableName, string command)
{
try
{
string connector = $"server=localhost;port=3306;user=user;password=pwd;database={tableName}";
MySqlConnection connection = new MySqlConnection(connector);
connection.Open();
MySqlCommand cmd = new MySqlCommand(command,connection);
var i = cmd.ExecuteNonQuery();
return i == 1 ? true : false;
}
catch (Exception e)
{
Debug.Print(e.Message);
return false;
}

}
}
}

web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>

<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true" targetFramework="4.6.1"/>
<httpRuntime targetFramework="4.6.1"/>
</system.web>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<!--
To browse web app root directory during debugging, set the value below to True.
Set this value to False before deployment to avoid disclosing web app folder information.
-->
<directoryBrowse enabled="true"/>
</system.webServer>

<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Buffers" publicKeyToken="CC7B13FFCD2DDD51" culture="neutral"/>
<bindingRedirect oldVersion="0.0.0.0-4.0.3.0" newVersion="4.0.3.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

```csharp
### Client
1. Add reference project

2.

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using WcfLinkRevit.ServiceReference1;
using WcfServiceLibrary;
using IService1 = WcfServiceLibrary.IService1;

namespace WcfLinkRevit
{
[Transaction(TransactionMode.Manual)]
public class Class1:IExternalCommand
{
private static readonly object _object = new object();
private IMysqlInfo m_server;
public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
{

        m_server = ChannelFactory<IMysqlInfo>.CreateChannel(new BasicHttpBinding(), 
            new EndpointAddress("http://120.27.124.206:8084/MysqlInfo.svc"));


        UIDocument uidoc = commandData.Application.ActiveUIDocument;
        var point = uidoc.Selection.PickPoint();
        if (point != null)
        {
            var set = m_server.GetDataSet("recheck_model", "select * from projectlist");
            StringBuilder builder = new StringBuilder("Revit -> Wcf -> Mysql");
            for (int i = 0; i < set.Tables[0].Columns.Count; i++)
            {
                var name = set.Tables[0].Columns[i].ColumnName;
                builder.AppendLine(name);
            }
            TaskDialog.Show("Wcf",builder.ToString());
            //TaskDialog.Show("Wcf", m_server.Subtract(20.00D, 10.02D).ToString());
        }


        return Result.Succeeded;
    }
}

}

1
Key point is:

m_server = ChannelFactory.CreateChannel(new BasicHttpBinding(),
new EndpointAddress(“http://localhost:port/MysqlInfo.svc“));

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

![Image Description](https://cdn.bimath.com/blog/pg/381e2ef2a54da11bda0897e08fa44ede.png#pic_center)

If confused just starting, URI location is generally location of referenced service
![Image Description](https://cdn.bimath.com/blog/pg/76b524506a7188cbf8d16ce8ac6feac1.png#pic_center)
The URL above is URI address

## Error Collection
### Error One
`
"Could not find default endpoint element that references contract 'ServiceReference1.IMysqlInfo' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."
`
But found can run normally during debugging. Searched WCF cases related to Revit, whole search only found above two cases, basically no explanation about intermediate error info.
Later went to [stackoverflow](https://stackoverflow.com/questions/25439631/wcf-client-configuration-from-custom-location?r=SearchResults)
Saw this problem. Someone already solved it. Everyone can check the reply. Author below has own explanation for this part, feel pretty good.

### Error Two
`Service does not support content type application/soap+xml; charset=utf-8. Client and service`
code:

_mysqlInfo = ChannelFactory.CreateChannel(new WsHttpBinding(),
new EndpointAddress(“http://127.0.0.1:8084/MysqlInfo.svc“));

1
2
3
4
5
6

This is original client configuration. Because this is just wanting to simply test project, so binding did not set all application initialization settings.
Later troubleshooting found it is problem of binding protocol here. Client binding protocol is:

![Image Description](https://cdn.bimath.com/blog/pg/89b67eb223efe3cb7f89fcd21fcfbb6b.png#pic_center)
Modified code should be:

_mysqlInfo = ChannelFactory.CreateChannel(new BasicHttpBinding(),
new EndpointAddress(“http://120.27.124.206:8084/MysqlInfo.svc“));
```csharp