gpt4 book ai didi

c# - 控制台应用宿主wcf服务交互

转载 作者:太空宇宙 更新时间:2023-11-03 11:10:21 26 4
gpt4 key购买 nike

由控制台应用程序托管的 WCF 服务可以与所述控制台应用程序交互以执行 Console.WriteLine() 的最简单方法是什么。

部分代码:

契约(Contract):

[ServiceContract(Name = "IProdsService")]
public interface IProdsService
{
[OperationContract(Name = "Alert",IsOneWay=true)]
void Alert(string msg);
}

服务:

public class ProdsService : IProdsService
{
//IProdsService.Alert implementation
public void Alert(string msg)
{
//TODO: Send Alert to Console Application!
}
}

控制台应用程序:

class Program
{
static void Main(string[] args)
{
ServiceHost prodService = new ServiceHost(typeof(ProdsService));
ServiceDescription serviceDesciption = prodService.Description;
prodService.Open();
Console.ReadLine();
}
}

最佳答案

以下是运行主机和客户端的示例,其中主机可以在控制台上记录消息。在您的示例中,我不确定您为什么设置了 IsOneWay=true。对于这种特定情况,一种方法不是您想要的。此外,我在以下示例中使用了 net.tcp 绑定(bind);它也应该与任何其他绑定(bind)一起工作。

基本上在这个例子中,用户的输入被发送到主机服务,主机服务在控制台上回显消息。

[ServiceContract]
public interface IProdsService
{
[OperationContract]
void Alert(string msg);
}

/// <summary>
/// Host Class
/// </summary>
public class ProdsService : IProdsService
{
public ProdsService()
{
Console.WriteLine("Service instantiated.");
}

public void Alert(string msg)
{
Console.WriteLine(msg);
}
}

/// <summary>
/// Client proxy wrapper
/// </summary>
public class ProdsServiceClient : ClientBase<IProdsService>, IProdsService
{
public ProdsServiceClient()
{
}

public ProdsServiceClient(System.ServiceModel.Channels.Binding binding, System.ServiceModel.EndpointAddress remoteAddress) :
base(binding, remoteAddress)
{
}

public void Alert(string msg)
{
base.Channel.Alert(msg);
}
}

class Program
{
static ManualResetEvent _reset;

static void Main(string[] args)
{
string host = "localhost";
int port = 8888;

//ManualResetEvent is used for syncing start/stop of service.
_reset = new ManualResetEvent(false);

var action = new Action<string, int>(Start);
var result = action.BeginInvoke(host, port, null, null);

//Wait for svc startup, this can be synced with resetEvents.
Thread.Sleep(2000);

//Create a client instance and send your messages to host
using (var client = new ProdsServiceClient(new NetTcpBinding(), new EndpointAddress(string.Format("net.tcp://{0}:{1}", host, port))))
{
client.Alert("Test message");

string msg = string.Empty;
do
{
Console.Write("Type a message to send (X to exit): ");
msg = Console.ReadLine();
client.Alert(msg);
}
while (!msg.Trim().ToUpper().Equals("X"));
}

//Signal host to stop
_reset.Set();
action.EndInvoke(result);

Console.Write("Press any to exit.");
Console.ReadKey();
}

static void Start(string host, int port)
{
string uri = string.Format("net.tcp://{0}:{1}", host, port);
//var server = new ProdsService();
ServiceHost prodService = new ServiceHost(typeof(ProdsService));
prodService.AddServiceEndpoint(typeof(IProdsService), new NetTcpBinding(), uri);
Console.WriteLine("Service host opened");
prodService.Open();

//Wait until signaled to stop
_reset.WaitOne();
Console.WriteLine("Stopping host, please wait...");
prodService.Close();
Console.WriteLine("Service host closed");
}
}

关于c# - 控制台应用宿主wcf服务交互,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14289214/

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