【问题标题】:Is it possible to start exploring WCF using Visual Studio Express for Windows Desktop 2013?是否可以使用 Visual Studio Express for Windows Desktop 2013 开始探索 WCF?
【发布时间】:2014-11-12 12:21:53
【问题描述】:

您能否在 Visual Studio Express 2013 for Windows Desktop C# 中设置一个简单的 WCF 项目

MS 入门教程 (http://msdn.microsoft.com/en-us/library/ms734712(v=vs.110).aspx) 中描述的演练是指 Visual Studio(不是速成版),它具有 Visual Studio Express (WDExpress.exe) 中不可用的模板,特别是 WCF服务库

如果没有模板,你如何在 WDExpress.exe 中开始类似的事情?

顺便说一句,我尝试从 Visual Studio Express for Web 2013 (VWDExpress.exe) 复制模板,但没有成功。

【问题讨论】:

  • 我在这里没有看到问题? Self-Answer 完全可以接受,请参阅blog.stackoverflow.com/2011/07/…。但是,您实际上应该以标准格式提供问题和答案,而不是在问题帖子中包含答案。
  • 中肯的评论,但我花了一个多星期才走到这一步,要么是这个,要么是问了几十个可能无法回答的问题,并产生了许多重复的回答。
  • 帖子被重新格式化为一个问题,并以 n 答案给出了建议的解决方法。

标签: c# wcf visual-studio-2013


【解决方案1】:

下面是使用 Visual Studio Express 2013 的 http://msdn.microsoft.com/en-us/library/bb386386.aspx 可能的解决方法。

所有步骤都在 VSE 2013 for Windows Desktop (WDExpress.exe) 中执行

  • 第 1 步 - 使用 Class Library 的模板启动一个新项目 - 它应该生成一个默认名称为 ClassLibrary1 的项目

  • 第 2 步 - 转到 References(在 Solution Explorer 中)并添加对 System.ServiceModelSystem.Runtime.Serialization 的引用

  • 第 3 步 - 创建一个名为 WCFServiceLibrary1.cs 的新类,其内容如下

using System.ServiceModel;

namespace ClassLibrary1
{
    public class WCFServiceLibrary1 : IWCFServiceLibrary1
    {
        public string GetData(string value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}
  • 第 4 步 - 创建一个名为 IWCFServiceLibrary1.cs 的新类,其内容如下

using System.ServiceModel;

namespace ClassLibrary1
{
    [ServiceContract] 
    public interface IWCFServiceLibrary1
    {
        [OperationContract]
        string GetData(string value);
    }
}
  • 第 5 步 - 您需要一个客户端来运行 WCF,因此创建一个 Windows 窗体,其默认名称为 Form1.cs,并添加三个控件;一个文本框 (textBox1)、一个标签 (label1) 和一个按钮 (button1)

  • 第 6 步 - 在 [设计] 模式下,双击 button1 并编辑操作,使 Form1.cs 看起来像这样

using System;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            ClassLibrary1.WCFServiceLibrary1 client = new ClassLibrary1.WCFServiceLibrary1();
            label1.Text = client.GetData(textBox1.Text);
        }
    }
}
  • 第 7 步 - 添加名为 Program.cs 的 Main 类,其内容如下

using System;
using System.Windows.Forms;

namespace ClassLibrary1
{
    public class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Form1 form = new Form1();
            Application.Run(form);
        }
    }
}
  • 第 8 步 - 打开项目属性 (PROJECT >> ClassLibrary1.properties) 中的 Application 选项卡,并将 Output Type 设置为 Windows Application 并将 Startup Object 设置为 ClassLibrary1.Program

  • 第 9 步 - F5 将启动表单,其行为与演练结尾处To build a client application 下所述的行为相同

因此,此方法不会执行演练中的“测试服务”。此外,它还简化了几个步骤,并将 WCF 与 Windows 窗体捆绑在同一个项目中。希望它提供了可以为您的应用程序开发和调整的基本工作代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-04-04
    • 2017-02-18
    • 2016-03-17
    • 2014-02-25
    • 1970-01-01
    • 2019-05-12
    • 1970-01-01
    • 2023-04-08
    相关资源
    最近更新 更多