在WCF服务中使用表单控件 [英] Use Form Controls Into WCF Service
问题描述
我编写了一个程序,该程序具有两个控制台应用程序(组件A和B)和一个WCF服务库.
我将A和B连接在一起,因此消息可以从A传输到B或从B传输到A(WCF双工).
为此,我将WCF服务库的引用添加到A& B项目.
现在,我想将这些控制台应用程序更改为WinForm应用程序,但是我不能这样做
因为在控制台应用程序中,我们有console.writeline(),它可以在WCF服务库中使用,但是在WinForm中,MessageBox不能在WCF服务中使用.
我也无法添加A&的引用B到Wcf服务,因为它产生循环依赖性错误
这是带有控制台应用程序的项目:
在Project A Program.cs文件中:
I Wrote a program that has two Console application(assusme A & B) and a WCF Service Library.
I connected A and B together so message can be transfer from A to B or B to A(WCF Duplex).
To Do this i added the reference of WCF Service Library to both A & B project.
Now I Want To Change these Console Applications To WinForm application but i cannot do that
because in console application we have console.writeline() that it Can be use in WCF Service Library But In WinForm,MessageBox Cannot be use in WCF Service.
Also I Cannot Add Reference Of A & B to Wcf Service Because it makes Circular Dependency Error
This Is The Project With Console Applications :
In Project A Program.cs file :
internal static void StartService()
{
myServiceHost = new ServiceHost(typeof(WCF.MessageService));
myServiceHost.Open();
}
internal static void StopService()
{
if (myServiceHost.State != CommunicationState.Closed)
myServiceHost.Close();
}
static void Main()
{
StartService();
Console.WriteLine("Service running; press return to exit");
Console.ReadLine();
StopService();
Console.WriteLine("stopped");
}
在Project B Program.cs文件中:
In Project B Program.cs file:
class ClientCallback : IMyMessageCallback
{
public void OnCallback(string message)
{
Console.WriteLine("message from the server: {0}", message);
}
}
class Program
{
static void Main()
{
Console.WriteLine("Client - wait for service");
Console.ReadLine();
WSDualHttpBinding binding = new WSDualHttpBinding();
EndpointAddress address =
newEndpointAddress("http://localhost:8731/Design_Time_Addresses/MessageService/Service1/");
ClientCallback clientCallback = new ClientCallback();
InstanceContext context = new InstanceContext(clientCallback);
DuplexChannelFactory<IMyMessage> factory =
new DuplexChannelFactory<IMyMessage>(context, binding, address);
IMyMessage messageChannel = factory.CreateChannel();
messageChannel.MessageToServer("From the client");
Console.WriteLine("Client - press return to exit");
Console.ReadLine();
}
}
在IMYMessage.cs文件(WCF服务库)中:
In IMYMessage.cs File (WCF Service Library):
public interface IMyMessageCallback
{
[OperationContract(IsOneWay = true)]
void OnCallback(string message);
}
[ServiceContract(CallbackContract = typeof(IMyMessageCallback))]
public interface IMyMessage
{
[OperationContract]
void MessageToServer(string message);
}
在MessageService.cs文件(WCF服务库)中:
In MessageService.cs File(WCF Service Library) :
public class MessageService : IMyMessage
{
public void MessageToServer(string message)
{
Console.WriteLine("message from the client: {0}", message);
IMyMessageCallback callback =
OperationContext.Current.
GetCallbackChannel<IMyMessageCallback>();
callback.OnCallback("message from the server");
new Thread(ThreadCallback).Start(callback);
}
private void ThreadCallback(object callback)
{
IMyMessageCallback messageCallback = callback as IMyMessageCallback;
for (int i = 0; i < 10; i++)
{
messageCallback.OnCallback("message " + i.ToString());
Thread.Sleep(1000);
}
}
}
推荐答案
为什么不将基于控制台的日志记录替换为 Log4Net [^ ]
您还希望查看 WCF/WPF聊天应用程序 [ ^ ].
最好的问候
Espen Harlinn
Why not replace your console based logging with Log4Net[^]
You also want to have a look at WCF / WPF Chat Application[^] by Sacha Barber.
Best regards
Espen Harlinn
更好的解决方案是在下面的链接中.
http://gregs-blog.com/2008/02/09/how-to-convert-a-console-app-into-a-windows-app-in-c-part-two/ [ ^ ]
Better Solution of your answer is in following link.
http://gregs-blog.com/2008/02/09/how-to-convert-a-console-app-into-a-windows-app-in-c-part-two/[^]
我使用此链接来解决:
http://www.switchonthecode.com/tutorials/wcf-callbacks-hanging-wpf-applications [^ ]
I Used This Link To Solve:
http://www.switchonthecode.com/tutorials/wcf-callbacks-hanging-wpf-applications[^]
这篇关于在WCF服务中使用表单控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!