Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在给定场景中编程WCF服务?_Wcf - Fatal编程技术网

如何在给定场景中编程WCF服务?

如何在给定场景中编程WCF服务?,wcf,Wcf,我目前正在开发一个C#Windows窗体应用程序,我打算让它与服务器交互。服务器将从我开发的移动应用程序接收帖子,每当收到帖子时,我的Windows窗体应用程序都会收到通知并给我一个通知 例如,我的移动应用程序向我的服务器发送消息。一旦我的服务器收到消息,我的windows窗体应用程序将显示一个新的通知,显示收到的消息的内容 我现在开始开发WCF服务,并已达到PostingService方法,我不确定我如何能够继续编程该服务,使其以上述方式工作 public class PostingServi

我目前正在开发一个C#Windows窗体应用程序,我打算让它与服务器交互。服务器将从我开发的移动应用程序接收帖子,每当收到帖子时,我的Windows窗体应用程序都会收到通知并给我一个通知

例如,我的移动应用程序向我的服务器发送消息。一旦我的服务器收到消息,我的windows窗体应用程序将显示一个新的通知,显示收到的消息的内容

我现在开始开发WCF服务,并已达到PostingService方法,我不确定我如何能够继续编程该服务,使其以上述方式工作

public class PostingService : IPostingService
{
    public void NotifyAboutPosting(Posting post)
    {
        // do something with post here
    }
}
在我对服务进行编程之后,我不知道如何测试服务?上传一篇假帖子,看看服务是否有效,这意味着一个虚拟测试。谢谢

编辑

对于我的主要方法,代码如下所示

Uri baseAddress = new Uri("http://localhost/");

        ServiceHost selfHost = new ServiceHost(typeof(IPostingService), baseAddress);

        try
        {

            selfHost.AddServiceEndpoint(typeof(IPostingService),new WSHttpBinding(),     "Posting");

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            selfHost.Description.Behaviors.Add(smb);

            selfHost.Open();
            Console.WriteLine("The service is ready.");
            Console.WriteLine("Press <ENTER> to terminate service.");
            Console.WriteLine();
            Console.ReadLine();

            selfHost.Close();
        }
        catch (CommunicationException ce)
        {
            Console.WriteLine("An exception occurred: {0}", ce.Message);
            selfHost.Abort();
        }
uribaseaddress=新的Uri(“http://localhost/");
ServiceHost selfHost=新的ServiceHost(typeof(IPostingService),baseAddress);
尝试
{
selfHost.AddServiceEndpoint(typeof(IPostingService),新的WSHttpBinding(),“Posting”);
ServiceMetadataBehavior smb=新ServiceMetadataBehavior();
smb.HttpGetEnabled=true;
selfHost.Description.Behaviors.Add(smb);
selfHost.Open();
WriteLine(“服务准备就绪”);
控制台。WriteLine(“按以终止服务”);
Console.WriteLine();
Console.ReadLine();
selfHost.Close();
}
捕获(通信异常ce)
{
WriteLine(“发生异常:{0}”,ce.Message);
selfHost.Abort();
}

基本上,我只是按照MSDN WCF getting started tutorial提供的教程进行操作。不确定对于我想要的实现类型,这是否真的是正确的方法。

也许WCF回调可以满足您的要求:


也许WCF回调可以满足您的要求:


好吧,你的WCF服务可以做你想做的任何事情——那么你真正想让它做什么呢

发布服务器从移动设备获取新邮件,然后在Winforms应用程序中调用此WCF服务类您希望此时此地发生什么事

要记住一件事:接收消息的WCF服务类和Winforms应用程序可能在不同的线程上运行;如果是这种情况,您不能仅从服务代码更新Winforms UI上的UI元素(例如,您需要使用一些同步方法)。但这取决于您在Winforms应用程序中创建和打开
ServiceHost
的确切方式

更新:如果您将创建和初始化
ServiceHost的代码放入主申请表中(有关如何执行此操作的示例,请参阅),那么您可能只需执行以下操作:

public class PostingService : IPostingService
{
    public void NotifyAboutPosting(Posting post)
    {
       MessageBox.Show(post.Title, post.Message, 
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}

假设您的
Posting
类同时具有
.Title
.Message
字符串属性…

那么,您的WCF服务可以做任何您想做的事情-那么您真正想让它做什么呢

发布服务器从移动设备获取新邮件,然后在Winforms应用程序中调用此WCF服务类您希望此时此地发生什么事

要记住一件事:接收消息的WCF服务类和Winforms应用程序可能在不同的线程上运行;如果是这种情况,您不能仅从服务代码更新Winforms UI上的UI元素(例如,您需要使用一些同步方法)。但这取决于您在Winforms应用程序中创建和打开
ServiceHost
的确切方式

更新:如果您将创建和初始化
ServiceHost的代码放入主申请表中(有关如何执行此操作的示例,请参阅),那么您可能只需执行以下操作:

public class PostingService : IPostingService
{
    public void NotifyAboutPosting(Posting post)
    {
       MessageBox.Show(post.Title, post.Message, 
                       MessageBoxButtons.OK, MessageBoxIcon.Information);
    }
}
假设您的
发布
类同时具有
标题
消息
字符串属性…

1)发布服务程序集(类库项目)

接口:IPostingService.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}

实现:PostingService.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}

数据合同:Posting.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}
2) 您的Winforms应用程序(Winforms应用程序项目)

必须引用服务程序集(因为它需要服务协定和数据协定类)

应用程序的主要形式:Form1.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}
3) 运行您的Winforms应用程序-现在服务已启动并运行,并准备好接收通知

4) 启动(这就是您的“发布服务器”稍后将要执行的操作)

4a)文件>添加服务-输入-应找到您的服务

4b)如果找到:在“发布”类的属性中输入一些值(ID、标题、消息)

4c)单击“调用”-现在应该调用您的服务,弹出一个对话框(消息框),其中包含您定义的标题和消息

1)发布服务程序集(类库项目)

接口:IPostingService.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}

实现:PostingService.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}

数据合同:Posting.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}
2) 您的Winforms应用程序(Winforms应用程序项目)

必须引用服务程序集(因为它需要服务协定和数据协定类)

应用程序的主要形式:Form1.cs

using System;
using System.ServiceModel;

namespace PostingService
{
    [ServiceContract]
    public interface IPostingService
    {
        [OperationContract]
        void NotifyAboutPosting(Posting posting);
    }
}
using System;
using System.Windows.Forms;

namespace PostingService
{
    public class PostingService : IPostingService
    {
        public void NotifyAboutPosting(Posting posting)
        {
            MessageBox.Show(posting.Message, posting.Title, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
    }
}
using System;
using System.Runtime.Serialization;

namespace PostingService
{
    [DataContract]
    public class Posting
    {
        [DataMember]
        public int ID { get; set; }

        [DataMember]
        public string Title { get; set; }

        [DataMember]
        public string Message { get; set; }
    }
}
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Windows.Forms;

using PostingService;   // your class library from above

namespace WinformsApp
{
    public partial class Form1 : Form
    {
        private ServiceHost _host = null;

        public Form1()
        {
            InitializeComponent();

            // IMPORTANT: here you need the **SERVICE IMPLEMENTATION CLASS** in the typeof() (*NOT* the interface!)
            _host = new ServiceHost(typeof(PostingService), new Uri("http://localhost:8888/PostingService"));

            ServiceMetadataBehavior smb = new ServiceMetadataBehavior();
            smb.HttpGetEnabled = true;
            _host.Description.Behaviors.Add(smb);

            _host.Open();

            label2.Text = "Service up and running (http://localhost:8888/PostingService)";
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            _host.Close();

            base.OnFormClosed(e);
        }
    }
}
3) 运行您的Winforms应用程序-现在服务已启动并运行,并准备好接收通知

4) 启动(这就是您的“发布服务器”稍后将要执行的操作)

4a)文件>添加服务-输入-应找到您的服务

4b)如果找到:将一些值输入到