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();
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(); //} //} }
<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>
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;
}
}
If confused just starting, URI location is generally location of referenced service  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 forthis part, feel pretty good.
### Error Two `Service does not support content type application/soap+xml; charset=utf-8. Client and service` code:
This is original client configuration. Because thisis just wanting to simply test project, so binding did notset all application initialization settings. Later troubleshooting found it is problem of binding protocol here. Client binding protocol is:
 Modified code should be: