wcfサービスを作成し、mysqlデータベースからレコードを取得するメソッドを定義しました。Windowsフォームアプリケーションからメソッドを呼び出すと、例外がスローされます。null値が処理されなかったということです。私には2つの異なる問題があります。1つは明らかにnull値を処理する方法であり、もう1つは、データベースにレコードがあるためメソッドが機能していないため、null値を受け取らないようにすることです。
フォームアプリケーション内でメソッドを定義すると、正常に機能しますが、WCFサービスでは機能しません。私はデータベースにmysqlを使用しています。
Wcfコードは以下のとおりです。
IService1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
namespace WcfService3
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
[ServiceContract]
public interface IService1
{
// [OperationContract]
// string InsertUserDetails(EmpDetails userInfo);
[OperationContract]
DataSet SelectEmpDetails();
// TODO: Add your service operations here
}
// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class CompositeType
{
bool boolValue = true;
string stringValue = "Hello ";
[DataMember]
public bool BoolValue
{
get { return boolValue; }
set { boolValue = value; }
}
[DataMember]
public string StringValue
{
get { return stringValue; }
set { stringValue = value; }
}
}
[DataContract]
public class EmpDetails
{
int empno;
string ename;
float sal;
int deptno;
string email;
[DataMember]
public int EmpNp
{
get { return empno; }
set { empno = value; }
}
[DataMember]
public string EName
{
get { return ename; }
set { ename = value; }
}
[DataMember]
public float Sal
{
get { return sal; }
set { sal = value; }
}
[DataMember]
public int DeptNo
{
get { return deptno; }
set { deptno = value; }
}
[DataMember]
public string Email
{
get { return email; }
set { email = value; }
}
}
}
Service1.svc
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows.Forms;
namespace WcfService3
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
// NOTE: In order to launch WCF Test Client for testing this service, please select Service1.svc or Service1.svc.cs at the Solution Explorer and start debugging.
public class Service1 : IService1
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
//Constructor
public Service1()
{
Initialize();
}
private void Initialize()
{
server = "192.168.1.222";
database = "cabee";
uid = "user";
password = "Password1";
string connectionString;
connectionString = "SERVER=" + server + ";" + "DATABASE=" +
database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";
connection = new MySqlConnection(connectionString);
}
private bool OpenConnection()
{
try
{
connection.Open();
return true;
}
catch (MySqlException ex)
{
//When handling errors, you can your application's response based
//on the error number.
//The two most common error numbers when connecting are as follows:
//0: Cannot connect to server.
//1045: Invalid user name and/or password.
switch (ex.Number)
{
case 0:
MessageBox.Show("Cannot connect to server. Contact administrator");
break;
case 1045:
MessageBox.Show("Invalid username/password, please try again");
break;
}
return false;
}
}
//Close connection
private bool CloseConnection()
{
try
{
connection.Close();
return true;
}
catch (MySqlException ex)
{
MessageBox.Show(ex.Message);
return false;
}
}
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
public CompositeType GetDataUsingDataContract(CompositeType composite)
{
if (composite == null)
{
throw new ArgumentNullException("composite");
}
if (composite.BoolValue)
{
composite.StringValue += "Suffix";
}
return composite;
}
public DataSet SelectEmpDetails()
{
DataSet ds = new DataSet();
if (this.OpenConnection() == true)
{
MySqlCommand cmd = new MySqlCommand("Select * from empinfo", connection);
MySqlDataAdapter sda = new MySqlDataAdapter(cmd);
sda.Fill(ds);
cmd.ExecuteNonQuery();
this.CloseConnection();
return ds;
}
else
{
return ds;
}
}
}
}
Windowsフォームアプリケーション
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private MySqlConnection connection;
private string server;
private string database;
private string uid;
private string password;
ServiceReference1.Service1Client obj = new ServiceReference1.Service1Client(); // Add service reference
public Form1()
{
showdata();
}
private void showdata() // to show the data in the DataGridView
{
DataSet ds = new DataSet();
ds = obj.SelectEmpDetails();
dataGridView1.DataSource = ds.Tables[0];
dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCells);
}
}
}
エラーメッセージ
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=WindowsFormsApplication1
StackTrace:
at WindowsFormsApplication1.Form1.showdata() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 100
at WindowsFormsApplication1.Form1..ctor() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Form1.cs:line 29
at WindowsFormsApplication1.Program.Main() in c:\Users\Anup\Google Drive\WindowsFormsApplication1\WindowsFormsApplication1\Program.cs:line 19
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: