如何在.NET上组织客户服务器应用程序(不在.NET框架上)?
与.NET框架相比,在.NET(含义.NET Core和.net)中,这种方法已被更改,显然是显着的。
在.NET框架中,像这样组织了一个客户端服务器应用程序:
对于服务器:
[ServiceContract]
interface IContract
{
[OperationContract]
void say_something(string s);
[OperationContract]
int sum(int a, int b);
}
class Service: IContract
{
public int sum(int a, int b)
{
return a + b;
}
void IContract.say_something(string s)
{
MessageBox.Show(s);
}
}
class Server
{
ServiceHost host;
public void open_host()
{
Uri address = new Uri(Global.uri_local); // A
BasicHttpBinding binding = new BasicHttpBinding(); // B
Type contract = typeof(IContract); // C
host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(contract, binding, address);
host.Open();
}
}
Settings
class Global
{
public static string uri_local = "http://localhost:4000/IContract";
}
对于客户端:
class Client
{
IContract channel;
public Client()
{
create_channel();
}
public void create_channel()
{
Uri address = new Uri(Global.uri_local);
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(address);
ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, endpoint);
channel = factory.CreateChannel();
}
public void send_message_to_server(string text)
{
channel.say_something(text);
}
public void find_sum_of_3_and_2()
{
MessageBox.Show(channel.sum(2, 3).ToString());
}
}
[ServiceContract]
interface IContract
{
[OperationContract]
void say_something(string s);
[OperationContract]
int sum (int a, int b);
}
Settings
class Global
{
public static string uri_local = "http://localhost:4000/IContract";
}
但是,在.NET中,所有内容的组织方式都不同。 我一直在搜索,找不到互联网上的示例。也许我在错误的地方看。 MSDN有一个.NET框架的示例,但没有.NET。同时,我在某些论坛上看到了这个问题,但答案只是事实的说法:在.NET中,微软更改了组织客户服务器互动的方法。 我认为,如果您举个例子,那么这对许多寻求者来说都是有用的。
如何在.NET中组织客户服务器交互?
请举一个简单的客户端服务器应用程序的示例。
In .NET (meaning .NET Core and .NET), compared to the .NET Framework, this approach has been changed, and, apparently, significantly.
In the .NET Framework, a client-server application was organized like this:
For the server:
[ServiceContract]
interface IContract
{
[OperationContract]
void say_something(string s);
[OperationContract]
int sum(int a, int b);
}
class Service: IContract
{
public int sum(int a, int b)
{
return a + b;
}
void IContract.say_something(string s)
{
MessageBox.Show(s);
}
}
class Server
{
ServiceHost host;
public void open_host()
{
Uri address = new Uri(Global.uri_local); // A
BasicHttpBinding binding = new BasicHttpBinding(); // B
Type contract = typeof(IContract); // C
host = new ServiceHost(typeof(Service));
host.AddServiceEndpoint(contract, binding, address);
host.Open();
}
}
Settings
class Global
{
public static string uri_local = "http://localhost:4000/IContract";
}
For client:
class Client
{
IContract channel;
public Client()
{
create_channel();
}
public void create_channel()
{
Uri address = new Uri(Global.uri_local);
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress endpoint = new EndpointAddress(address);
ChannelFactory<IContract> factory = new ChannelFactory<IContract>(binding, endpoint);
channel = factory.CreateChannel();
}
public void send_message_to_server(string text)
{
channel.say_something(text);
}
public void find_sum_of_3_and_2()
{
MessageBox.Show(channel.sum(2, 3).ToString());
}
}
[ServiceContract]
interface IContract
{
[OperationContract]
void say_something(string s);
[OperationContract]
int sum (int a, int b);
}
Settings
class Global
{
public static string uri_local = "http://localhost:4000/IContract";
}
However, in .NET, everything is organized differently.
I have been searching and could not find examples on the Internet. Maybe I'm looking in the wrong place. MSDN has an example for the .NET Framework, but not for .NET. At the same time, I saw this question on some forums, but the answers to it were only a statement of fact: in .NET, Microsoft changed the approach to organizing client-server interaction.
I think if you give an example, it will be useful for many seekers.
How is client-server interaction organized in .NET?
Please, give an example of a simple client-server application.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论