Windows 使用 .NET 远程处理服务的表单应用程序失败
Windows Form Application failed with .NET Remoting Service
我正在尝试使用应用程序中的 windows 将数据插入数据库。我将它托管到控制台应用程序中。我正在使用 .net 远程调用方法。我的主机是 运行ning 没有任何问题,我也可以 运行 windows 表单应用程序没有任何问题。但问题是,当我点击提交按钮插入数据时,我得到 error.I 不知道为什么会出现此错误。
抛出异常:'System.NullReferenceException' in mscorlib.dll
附加信息:未将对象引用设置为对象的实例。发生
这是界面。
namespace IHelloRemotingService
{
public interface IHelloRemotingService
{
void Insert(string Name, string Address, string Email, string Mobile)
}
}
这里是接口的实现..
public class HelloRemotingService : MarshalByRefObject , IHelloRemotingService.IHelloRemotingService
{
public void Insert(string Name, string Address, string Email, string Mobile)
{
string constr = ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("AddNewStudent", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@EmailID", Email);
cmd.Parameters.AddWithValue("@Mobile", Mobile);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
托管服务代码....
namespace RemotingServiceHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" .NET Remoting Test Server");
Console.WriteLine(" *************************");
Console.WriteLine();
try
{
StartServer();
Console.WriteLine("Server started");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Server.Main exception: " + ex);
}
Console.WriteLine("Press <ENTER> to exit.");
Console.ReadLine();
StopServer();
}
static void StartServer()
{
RegisterBinaryTCPServerChannel(500);
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloRemotingService.HelloRemotingService),
"Insert.rem",
WellKnownObjectMode.Singleton);
}
static void StopServer()
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch (Exception ex)
{
Console.WriteLine("Server.StopServer exception: " + ex);
}
}
}
static void RegisterBinaryTCPServerChannel(int port, string name = "tcp srv")
{
IServerChannelSinkProvider firstServerProvider;
IClientChannelSinkProvider firstClientProvider;
var channelProperties = new Hashtable();
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = port;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null); // binary formatter
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
windows 表单申请代码..
namespace HelloRemotingServiceClient
{
public partial class InsertStudentData : Form
{
public InsertStudentData()
{
InitializeComponent();
RegisterBinaryTcpClientChannel();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var remService = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject(typeof(IHelloRemotingService.IHelloRemotingService), "tcp://localhost:500/Insert.rem");
remService.Insert(textName.Text, textAddress.Text, textEmail.Text, textBox1.Text);
label5.Text = "Recored Inserted Successfully";
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RegisterBinaryTcpClientChannel(string name = "tcp client")
{
IClientChannelSinkProvider firstClientProvider;
IServerChannelSinkProvider firstServerProvider;
var channelProperties = new Hashtable();
channelProperties["name"] = name;
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = 0; // auto
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null);
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
表格的设计..
这是错误消息的屏幕截图。
文本框能够捕获值,但为什么会抛出此错误?
这是一个工作项目。您没有配置格式化程序。
SharedLib 项目:
namespace IHelloRemotingService
{
public interface IHelloRemotingService
{
void Insert(string Name, string Address, string Email, string Mobile);
}
}
服务器控制台项目:
namespace Server
{
public class HelloRemotingService : MarshalByRefObject, IHelloRemotingService.IHelloRemotingService
{
public HelloRemotingService()
{
}
public void Insert(string Name, string Address, string Email, string Mobile)
{
Console.WriteLine("HelloRemotingService.Insert called");
}
public override object InitializeLifetimeService()
{
return null; // manage lifetime by myself
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" .NET Remoting Test Server");
Console.WriteLine(" *************************");
Console.WriteLine();
try
{
StartServer();
Console.WriteLine("Server started");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Server.Main exception: " + ex);
}
Console.WriteLine("Press <ENTER> to exit.");
Console.ReadLine();
StopServer();
}
static void StartServer()
{
RegisterBinaryTCPServerChannel(500);
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloRemotingService),
"Insert.rem",
WellKnownObjectMode.Singleton);
}
static void StopServer()
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch(Exception ex)
{
Console.WriteLine("Server.StopServer exception: " + ex);
}
}
}
static void RegisterBinaryTCPServerChannel(int port, string name = "tcp srv")
{
IServerChannelSinkProvider firstServerProvider;
IClientChannelSinkProvider firstClientProvider;
var channelProperties = new Hashtable();
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = port;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null); // binary formatter
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
客户端 WinForms 项目:
namespace Client
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RegisterBinaryTcpClientChannel();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (MainForm form = new MainForm())
{
Application.Run(form);
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch (Exception ex)
{
Debug.WriteLine("Client.Dispose exception: " + ex);
}
}
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
private void _btnAccessServer_Click(object sender, EventArgs e)
{
try
{
var remService = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject(typeof(IHelloRemotingService.IHelloRemotingService), "tcp://localhost:500/Insert.rem");
remService.Insert("MyName", "MyAddress", "MyEmail", "MyMobile");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RegisterBinaryTcpClientChannel(string name = "tcp client")
{
IClientChannelSinkProvider firstClientProvider;
IServerChannelSinkProvider firstServerProvider;
var channelProperties = new Hashtable();
channelProperties["name"] = name;
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = 0; // auto
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null);
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
我正在尝试使用应用程序中的 windows 将数据插入数据库。我将它托管到控制台应用程序中。我正在使用 .net 远程调用方法。我的主机是 运行ning 没有任何问题,我也可以 运行 windows 表单应用程序没有任何问题。但问题是,当我点击提交按钮插入数据时,我得到 error.I 不知道为什么会出现此错误。
抛出异常:'System.NullReferenceException' in mscorlib.dll 附加信息:未将对象引用设置为对象的实例。发生
这是界面。
namespace IHelloRemotingService
{
public interface IHelloRemotingService
{
void Insert(string Name, string Address, string Email, string Mobile)
}
}
这里是接口的实现..
public class HelloRemotingService : MarshalByRefObject , IHelloRemotingService.IHelloRemotingService
{
public void Insert(string Name, string Address, string Email, string Mobile)
{
string constr = ConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("AddNewStudent", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Name", Name);
cmd.Parameters.AddWithValue("@Address", Address);
cmd.Parameters.AddWithValue("@EmailID", Email);
cmd.Parameters.AddWithValue("@Mobile", Mobile);
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
}
托管服务代码....
namespace RemotingServiceHost
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" .NET Remoting Test Server");
Console.WriteLine(" *************************");
Console.WriteLine();
try
{
StartServer();
Console.WriteLine("Server started");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Server.Main exception: " + ex);
}
Console.WriteLine("Press <ENTER> to exit.");
Console.ReadLine();
StopServer();
}
static void StartServer()
{
RegisterBinaryTCPServerChannel(500);
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloRemotingService.HelloRemotingService),
"Insert.rem",
WellKnownObjectMode.Singleton);
}
static void StopServer()
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch (Exception ex)
{
Console.WriteLine("Server.StopServer exception: " + ex);
}
}
}
static void RegisterBinaryTCPServerChannel(int port, string name = "tcp srv")
{
IServerChannelSinkProvider firstServerProvider;
IClientChannelSinkProvider firstClientProvider;
var channelProperties = new Hashtable();
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = port;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null); // binary formatter
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
windows 表单申请代码..
namespace HelloRemotingServiceClient
{
public partial class InsertStudentData : Form
{
public InsertStudentData()
{
InitializeComponent();
RegisterBinaryTcpClientChannel();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
var remService = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject(typeof(IHelloRemotingService.IHelloRemotingService), "tcp://localhost:500/Insert.rem");
remService.Insert(textName.Text, textAddress.Text, textEmail.Text, textBox1.Text);
label5.Text = "Recored Inserted Successfully";
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RegisterBinaryTcpClientChannel(string name = "tcp client")
{
IClientChannelSinkProvider firstClientProvider;
IServerChannelSinkProvider firstServerProvider;
var channelProperties = new Hashtable();
channelProperties["name"] = name;
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = 0; // auto
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null);
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
表格的设计..
这是错误消息的屏幕截图。
这是一个工作项目。您没有配置格式化程序。
SharedLib 项目:
namespace IHelloRemotingService
{
public interface IHelloRemotingService
{
void Insert(string Name, string Address, string Email, string Mobile);
}
}
服务器控制台项目:
namespace Server
{
public class HelloRemotingService : MarshalByRefObject, IHelloRemotingService.IHelloRemotingService
{
public HelloRemotingService()
{
}
public void Insert(string Name, string Address, string Email, string Mobile)
{
Console.WriteLine("HelloRemotingService.Insert called");
}
public override object InitializeLifetimeService()
{
return null; // manage lifetime by myself
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine(" .NET Remoting Test Server");
Console.WriteLine(" *************************");
Console.WriteLine();
try
{
StartServer();
Console.WriteLine("Server started");
Console.WriteLine();
}
catch (Exception ex)
{
Console.WriteLine("Server.Main exception: " + ex);
}
Console.WriteLine("Press <ENTER> to exit.");
Console.ReadLine();
StopServer();
}
static void StartServer()
{
RegisterBinaryTCPServerChannel(500);
RemotingConfiguration.CustomErrorsMode = CustomErrorsModes.Off;
RemotingConfiguration.RegisterWellKnownServiceType(typeof(HelloRemotingService),
"Insert.rem",
WellKnownObjectMode.Singleton);
}
static void StopServer()
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch(Exception ex)
{
Console.WriteLine("Server.StopServer exception: " + ex);
}
}
}
static void RegisterBinaryTCPServerChannel(int port, string name = "tcp srv")
{
IServerChannelSinkProvider firstServerProvider;
IClientChannelSinkProvider firstClientProvider;
var channelProperties = new Hashtable();
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = port;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null); // binary formatter
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}
客户端 WinForms 项目:
namespace Client
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
RegisterBinaryTcpClientChannel();
}
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
using (MainForm form = new MainForm())
{
Application.Run(form);
}
}
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing)
{
foreach (IChannel channel in ChannelServices.RegisteredChannels)
{
try
{
ChannelServices.UnregisterChannel(channel);
}
catch (Exception ex)
{
Debug.WriteLine("Client.Dispose exception: " + ex);
}
}
if (components != null)
components.Dispose();
}
base.Dispose(disposing);
}
private void _btnAccessServer_Click(object sender, EventArgs e)
{
try
{
var remService = (IHelloRemotingService.IHelloRemotingService)Activator.GetObject(typeof(IHelloRemotingService.IHelloRemotingService), "tcp://localhost:500/Insert.rem");
remService.Insert("MyName", "MyAddress", "MyEmail", "MyMobile");
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void RegisterBinaryTcpClientChannel(string name = "tcp client")
{
IClientChannelSinkProvider firstClientProvider;
IServerChannelSinkProvider firstServerProvider;
var channelProperties = new Hashtable();
channelProperties["name"] = name;
channelProperties["typeFilterLevel"] = TypeFilterLevel.Full;
channelProperties["machineName"] = Environment.MachineName;
channelProperties["port"] = 0; // auto
// create client format provider
var clientProperties = new Hashtable();
clientProperties["typeFilterLevel"] = TypeFilterLevel.Full;
var clientFormatProvider = new BinaryClientFormatterSinkProvider(clientProperties, null);
firstClientProvider = clientFormatProvider;
// create server format provider
var serverFormatProvider = new BinaryServerFormatterSinkProvider(null, null);
serverFormatProvider.TypeFilterLevel = TypeFilterLevel.Full;
firstServerProvider = serverFormatProvider;
TcpChannel tcp = new TcpChannel(channelProperties, firstClientProvider, firstServerProvider);
ChannelServices.RegisterChannel(tcp, false);
}
}
}