WCF入门

瞎看了下WCF,具体也不怎么明白,管他呢,先上手一个demo再说吧!过二天再来搞搞总结神马的。

首先创建一个WCF服务项目:

建好后为了更好的使用把所有的.cs和.svc文件都删除,再创建

创建完毕了.添加接口方法showname(string name)并在svc文件中实现。

服务就算完成了。

把svc文件设置为启动项,按F5可以看到如下的东东:

这里可以测试我们接口,这样服务算全部搞定。

现在可是创建client:随便创建一个项目-我使用的winform,就是简单输入用户名,在点击展示结果。

先添加服务:Add service reference 把刚刚看到的地址copy过去,生成后生成了如下的app.config文件。

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IUser" />
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:60100/User.svc" binding="basicHttpBinding"
                bindingConfiguration="BasicHttpBinding_IUser" contract="WCFService.IUser"
                name="BasicHttpBinding_IUser" />
        </client>
    </system.serviceModel>
</configuration>

winform中的代码:

using System;
using System.Windows.Forms;
using WCFClient.WCFService;

namespace WCFClient
{
    public partial class InvokeWCF : Form
    {
        public InvokeWCF()
        {
            InitializeComponent();
        }

        private void btnInvoke_Click(object sender, EventArgs e)
        {
            // Since the address is http://localhost:60100/User.svc, the Class is going to be UserClient
            UserClient uc = new UserClient();
            lblMsg.Text = uc.ShowName(txtName.Text);
        }
    }
}

效果:

就这样完毕...

 

posted on 2013-08-11 20:24  wsf-yifan  阅读(254)  评论(0编辑  收藏  举报

导航