gpt4 book ai didi

托管应用程序中的 C# 方法,在客户端应用程序中变成类

转载 作者:太空宇宙 更新时间:2023-11-03 15:01:07 25 4
gpt4 key购买 nike

我一直在尝试创建一个可以通过 WCF 从其他正在运行的应用程序接收信息的应用程序。我在单独的类中设置了一个 void 方法,创建了接口(interface),并托管了服务。

在我的主机应用程序中,我有以下方法。

public Class ReceivingMethods : IReceivingMethods
{
Public void HelloWorld(string text)
{
MessageBox.Show(text);
}
}

[ServiceContract]
interface iReceivingMethods
{
[OperationContract]
void HelloWorld(string text);
}

在客户端,我想这样做:

HostService client = new HostService();
client.HelloWorld("Hello World");
client.close();

但它不起作用,我必须这样做。

HostService client = new HostService();
HelloWorld hi = new HelloWorld();
hi.text = "Hello World";
client.HelloWorld(hi);
client.close();

我已经让它像以前一样在应用程序/ASP 组合中工作,但在这个应用程序中没有,我找不到两个应用程序之间的设置有任何区别。

谁能告诉我 WCF 设置需要什么才能让它像前者一样工作?

最佳答案

HostService client = new HostService();

您没有提到要使用哪个端点或哪个类对象。通常,服务主机类必须创建特定端点的对象,如下所示。

     using(System.ServiceModel.ServiceHost host = 
new System.ServiceModel.ServiceHost(typeof(ReceivingMethodsnamespace.ReceivingMethods )))
{
host.Open();
Console.WriteLine("Host started @ " + DateTime.Now.ToString());
Console.ReadLine();
}

通常主机服务必须创建一个实现服务契约(Contract)接口(interface)的类的对象(地址绑定(bind)契约(Contract)文件的服务名称)

关于托管应用程序中的 C# 方法,在客户端应用程序中变成类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46081978/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com