Basic Course opening of WCF: create, test and call WCF

 In a flash, and half did not update the blog. To be honest, really it is a bit busy recently. But even then headlong, but also taking the time to learn something. More recently with WCF, come to share with you the knowledge about WCF it! In order to let everyone can understand, did not learn to take care of some of the original WCF, I write from the most basic things, I hope you can understand.

 First, create a simple WCF service

   After opening the VS2013, a new Silverlight project, just starting their own name, and create a Web site, you create the project, we add a WCF Web service project, enter the name UserService, for simplicity, choose the Silverlight-enabled WCF Service, as :

This creates out of the WCF service is automatically generated in webconfig the configuration information about the configuration of the WCF is not a few words can say clearly, to have time to write back a few blog about WCF for it. Then, we modify Dowork method, as shown:

Then, we click on the right mouse button above UserService.cs preview in a browser, if not wrong, then there will be the following:

Here, a WCF service is completed, the following test to see if our WCF can work, here recommend a tool for everyone, WCFStorm, we open WCFStorm, as shown:

We copy the address of the web address bar and paste it into the URL bar, and then click the green arrow button to the right, then we will see the software to resolve WCF services WCF display method, as shown:

We then choose a method, you will see specific parameters and return values, as shown in the right window:

There is no argument, click on the implementation, we will see among the far right to the results, as shown:

We see the result returned is string type, content Hello, world.

 

 Second, call a WCF service in SilverLight

   上面我们创建了一个WCF服务,并用WCFStorm工具进行了测试。下面我们在SilverLight中进行调用,这里没做过Silverlight没关系,你会WPF自然就会Silverlight了,其实SilverLight就相当于跑在浏览器中的WPF。这里我们在SilverLight项目中,引用上面点击鼠标右键,添加服务应用,如图:

点击发现按钮,就会出现我们刚刚的WCF服务地址,然后修改命名空间为UserServiceReference,点击确定即可。下面我们在MainPage.cs中添加代码,调用WCF方法,代码如下:

复制代码
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            GetData();
        }

        public void GetData()
        {
            UserServiceReference.UserServiceClient client = new UserServiceReference.UserServiceClient();
            client.DoWorkCompleted += client_DoWorkCompleted;
            client.DoWorkAsync();
        }

        void client_DoWorkCompleted(object sender, UserServiceReference.DoWorkCompletedEventArgs e)
        {
            MessageBox.Show(e.Result.ToString());
        }

    }
复制代码

 为了验证正常打开了,我们在页面中随便加点控件,这里我添加了一个按钮控件,运行项目后,会调用WCF服务,然后弹出一个消息框,显示Hello,world.

今天就先说到这里吧,欢迎大家加入QQ交流群一起交流学习~~

 

 作者:雲霏霏

QQ交流群:243633526

 博客地址:http://www.cnblogs.com/yunfeifei/

 声明:本博客原创文字只代表本人工作中在某一时间内总结的观点或结论,与本人所在单位没有直接利益关系。非商业,未授权,贴子请以现状保留,转载时必须保留此段声明,且在文章页面明显位置给出原文连接。

如果大家感觉我的博文对大家有帮助,请推荐支持一把,给我写作的动力。

Guess you like

Origin www.cnblogs.com/Jeely/p/11314506.html