如何使用WCF将文本发送到不同的应用程序 [英] How to send text to different application with WCF

查看:85
本文介绍了如何使用WCF将文本发送到不同的应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的解决方案中有一个Windows应用程序(A),WCFService Library,WCFServiceApplication。



从Windows应用程序,当我点击提交时,我可以显示数据标签。



现在我该如何从其他应用程序(Windows应用程序B)按钮发送数据点击。



以下是Windows申请表的代码:



  private   void  btnMainWindow_Click( object  sender,EventArgs e)
{
ChannelFactory< IService1> factory = null ;
try
{
BasicHttpBinding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress( );
factory = new ChannelFactory< IService1>(绑定,地址);
IService1 channel = factory.CreateChannel();

}
catch (CommunicationException ex)
{
// Console.WriteLine(ex);
if (工厂! = null
factory.Abort();
}
catch (TimeoutException)
{
if (factory!= null
factory.Abort();
}
catch (例外情况)
{
if (factory!= null
factory.Abort();
// Console.WriteLine(ex.Message);
}
MessageBox.Show( 代理关闭);
}





我的界面



 [ServiceContract] 
public interface IMyService
{
[OperationContract]
string GetData( int );
}







  public   string  GetData( int  
{
return string .Format(< span class =code-string> 您输入了:{0} value );
}





我的尝试:



我同时使用了WCFService Library和WCFServiceApplication,因为我没有添加服务引用。

解决方案

这样的东西,如果组件在您可以使用命名管道的同一台机器:

 使用(ServiceHost host =  ServiceHost(
typeof (GetData),
new Uri [] {
new Uri( http:// localhost:8000),
new Uri( net.pipe:// localhost
}))





这是我能找到的最简单的例子:WCF教程 - 基本进程间通信 - Tech.pro [ ^


I have a windows application(A), WCFService Library, WCFServiceApplication in my solution.

From windows application, when I hit submit, I can get data displayed on label.

Now what should I do to send the data from another application(Windows Application B) button click.

Here is code for Windows Application Form:

private void btnMainWindow_Click(object sender, EventArgs e)
        {
            ChannelFactory<IService1> factory = null;
            try 
            {
                BasicHttpBinding binding = new BasicHttpBinding();
                EndpointAddress address = new EndpointAddress("");
                factory = new ChannelFactory<IService1>(binding, address);
                IService1 channel = factory.CreateChannel();
                
            }
            catch (CommunicationException ex)
            {
                //Console.WriteLine(ex);
                if (factory != null)
                    factory.Abort();
            }
            catch (TimeoutException)
            {
                if (factory != null)
                    factory.Abort();
            }
            catch (Exception ex)
            {
                if (factory != null)
                    factory.Abort();
                //Console.WriteLine(ex.Message);
            }
            MessageBox.Show("Proxy closed");
        }



My Interface

[ServiceContract]
    public interface IMyService
    {
        [OperationContract]
        string GetData(int value);
    }




public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }



What I have tried:

I took both WCFService Library and WCFServiceApplication because, I didn't add Service Reference.

解决方案

Something like this, if the components are on the same machine you can use named pipes:

using (ServiceHost host = new ServiceHost(
      typeof(GetData),
      new Uri[]{
        new Uri("http://localhost:8000"),
        new Uri("net.pipe://localhost")
      }))



This is the simplest example I could find: WCF Tutorial - Basic Interprocess Communication - Tech.pro[^]


这篇关于如何使用WCF将文本发送到不同的应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆